返回 AiToEarn
1707791500-InitialMigration.ts
根目录 / project / aitoearn-electron / electron / db / migrations / 1707791500-InitialMigration.ts
1 import { MigrationInterface, QueryRunner } from 'typeorm';
2
3 export class InitialMigration1707791500000 implements MigrationInterface {
4 name = 'InitialMigration1707791500000';
5
6 async up(queryRunner: QueryRunner): Promise<void> {
7 // Create user table
8 await queryRunner.query(`
9 CREATE TABLE "user" (
10 "id" varchar PRIMARY KEY NOT NULL,
11 "name" varchar NOT NULL,
12 "phone" varchar NOT NULL,
13 "loginTime" datetime NOT NULL,
14 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
15 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP)
16 )
17 `);
18
19 // Create account table
20 await queryRunner.query(`
21 CREATE TABLE "account" (
22 "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
23 "userId" varchar NOT NULL,
24 "type" varchar NOT NULL,
25 "loginCookie" varchar NOT NULL,
26 "token" varchar,
27 "loginTime" datetime,
28 "uid" varchar NOT NULL,
29 "account" varchar NOT NULL,
30 "avatar" varchar NOT NULL,
31 "nickname" varchar NOT NULL,
32 "fansCount" integer NOT NULL DEFAULT 0,
33 "readCount" integer NOT NULL DEFAULT 0,
34 "likeCount" integer NOT NULL DEFAULT 0,
35 "collectCount" integer NOT NULL DEFAULT 0,
36 "forwardCount" integer NOT NULL DEFAULT 0,
37 "commentCount" integer NOT NULL DEFAULT 0,
38 "lastStatsTime" datetime,
39 "workCount" integer NOT NULL DEFAULT 0,
40 "income" bigint NOT NULL DEFAULT 0,
41 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
42 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
43 CONSTRAINT "FK_account_user" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE
44 )
45 `);
46
47 // Create pubRecord table
48 await queryRunner.query(`
49 CREATE TABLE "pubRecord" (
50 "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
51 "userId" varchar NOT NULL,
52 "type" varchar NOT NULL,
53 "title" varchar,
54 "desc" varchar NOT NULL,
55 "videoPath" varchar NOT NULL,
56 "coverPath" varchar NOT NULL,
57 "publishTime" datetime NOT NULL,
58 "status" tinyint NOT NULL DEFAULT 0,
59 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
60 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
61 CONSTRAINT "FK_pubRecord_user" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE
62 )
63 `);
64
65 // Create video table
66 await queryRunner.query(`
67 CREATE TABLE "video" (
68 "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
69 "userId" varchar NOT NULL,
70 "pubRecordId" integer NOT NULL,
71 "accountId" integer NOT NULL,
72 "title" varchar,
73 "desc" varchar,
74 "videoPath" varchar,
75 "coverPath" varchar,
76 "lastStatsTime" datetime,
77 "dataId" varchar,
78 "type" varchar NOT NULL,
79 "publishTime" datetime,
80 "otherInfo" json,
81 "failMsg" varchar,
82 "status" tinyint NOT NULL DEFAULT 0,
83 "readCount" integer NOT NULL DEFAULT 0,
84 "likeCount" integer NOT NULL DEFAULT 0,
85 "collectCount" integer NOT NULL DEFAULT 0,
86 "forwardCount" integer NOT NULL DEFAULT 0,
87 "commentCount" integer NOT NULL DEFAULT 0,
88 "income" bigint NOT NULL DEFAULT 0,
89 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
90 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
91 CONSTRAINT "FK_video_user" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE,
92 CONSTRAINT "FK_video_pubRecord" FOREIGN KEY ("pubRecordId") REFERENCES "pubRecord" ("id") ON DELETE CASCADE,
93 CONSTRAINT "FK_video_account" FOREIGN KEY ("accountId") REFERENCES "account" ("id") ON DELETE CASCADE
94 )
95 `);
96
97 // Create account_stats table
98 await queryRunner.query(`
99 CREATE TABLE "account_stats" (
100 "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
101 "userId" varchar NOT NULL,
102 "accountId" integer NOT NULL,
103 "type" varchar NOT NULL,
104 "readCount" integer NOT NULL DEFAULT 0,
105 "likeCount" integer NOT NULL DEFAULT 0,
106 "collectCount" integer NOT NULL DEFAULT 0,
107 "forwardCount" integer NOT NULL DEFAULT 0,
108 "commentCount" integer NOT NULL DEFAULT 0,
109 "fansCount" integer NOT NULL DEFAULT 0,
110 "income" bigint NOT NULL DEFAULT 0,
111 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
112 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
113 CONSTRAINT "FK_account_stats_user" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE,
114 CONSTRAINT "FK_account_stats_account" FOREIGN KEY ("accountId") REFERENCES "account" ("id") ON DELETE CASCADE
115 )
116 `);
117
118 // Create video_stats table
119 await queryRunner.query(`
120 CREATE TABLE "video_stats" (
121 "id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
122 "userId" varchar NOT NULL,
123 "videoId" integer NOT NULL,
124 "accountId" integer NOT NULL,
125 "type" varchar NOT NULL,
126 "readCount" integer NOT NULL DEFAULT 0,
127 "likeCount" integer NOT NULL DEFAULT 0,
128 "collectCount" integer NOT NULL DEFAULT 0,
129 "forwardCount" integer NOT NULL DEFAULT 0,
130 "commentCount" integer NOT NULL DEFAULT 0,
131 "income" bigint NOT NULL DEFAULT 0,
132 "createTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
133 "updateTime" datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP),
134 CONSTRAINT "FK_video_stats_user" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE,
135 CONSTRAINT "FK_video_stats_video" FOREIGN KEY ("videoId") REFERENCES "video" ("id") ON DELETE CASCADE,
136 CONSTRAINT "FK_video_stats_account" FOREIGN KEY ("accountId") REFERENCES "account" ("id") ON DELETE CASCADE
137 )
138 `);
139 }
140
141 async down(queryRunner: QueryRunner): Promise<void> {
142 // Drop tables in reverse order to handle foreign key constraints
143 await queryRunner.query(`DROP TABLE "video_stats"`);
144 await queryRunner.query(`DROP TABLE "account_stats"`);
145 await queryRunner.query(`DROP TABLE "video"`);
146 await queryRunner.query(`DROP TABLE "pubRecord"`);
147 await queryRunner.query(`DROP TABLE "account"`);
148 await queryRunner.query(`DROP TABLE "user"`);
149 }
150 }
151
151 lines TYPESCRIPT