Skip to content
This repository was archived by the owner on Mar 28, 2024. It is now read-only.

Commit 34b274c

Browse files
committed
feat: add migration for initial tables
1 parent 5b6b004 commit 34b274c

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,6 @@ lerna-debug.log*
3131
!.vscode/settings.json
3232
!.vscode/tasks.json
3333
!.vscode/launch.json
34-
!.vscode/extensions.json
34+
!.vscode/extensions.json
35+
36+
*.sqlite

ormconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"type": "sqlite",
33
"database": "database.sqlite",
4-
"synchronize": true,
4+
"synchronize": false,
55
"logging": false,
66
"entities": ["src/db/entities/**/*.ts"],
77
"migrations": ["src/db/migrations/**/*.ts"],

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
"test:watch": "jest --watch",
1919
"test:cov": "jest --coverage",
2020
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
21-
"test:e2e": "jest --config ./test/jest-e2e.json"
21+
"test:e2e": "jest --config ./test/jest-e2e.json",
22+
"migration:create": "ts-node node_modules/.bin/typeorm migration:generate",
23+
"migration:run": "ts-node node_modules/.bin/typeorm migration:run"
2224
},
2325
"dependencies": {
2426
"@nestjs/common": "^7.0.7",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {MigrationInterface, QueryRunner} from "typeorm";
2+
3+
export class CreateInitialTables1586288011982 implements MigrationInterface {
4+
name = 'CreateInitialTables1586288011982'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`CREATE TABLE "episode" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "url" varchar NOT NULL, "image" varchar NOT NULL, "type" varchar NOT NULL, "filesisze" integer NOT NULL, "explicit" boolean NOT NULL, "guid" varchar NOT NULL, "duration" varchar NOT NULL, "publication" datetime NOT NULL, "podcastId" integer)`, undefined);
8+
await queryRunner.query(`CREATE TABLE "category" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL)`, undefined);
9+
await queryRunner.query(`CREATE TABLE "podcast" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "image" varchar NOT NULL, "language" varchar NOT NULL, "link" varchar NOT NULL, "explicit" boolean NOT NULL, "authorId" integer)`, undefined);
10+
await queryRunner.query(`CREATE TABLE "author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL)`, undefined);
11+
await queryRunner.query(`CREATE TABLE "temporary_episode" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "url" varchar NOT NULL, "image" varchar NOT NULL, "type" varchar NOT NULL, "filesisze" integer NOT NULL, "explicit" boolean NOT NULL, "guid" varchar NOT NULL, "duration" varchar NOT NULL, "publication" datetime NOT NULL, "podcastId" integer, CONSTRAINT "FK_553934f46dc107c0ce9326d2419" FOREIGN KEY ("podcastId") REFERENCES "podcast" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, undefined);
12+
await queryRunner.query(`INSERT INTO "temporary_episode"("id", "title", "description", "url", "image", "type", "filesisze", "explicit", "guid", "duration", "publication", "podcastId") SELECT "id", "title", "description", "url", "image", "type", "filesisze", "explicit", "guid", "duration", "publication", "podcastId" FROM "episode"`, undefined);
13+
await queryRunner.query(`DROP TABLE "episode"`, undefined);
14+
await queryRunner.query(`ALTER TABLE "temporary_episode" RENAME TO "episode"`, undefined);
15+
await queryRunner.query(`CREATE TABLE "temporary_podcast" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "image" varchar NOT NULL, "language" varchar NOT NULL, "link" varchar NOT NULL, "explicit" boolean NOT NULL, "authorId" integer, CONSTRAINT "FK_ab01f0a2c00d90e8e4312094f14" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, undefined);
16+
await queryRunner.query(`INSERT INTO "temporary_podcast"("id", "title", "description", "image", "language", "link", "explicit", "authorId") SELECT "id", "title", "description", "image", "language", "link", "explicit", "authorId" FROM "podcast"`, undefined);
17+
await queryRunner.query(`DROP TABLE "podcast"`, undefined);
18+
await queryRunner.query(`ALTER TABLE "temporary_podcast" RENAME TO "podcast"`, undefined);
19+
}
20+
21+
public async down(queryRunner: QueryRunner): Promise<void> {
22+
await queryRunner.query(`ALTER TABLE "podcast" RENAME TO "temporary_podcast"`, undefined);
23+
await queryRunner.query(`CREATE TABLE "podcast" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "image" varchar NOT NULL, "language" varchar NOT NULL, "link" varchar NOT NULL, "explicit" boolean NOT NULL, "authorId" integer)`, undefined);
24+
await queryRunner.query(`INSERT INTO "podcast"("id", "title", "description", "image", "language", "link", "explicit", "authorId") SELECT "id", "title", "description", "image", "language", "link", "explicit", "authorId" FROM "temporary_podcast"`, undefined);
25+
await queryRunner.query(`DROP TABLE "temporary_podcast"`, undefined);
26+
await queryRunner.query(`ALTER TABLE "episode" RENAME TO "temporary_episode"`, undefined);
27+
await queryRunner.query(`CREATE TABLE "episode" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "description" varchar NOT NULL, "url" varchar NOT NULL, "image" varchar NOT NULL, "type" varchar NOT NULL, "filesisze" integer NOT NULL, "explicit" boolean NOT NULL, "guid" varchar NOT NULL, "duration" varchar NOT NULL, "publication" datetime NOT NULL, "podcastId" integer)`, undefined);
28+
await queryRunner.query(`INSERT INTO "episode"("id", "title", "description", "url", "image", "type", "filesisze", "explicit", "guid", "duration", "publication", "podcastId") SELECT "id", "title", "description", "url", "image", "type", "filesisze", "explicit", "guid", "duration", "publication", "podcastId" FROM "temporary_episode"`, undefined);
29+
await queryRunner.query(`DROP TABLE "temporary_episode"`, undefined);
30+
await queryRunner.query(`DROP TABLE "author"`, undefined);
31+
await queryRunner.query(`DROP TABLE "podcast"`, undefined);
32+
await queryRunner.query(`DROP TABLE "category"`, undefined);
33+
await queryRunner.query(`DROP TABLE "episode"`, undefined);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)