From f8f198f9c99ad57c42eb3aade944f9de6f84b7c3 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 8 Oct 2023 15:08:57 +0200 Subject: [PATCH] feat: add initial database migration Signed-off-by: David Mehren --- backend/package.json | 3 +- backend/src/app.module.ts | 4 +- .../migrations/mariadb-1696756465867-init.ts | 268 ++++++++++ .../migrations/postgres-1696756465867-init.ts | 280 ++++++++++ .../migrations/sqlite-1696756465867-init.ts | 496 ++++++++++++++++++ backend/src/ormconfig.ts | 28 + backend/test/test-setup.ts | 8 +- 7 files changed, 1082 insertions(+), 5 deletions(-) create mode 100644 backend/src/migrations/mariadb-1696756465867-init.ts create mode 100644 backend/src/migrations/postgres-1696756465867-init.ts create mode 100644 backend/src/migrations/sqlite-1696756465867-init.ts create mode 100644 backend/src/ormconfig.ts diff --git a/backend/package.json b/backend/package.json index 25c903105..197e356e1 100644 --- a/backend/package.json +++ b/backend/package.json @@ -21,7 +21,8 @@ "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", "test:e2e": "jest --config jest-e2e.json && rimraf test_uploads*", "test:e2e:ci": "jest --config jest-e2e.json --coverage && rimraf test_uploads*", - "seed": "ts-node src/seed.ts" + "seed": "ts-node src/seed.ts", + "typeorm": "typeorm-ts-node-commonjs -d src/ormconfig.ts" }, "dependencies": { "@azure/storage-blob": "12.16.0", diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 0a3f9b04e..02416dd57 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -68,10 +68,10 @@ const routes: Routes = [ password: databaseConfig.password, database: databaseConfig.database, autoLoadEntities: true, - synchronize: true, // ToDo: Remove this before release. It should be replaced by database migrations - // https://stackoverflow.com/questions/65222981/typeorm-synchronize-in-production logging: true, logger: logger, + migrations: [`**/migrations/${databaseConfig.type}-*{.ts,.js}`], + migrationsRun: true, }; }, }), diff --git a/backend/src/migrations/mariadb-1696756465867-init.ts b/backend/src/migrations/mariadb-1696756465867-init.ts new file mode 100644 index 000000000..5cf3a3ed9 --- /dev/null +++ b/backend/src/migrations/mariadb-1696756465867-init.ts @@ -0,0 +1,268 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class Init1696756465867 implements MigrationInterface { + name = 'Init1696756465867'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE \`auth_token\` (\`id\` int NOT NULL AUTO_INCREMENT, \`keyId\` varchar(255) NOT NULL, \`label\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`accessTokenHash\` varchar(255) NOT NULL, \`validUntil\` datetime NOT NULL, \`lastUsedAt\` date NULL, \`userId\` int NULL, UNIQUE INDEX \`IDX_9d2cf0a2cc3df58b87cbbc43e4\` (\`keyId\`), UNIQUE INDEX \`IDX_15b2228d06b08bb4cc98876c65\` (\`accessTokenHash\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`history_entry\` (\`id\` int NOT NULL AUTO_INCREMENT, \`pinStatus\` tinyint NOT NULL, \`updatedAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`userId\` int NULL, \`noteId\` int NULL, UNIQUE INDEX \`IDX_928dd947355b0837366470a916\` (\`noteId\`, \`userId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`media_upload\` (\`id\` varchar(255) NOT NULL, \`backendType\` varchar(255) NOT NULL, \`fileUrl\` varchar(255) NOT NULL, \`backendData\` text NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`noteId\` int NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`group\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, \`displayName\` varchar(255) NOT NULL, \`special\` tinyint NOT NULL, UNIQUE INDEX \`IDX_8a45300fd825918f3b40195fbd\` (\`name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`note_group_permission\` (\`id\` int NOT NULL AUTO_INCREMENT, \`canEdit\` tinyint NOT NULL, \`groupId\` int NULL, \`noteId\` int NULL, UNIQUE INDEX \`IDX_ee1744842a9ef3ffbc05a7016a\` (\`groupId\`, \`noteId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`note_user_permission\` (\`id\` int NOT NULL AUTO_INCREMENT, \`canEdit\` tinyint NOT NULL, \`userId\` int NULL, \`noteId\` int NULL, UNIQUE INDEX \`IDX_5a3e91233d8878f98f5ad86b71\` (\`userId\`, \`noteId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`alias\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, \`primary\` tinyint NULL COMMENT 'This field tells you if this is the primary alias of the note. If this field is null, that means this alias is not primary.', \`noteId\` int NULL, UNIQUE INDEX \`IDX_89f27e45cc5c1e43abd9132c9b\` (\`name\`), UNIQUE INDEX \`Only one primary alias per note\` (\`noteId\`, \`primary\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`note\` (\`id\` int NOT NULL AUTO_INCREMENT, \`publicId\` text NOT NULL, \`viewCount\` int NOT NULL DEFAULT '0', \`version\` int NOT NULL DEFAULT '2', \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`ownerId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`tag\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`revision\` (\`id\` int NOT NULL AUTO_INCREMENT, \`patch\` text NOT NULL, \`title\` text NOT NULL, \`description\` text NOT NULL, \`content\` text NOT NULL, \`length\` int NOT NULL, \`yjsStateVector\` text NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`noteId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`edit\` (\`id\` int NOT NULL AUTO_INCREMENT, \`startPos\` int NOT NULL, \`endPos\` int NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updatedAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`authorId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`session\` (\`id\` varchar(255) NOT NULL, \`expiredAt\` bigint NOT NULL, \`json\` text NOT NULL, \`destroyedAt\` datetime(6) NULL, \`authorId\` int NULL, INDEX \`IDX_28c5d1d16da7908c97c9bc2f74\` (\`expiredAt\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`author\` (\`id\` int NOT NULL AUTO_INCREMENT, \`color\` int NOT NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`user\` (\`id\` int NOT NULL AUTO_INCREMENT, \`username\` varchar(255) NOT NULL, \`displayName\` varchar(255) NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updatedAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`photo\` text NULL, \`email\` text NULL, UNIQUE INDEX \`IDX_78a916df40e02a9deb1c4b75ed\` (\`username\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`identity\` (\`id\` int NOT NULL AUTO_INCREMENT, \`providerType\` varchar(255) NOT NULL, \`providerName\` text NULL, \`syncSource\` tinyint NOT NULL, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updatedAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`providerUserId\` text NULL, \`oAuthAccessToken\` text NULL, \`passwordHash\` text NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`group_members_user\` (\`groupId\` int NOT NULL, \`userId\` int NOT NULL, INDEX \`IDX_bfa303089d367a2e3c02b002b8\` (\`groupId\`), INDEX \`IDX_427107c650638bcb2f1e167d2e\` (\`userId\`), PRIMARY KEY (\`groupId\`, \`userId\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`revision_tags_tag\` (\`revisionId\` int NOT NULL, \`tagId\` int NOT NULL, INDEX \`IDX_3382f45eefeb40f91e45cfd418\` (\`revisionId\`), INDEX \`IDX_19dbafe2a8b456c0ef40858d49\` (\`tagId\`), PRIMARY KEY (\`revisionId\`, \`tagId\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`revision_edits_edit\` (\`revisionId\` int NOT NULL, \`editId\` int NOT NULL, INDEX \`IDX_52c6a61e1a646768391c7854fe\` (\`revisionId\`), INDEX \`IDX_470886feb50e30114e39c42698\` (\`editId\`), PRIMARY KEY (\`revisionId\`, \`editId\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `ALTER TABLE \`auth_token\` ADD CONSTRAINT \`FK_5a326267f11b44c0d62526bc718\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`history_entry\` ADD CONSTRAINT \`FK_42b8ae461cb58747a24340e6c64\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`history_entry\` ADD CONSTRAINT \`FK_8f3595373fc9f6a32f126270422\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`media_upload\` ADD CONSTRAINT \`FK_edba6d4e0f3bcf6605772f0af6b\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`media_upload\` ADD CONSTRAINT \`FK_73ce66b082df1df2003e305e9ac\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`note_group_permission\` ADD CONSTRAINT \`FK_743ea3d9e0e26d7cbb9c174e56b\` FOREIGN KEY (\`groupId\`) REFERENCES \`group\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`note_group_permission\` ADD CONSTRAINT \`FK_fd5c9329d8b45cb160676f8d8c1\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`note_user_permission\` ADD CONSTRAINT \`FK_03cea81e07bab8864de026d517d\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`note_user_permission\` ADD CONSTRAINT \`FK_bc1ca3a87a9d662350d281a7f16\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`alias\` ADD CONSTRAINT \`FK_63012a303e6ca53144a8b7b64b0\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`note\` ADD CONSTRAINT \`FK_b09836eba01a8653c0628a78af8\` FOREIGN KEY (\`ownerId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`revision\` ADD CONSTRAINT \`FK_8ac498c7c70de43d01b94fe7905\` FOREIGN KEY (\`noteId\`) REFERENCES \`note\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`edit\` ADD CONSTRAINT \`FK_bbab22ed1a0e243b28623f4f48a\` FOREIGN KEY (\`authorId\`) REFERENCES \`author\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`session\` ADD CONSTRAINT \`FK_e5da4837ed9d236532b3215a84e\` FOREIGN KEY (\`authorId\`) REFERENCES \`author\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`author\` ADD CONSTRAINT \`FK_645811deaaaa772f9e6c2a4b927\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`identity\` ADD CONSTRAINT \`FK_12915039d2868ab654567bf5181\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`group_members_user\` ADD CONSTRAINT \`FK_bfa303089d367a2e3c02b002b8f\` FOREIGN KEY (\`groupId\`) REFERENCES \`group\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE \`group_members_user\` ADD CONSTRAINT \`FK_427107c650638bcb2f1e167d2e5\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`revision_tags_tag\` ADD CONSTRAINT \`FK_3382f45eefeb40f91e45cfd4180\` FOREIGN KEY (\`revisionId\`) REFERENCES \`revision\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE \`revision_tags_tag\` ADD CONSTRAINT \`FK_19dbafe2a8b456c0ef40858d49f\` FOREIGN KEY (\`tagId\`) REFERENCES \`tag\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`revision_edits_edit\` ADD CONSTRAINT \`FK_52c6a61e1a646768391c7854feb\` FOREIGN KEY (\`revisionId\`) REFERENCES \`revision\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE \`revision_edits_edit\` ADD CONSTRAINT \`FK_470886feb50e30114e39c426987\` FOREIGN KEY (\`editId\`) REFERENCES \`edit\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE \`revision_edits_edit\` DROP FOREIGN KEY \`FK_470886feb50e30114e39c426987\``, + ); + await queryRunner.query( + `ALTER TABLE \`revision_edits_edit\` DROP FOREIGN KEY \`FK_52c6a61e1a646768391c7854feb\``, + ); + await queryRunner.query( + `ALTER TABLE \`revision_tags_tag\` DROP FOREIGN KEY \`FK_19dbafe2a8b456c0ef40858d49f\``, + ); + await queryRunner.query( + `ALTER TABLE \`revision_tags_tag\` DROP FOREIGN KEY \`FK_3382f45eefeb40f91e45cfd4180\``, + ); + await queryRunner.query( + `ALTER TABLE \`group_members_user\` DROP FOREIGN KEY \`FK_427107c650638bcb2f1e167d2e5\``, + ); + await queryRunner.query( + `ALTER TABLE \`group_members_user\` DROP FOREIGN KEY \`FK_bfa303089d367a2e3c02b002b8f\``, + ); + await queryRunner.query( + `ALTER TABLE \`identity\` DROP FOREIGN KEY \`FK_12915039d2868ab654567bf5181\``, + ); + await queryRunner.query( + `ALTER TABLE \`author\` DROP FOREIGN KEY \`FK_645811deaaaa772f9e6c2a4b927\``, + ); + await queryRunner.query( + `ALTER TABLE \`session\` DROP FOREIGN KEY \`FK_e5da4837ed9d236532b3215a84e\``, + ); + await queryRunner.query( + `ALTER TABLE \`edit\` DROP FOREIGN KEY \`FK_bbab22ed1a0e243b28623f4f48a\``, + ); + await queryRunner.query( + `ALTER TABLE \`revision\` DROP FOREIGN KEY \`FK_8ac498c7c70de43d01b94fe7905\``, + ); + await queryRunner.query( + `ALTER TABLE \`note\` DROP FOREIGN KEY \`FK_b09836eba01a8653c0628a78af8\``, + ); + await queryRunner.query( + `ALTER TABLE \`alias\` DROP FOREIGN KEY \`FK_63012a303e6ca53144a8b7b64b0\``, + ); + await queryRunner.query( + `ALTER TABLE \`note_user_permission\` DROP FOREIGN KEY \`FK_bc1ca3a87a9d662350d281a7f16\``, + ); + await queryRunner.query( + `ALTER TABLE \`note_user_permission\` DROP FOREIGN KEY \`FK_03cea81e07bab8864de026d517d\``, + ); + await queryRunner.query( + `ALTER TABLE \`note_group_permission\` DROP FOREIGN KEY \`FK_fd5c9329d8b45cb160676f8d8c1\``, + ); + await queryRunner.query( + `ALTER TABLE \`note_group_permission\` DROP FOREIGN KEY \`FK_743ea3d9e0e26d7cbb9c174e56b\``, + ); + await queryRunner.query( + `ALTER TABLE \`media_upload\` DROP FOREIGN KEY \`FK_73ce66b082df1df2003e305e9ac\``, + ); + await queryRunner.query( + `ALTER TABLE \`media_upload\` DROP FOREIGN KEY \`FK_edba6d4e0f3bcf6605772f0af6b\``, + ); + await queryRunner.query( + `ALTER TABLE \`history_entry\` DROP FOREIGN KEY \`FK_8f3595373fc9f6a32f126270422\``, + ); + await queryRunner.query( + `ALTER TABLE \`history_entry\` DROP FOREIGN KEY \`FK_42b8ae461cb58747a24340e6c64\``, + ); + await queryRunner.query( + `ALTER TABLE \`auth_token\` DROP FOREIGN KEY \`FK_5a326267f11b44c0d62526bc718\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_470886feb50e30114e39c42698\` ON \`revision_edits_edit\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_52c6a61e1a646768391c7854fe\` ON \`revision_edits_edit\``, + ); + await queryRunner.query(`DROP TABLE \`revision_edits_edit\``); + await queryRunner.query( + `DROP INDEX \`IDX_19dbafe2a8b456c0ef40858d49\` ON \`revision_tags_tag\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_3382f45eefeb40f91e45cfd418\` ON \`revision_tags_tag\``, + ); + await queryRunner.query(`DROP TABLE \`revision_tags_tag\``); + await queryRunner.query( + `DROP INDEX \`IDX_427107c650638bcb2f1e167d2e\` ON \`group_members_user\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_bfa303089d367a2e3c02b002b8\` ON \`group_members_user\``, + ); + await queryRunner.query(`DROP TABLE \`group_members_user\``); + await queryRunner.query(`DROP TABLE \`identity\``); + await queryRunner.query( + `DROP INDEX \`IDX_78a916df40e02a9deb1c4b75ed\` ON \`user\``, + ); + await queryRunner.query(`DROP TABLE \`user\``); + await queryRunner.query(`DROP TABLE \`author\``); + await queryRunner.query( + `DROP INDEX \`IDX_28c5d1d16da7908c97c9bc2f74\` ON \`session\``, + ); + await queryRunner.query(`DROP TABLE \`session\``); + await queryRunner.query(`DROP TABLE \`edit\``); + await queryRunner.query(`DROP TABLE \`revision\``); + await queryRunner.query(`DROP TABLE \`tag\``); + await queryRunner.query(`DROP TABLE \`note\``); + await queryRunner.query( + `DROP INDEX \`Only one primary alias per note\` ON \`alias\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_89f27e45cc5c1e43abd9132c9b\` ON \`alias\``, + ); + await queryRunner.query(`DROP TABLE \`alias\``); + await queryRunner.query( + `DROP INDEX \`IDX_5a3e91233d8878f98f5ad86b71\` ON \`note_user_permission\``, + ); + await queryRunner.query(`DROP TABLE \`note_user_permission\``); + await queryRunner.query( + `DROP INDEX \`IDX_ee1744842a9ef3ffbc05a7016a\` ON \`note_group_permission\``, + ); + await queryRunner.query(`DROP TABLE \`note_group_permission\``); + await queryRunner.query( + `DROP INDEX \`IDX_8a45300fd825918f3b40195fbd\` ON \`group\``, + ); + await queryRunner.query(`DROP TABLE \`group\``); + await queryRunner.query(`DROP TABLE \`media_upload\``); + await queryRunner.query( + `DROP INDEX \`IDX_928dd947355b0837366470a916\` ON \`history_entry\``, + ); + await queryRunner.query(`DROP TABLE \`history_entry\``); + await queryRunner.query( + `DROP INDEX \`IDX_15b2228d06b08bb4cc98876c65\` ON \`auth_token\``, + ); + await queryRunner.query( + `DROP INDEX \`IDX_9d2cf0a2cc3df58b87cbbc43e4\` ON \`auth_token\``, + ); + await queryRunner.query(`DROP TABLE \`auth_token\``); + } +} diff --git a/backend/src/migrations/postgres-1696756465867-init.ts b/backend/src/migrations/postgres-1696756465867-init.ts new file mode 100644 index 000000000..aa45f25d4 --- /dev/null +++ b/backend/src/migrations/postgres-1696756465867-init.ts @@ -0,0 +1,280 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class Init1696756465867 implements MigrationInterface { + name = 'Init1696756465867'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "auth_token" ("id" SERIAL NOT NULL, "keyId" character varying NOT NULL, "label" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "accessTokenHash" character varying NOT NULL, "validUntil" TIMESTAMP NOT NULL, "lastUsedAt" date, "userId" integer, CONSTRAINT "UQ_9d2cf0a2cc3df58b87cbbc43e48" UNIQUE ("keyId"), CONSTRAINT "UQ_15b2228d06b08bb4cc98876c651" UNIQUE ("accessTokenHash"), CONSTRAINT "PK_4572ff5d1264c4a523f01aa86a0" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "history_entry" ("id" SERIAL NOT NULL, "pinStatus" boolean NOT NULL, "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "userId" integer, "noteId" integer, CONSTRAINT "PK_b65bd95b0d2929668589d57b97a" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_928dd947355b0837366470a916" ON "history_entry" ("noteId", "userId") `, + ); + await queryRunner.query( + `CREATE TABLE "media_upload" ("id" character varying NOT NULL, "backendType" character varying NOT NULL, "fileUrl" character varying NOT NULL, "backendData" text, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "noteId" integer, "userId" integer, CONSTRAINT "PK_b406d9cee56e253dfd3b3d52706" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "group" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "displayName" character varying NOT NULL, "special" boolean NOT NULL, CONSTRAINT "UQ_8a45300fd825918f3b40195fbdc" UNIQUE ("name"), CONSTRAINT "PK_256aa0fda9b1de1a73ee0b7106b" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "note_group_permission" ("id" SERIAL NOT NULL, "canEdit" boolean NOT NULL, "groupId" integer, "noteId" integer, CONSTRAINT "PK_6327989190949e6a55d02a080c3" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_ee1744842a9ef3ffbc05a7016a" ON "note_group_permission" ("groupId", "noteId") `, + ); + await queryRunner.query( + `CREATE TABLE "note_user_permission" ("id" SERIAL NOT NULL, "canEdit" boolean NOT NULL, "userId" integer, "noteId" integer, CONSTRAINT "PK_672af1e0c89837f0aa390d34b8d" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_5a3e91233d8878f98f5ad86b71" ON "note_user_permission" ("userId", "noteId") `, + ); + await queryRunner.query( + `CREATE TABLE "alias" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "primary" boolean, "noteId" integer, CONSTRAINT "UQ_89f27e45cc5c1e43abd9132c9b9" UNIQUE ("name"), CONSTRAINT "Only one primary alias per note" UNIQUE ("noteId", "primary"), CONSTRAINT "PK_b1848d04b41d10a5712fc2e673c" PRIMARY KEY ("id")); COMMENT ON COLUMN "alias"."primary" IS 'This field tells you if this is the primary alias of the note. If this field is null, that means this alias is not primary.'`, + ); + await queryRunner.query( + `CREATE TABLE "note" ("id" SERIAL NOT NULL, "publicId" text NOT NULL, "viewCount" integer NOT NULL DEFAULT '0', "version" integer NOT NULL DEFAULT '2', "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "ownerId" integer, CONSTRAINT "PK_96d0c172a4fba276b1bbed43058" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "tag" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, CONSTRAINT "PK_8e4052373c579afc1471f526760" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "revision" ("id" SERIAL NOT NULL, "patch" text NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "content" text NOT NULL, "length" integer NOT NULL, "yjsStateVector" text, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "noteId" integer, CONSTRAINT "PK_f4767cdf0c0e78041514e5a94be" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "edit" ("id" SERIAL NOT NULL, "startPos" integer NOT NULL, "endPos" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "authorId" integer, CONSTRAINT "PK_062a4b68154c101205e4595810d" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "session" ("id" character varying(255) NOT NULL, "expiredAt" bigint NOT NULL, "json" text NOT NULL, "destroyedAt" TIMESTAMP, "authorId" integer, CONSTRAINT "PK_f55da76ac1c3ac420f444d2ff11" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_28c5d1d16da7908c97c9bc2f74" ON "session" ("expiredAt") `, + ); + await queryRunner.query( + `CREATE TABLE "author" ("id" SERIAL NOT NULL, "color" integer NOT NULL, "userId" integer, CONSTRAINT "PK_5a0e79799d372fe56f2f3fa6871" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "user" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "displayName" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "photo" text, "email" text, CONSTRAINT "UQ_78a916df40e02a9deb1c4b75edb" UNIQUE ("username"), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "identity" ("id" SERIAL NOT NULL, "providerType" character varying NOT NULL, "providerName" text, "syncSource" boolean NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), "providerUserId" text, "oAuthAccessToken" text, "passwordHash" text, "userId" integer, CONSTRAINT "PK_ff16a44186b286d5e626178f726" PRIMARY KEY ("id"))`, + ); + await queryRunner.query( + `CREATE TABLE "group_members_user" ("groupId" integer NOT NULL, "userId" integer NOT NULL, CONSTRAINT "PK_7170c9a27e7b823d391d9e11f2e" PRIMARY KEY ("groupId", "userId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_bfa303089d367a2e3c02b002b8" ON "group_members_user" ("groupId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_427107c650638bcb2f1e167d2e" ON "group_members_user" ("userId") `, + ); + await queryRunner.query( + `CREATE TABLE "revision_tags_tag" ("revisionId" integer NOT NULL, "tagId" integer NOT NULL, CONSTRAINT "PK_006354d3ecad6cb1e606320647b" PRIMARY KEY ("revisionId", "tagId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_3382f45eefeb40f91e45cfd418" ON "revision_tags_tag" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_19dbafe2a8b456c0ef40858d49" ON "revision_tags_tag" ("tagId") `, + ); + await queryRunner.query( + `CREATE TABLE "revision_edits_edit" ("revisionId" integer NOT NULL, "editId" integer NOT NULL, CONSTRAINT "PK_2651ab66ea1c428e844a80d81ed" PRIMARY KEY ("revisionId", "editId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_52c6a61e1a646768391c7854fe" ON "revision_edits_edit" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_470886feb50e30114e39c42698" ON "revision_edits_edit" ("editId") `, + ); + await queryRunner.query( + `ALTER TABLE "auth_token" ADD CONSTRAINT "FK_5a326267f11b44c0d62526bc718" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "history_entry" ADD CONSTRAINT "FK_42b8ae461cb58747a24340e6c64" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "history_entry" ADD CONSTRAINT "FK_8f3595373fc9f6a32f126270422" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "media_upload" ADD CONSTRAINT "FK_edba6d4e0f3bcf6605772f0af6b" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "media_upload" ADD CONSTRAINT "FK_73ce66b082df1df2003e305e9ac" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "note_group_permission" ADD CONSTRAINT "FK_743ea3d9e0e26d7cbb9c174e56b" FOREIGN KEY ("groupId") REFERENCES "group"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "note_group_permission" ADD CONSTRAINT "FK_fd5c9329d8b45cb160676f8d8c1" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "note_user_permission" ADD CONSTRAINT "FK_03cea81e07bab8864de026d517d" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "note_user_permission" ADD CONSTRAINT "FK_bc1ca3a87a9d662350d281a7f16" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "alias" ADD CONSTRAINT "FK_63012a303e6ca53144a8b7b64b0" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "note" ADD CONSTRAINT "FK_b09836eba01a8653c0628a78af8" FOREIGN KEY ("ownerId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "revision" ADD CONSTRAINT "FK_8ac498c7c70de43d01b94fe7905" FOREIGN KEY ("noteId") REFERENCES "note"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "edit" ADD CONSTRAINT "FK_bbab22ed1a0e243b28623f4f48a" FOREIGN KEY ("authorId") REFERENCES "author"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "session" ADD CONSTRAINT "FK_e5da4837ed9d236532b3215a84e" FOREIGN KEY ("authorId") REFERENCES "author"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "author" ADD CONSTRAINT "FK_645811deaaaa772f9e6c2a4b927" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "identity" ADD CONSTRAINT "FK_12915039d2868ab654567bf5181" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "group_members_user" ADD CONSTRAINT "FK_bfa303089d367a2e3c02b002b8f" FOREIGN KEY ("groupId") REFERENCES "group"("id") ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE "group_members_user" ADD CONSTRAINT "FK_427107c650638bcb2f1e167d2e5" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "revision_tags_tag" ADD CONSTRAINT "FK_3382f45eefeb40f91e45cfd4180" FOREIGN KEY ("revisionId") REFERENCES "revision"("id") ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE "revision_tags_tag" ADD CONSTRAINT "FK_19dbafe2a8b456c0ef40858d49f" FOREIGN KEY ("tagId") REFERENCES "tag"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE "revision_edits_edit" ADD CONSTRAINT "FK_52c6a61e1a646768391c7854feb" FOREIGN KEY ("revisionId") REFERENCES "revision"("id") ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE "revision_edits_edit" ADD CONSTRAINT "FK_470886feb50e30114e39c426987" FOREIGN KEY ("editId") REFERENCES "edit"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "revision_edits_edit" DROP CONSTRAINT "FK_470886feb50e30114e39c426987"`, + ); + await queryRunner.query( + `ALTER TABLE "revision_edits_edit" DROP CONSTRAINT "FK_52c6a61e1a646768391c7854feb"`, + ); + await queryRunner.query( + `ALTER TABLE "revision_tags_tag" DROP CONSTRAINT "FK_19dbafe2a8b456c0ef40858d49f"`, + ); + await queryRunner.query( + `ALTER TABLE "revision_tags_tag" DROP CONSTRAINT "FK_3382f45eefeb40f91e45cfd4180"`, + ); + await queryRunner.query( + `ALTER TABLE "group_members_user" DROP CONSTRAINT "FK_427107c650638bcb2f1e167d2e5"`, + ); + await queryRunner.query( + `ALTER TABLE "group_members_user" DROP CONSTRAINT "FK_bfa303089d367a2e3c02b002b8f"`, + ); + await queryRunner.query( + `ALTER TABLE "identity" DROP CONSTRAINT "FK_12915039d2868ab654567bf5181"`, + ); + await queryRunner.query( + `ALTER TABLE "author" DROP CONSTRAINT "FK_645811deaaaa772f9e6c2a4b927"`, + ); + await queryRunner.query( + `ALTER TABLE "session" DROP CONSTRAINT "FK_e5da4837ed9d236532b3215a84e"`, + ); + await queryRunner.query( + `ALTER TABLE "edit" DROP CONSTRAINT "FK_bbab22ed1a0e243b28623f4f48a"`, + ); + await queryRunner.query( + `ALTER TABLE "revision" DROP CONSTRAINT "FK_8ac498c7c70de43d01b94fe7905"`, + ); + await queryRunner.query( + `ALTER TABLE "note" DROP CONSTRAINT "FK_b09836eba01a8653c0628a78af8"`, + ); + await queryRunner.query( + `ALTER TABLE "alias" DROP CONSTRAINT "FK_63012a303e6ca53144a8b7b64b0"`, + ); + await queryRunner.query( + `ALTER TABLE "note_user_permission" DROP CONSTRAINT "FK_bc1ca3a87a9d662350d281a7f16"`, + ); + await queryRunner.query( + `ALTER TABLE "note_user_permission" DROP CONSTRAINT "FK_03cea81e07bab8864de026d517d"`, + ); + await queryRunner.query( + `ALTER TABLE "note_group_permission" DROP CONSTRAINT "FK_fd5c9329d8b45cb160676f8d8c1"`, + ); + await queryRunner.query( + `ALTER TABLE "note_group_permission" DROP CONSTRAINT "FK_743ea3d9e0e26d7cbb9c174e56b"`, + ); + await queryRunner.query( + `ALTER TABLE "media_upload" DROP CONSTRAINT "FK_73ce66b082df1df2003e305e9ac"`, + ); + await queryRunner.query( + `ALTER TABLE "media_upload" DROP CONSTRAINT "FK_edba6d4e0f3bcf6605772f0af6b"`, + ); + await queryRunner.query( + `ALTER TABLE "history_entry" DROP CONSTRAINT "FK_8f3595373fc9f6a32f126270422"`, + ); + await queryRunner.query( + `ALTER TABLE "history_entry" DROP CONSTRAINT "FK_42b8ae461cb58747a24340e6c64"`, + ); + await queryRunner.query( + `ALTER TABLE "auth_token" DROP CONSTRAINT "FK_5a326267f11b44c0d62526bc718"`, + ); + await queryRunner.query( + `DROP INDEX "public"."IDX_470886feb50e30114e39c42698"`, + ); + await queryRunner.query( + `DROP INDEX "public"."IDX_52c6a61e1a646768391c7854fe"`, + ); + await queryRunner.query(`DROP TABLE "revision_edits_edit"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_19dbafe2a8b456c0ef40858d49"`, + ); + await queryRunner.query( + `DROP INDEX "public"."IDX_3382f45eefeb40f91e45cfd418"`, + ); + await queryRunner.query(`DROP TABLE "revision_tags_tag"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_427107c650638bcb2f1e167d2e"`, + ); + await queryRunner.query( + `DROP INDEX "public"."IDX_bfa303089d367a2e3c02b002b8"`, + ); + await queryRunner.query(`DROP TABLE "group_members_user"`); + await queryRunner.query(`DROP TABLE "identity"`); + await queryRunner.query(`DROP TABLE "user"`); + await queryRunner.query(`DROP TABLE "author"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_28c5d1d16da7908c97c9bc2f74"`, + ); + await queryRunner.query(`DROP TABLE "session"`); + await queryRunner.query(`DROP TABLE "edit"`); + await queryRunner.query(`DROP TABLE "revision"`); + await queryRunner.query(`DROP TABLE "tag"`); + await queryRunner.query(`DROP TABLE "note"`); + await queryRunner.query(`DROP TABLE "alias"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_5a3e91233d8878f98f5ad86b71"`, + ); + await queryRunner.query(`DROP TABLE "note_user_permission"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_ee1744842a9ef3ffbc05a7016a"`, + ); + await queryRunner.query(`DROP TABLE "note_group_permission"`); + await queryRunner.query(`DROP TABLE "group"`); + await queryRunner.query(`DROP TABLE "media_upload"`); + await queryRunner.query( + `DROP INDEX "public"."IDX_928dd947355b0837366470a916"`, + ); + await queryRunner.query(`DROP TABLE "history_entry"`); + await queryRunner.query(`DROP TABLE "auth_token"`); + } +} diff --git a/backend/src/migrations/sqlite-1696756465867-init.ts b/backend/src/migrations/sqlite-1696756465867-init.ts new file mode 100644 index 000000000..195cd9001 --- /dev/null +++ b/backend/src/migrations/sqlite-1696756465867-init.ts @@ -0,0 +1,496 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class Init1696756465867 implements MigrationInterface { + name = 'Init1696756465867'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE "auth_token" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "keyId" varchar NOT NULL, "label" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "accessTokenHash" varchar NOT NULL, "validUntil" datetime NOT NULL, "lastUsedAt" date, "userId" integer, CONSTRAINT "UQ_9d2cf0a2cc3df58b87cbbc43e48" UNIQUE ("keyId"), CONSTRAINT "UQ_15b2228d06b08bb4cc98876c651" UNIQUE ("accessTokenHash"))`, + ); + await queryRunner.query( + `CREATE TABLE "history_entry" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "pinStatus" boolean NOT NULL, "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "userId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_928dd947355b0837366470a916" ON "history_entry" ("noteId", "userId") `, + ); + await queryRunner.query( + `CREATE TABLE "media_upload" ("id" varchar PRIMARY KEY NOT NULL, "backendType" varchar NOT NULL, "fileUrl" varchar NOT NULL, "backendData" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer, "userId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "group" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "displayName" varchar NOT NULL, "special" boolean NOT NULL, CONSTRAINT "UQ_8a45300fd825918f3b40195fbdc" UNIQUE ("name"))`, + ); + await queryRunner.query( + `CREATE TABLE "note_group_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "groupId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_ee1744842a9ef3ffbc05a7016a" ON "note_group_permission" ("groupId", "noteId") `, + ); + await queryRunner.query( + `CREATE TABLE "note_user_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "userId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_5a3e91233d8878f98f5ad86b71" ON "note_user_permission" ("userId", "noteId") `, + ); + await queryRunner.query( + `CREATE TABLE "alias" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "primary" boolean, "noteId" integer, CONSTRAINT "UQ_89f27e45cc5c1e43abd9132c9b9" UNIQUE ("name"), CONSTRAINT "Only one primary alias per note" UNIQUE ("noteId", "primary"))`, + ); + await queryRunner.query( + `CREATE TABLE "note" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "publicId" text NOT NULL, "viewCount" integer NOT NULL DEFAULT (0), "version" integer NOT NULL DEFAULT (2), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "ownerId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "tag" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL)`, + ); + await queryRunner.query( + `CREATE TABLE "revision" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "patch" text NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "content" text NOT NULL, "length" integer NOT NULL, "yjsStateVector" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "edit" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "startPos" integer NOT NULL, "endPos" integer NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "authorId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "session" ("id" varchar(255) PRIMARY KEY NOT NULL, "expiredAt" bigint NOT NULL, "json" text NOT NULL, "destroyedAt" datetime, "authorId" integer)`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_28c5d1d16da7908c97c9bc2f74" ON "session" ("expiredAt") `, + ); + await queryRunner.query( + `CREATE TABLE "author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "color" integer NOT NULL, "userId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "user" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "username" varchar NOT NULL, "displayName" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "photo" text, "email" text, CONSTRAINT "UQ_78a916df40e02a9deb1c4b75edb" UNIQUE ("username"))`, + ); + await queryRunner.query( + `CREATE TABLE "identity" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "providerType" varchar NOT NULL, "providerName" text, "syncSource" boolean NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "providerUserId" text, "oAuthAccessToken" text, "passwordHash" text, "userId" integer)`, + ); + await queryRunner.query( + `CREATE TABLE "group_members_user" ("groupId" integer NOT NULL, "userId" integer NOT NULL, PRIMARY KEY ("groupId", "userId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_bfa303089d367a2e3c02b002b8" ON "group_members_user" ("groupId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_427107c650638bcb2f1e167d2e" ON "group_members_user" ("userId") `, + ); + await queryRunner.query( + `CREATE TABLE "revision_tags_tag" ("revisionId" integer NOT NULL, "tagId" integer NOT NULL, PRIMARY KEY ("revisionId", "tagId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_3382f45eefeb40f91e45cfd418" ON "revision_tags_tag" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_19dbafe2a8b456c0ef40858d49" ON "revision_tags_tag" ("tagId") `, + ); + await queryRunner.query( + `CREATE TABLE "revision_edits_edit" ("revisionId" integer NOT NULL, "editId" integer NOT NULL, PRIMARY KEY ("revisionId", "editId"))`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_52c6a61e1a646768391c7854fe" ON "revision_edits_edit" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_470886feb50e30114e39c42698" ON "revision_edits_edit" ("editId") `, + ); + await queryRunner.query( + `CREATE TABLE "temporary_auth_token" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "keyId" varchar NOT NULL, "label" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "accessTokenHash" varchar NOT NULL, "validUntil" datetime NOT NULL, "lastUsedAt" date, "userId" integer, CONSTRAINT "UQ_9d2cf0a2cc3df58b87cbbc43e48" UNIQUE ("keyId"), CONSTRAINT "UQ_15b2228d06b08bb4cc98876c651" UNIQUE ("accessTokenHash"), CONSTRAINT "FK_5a326267f11b44c0d62526bc718" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_auth_token"("id", "keyId", "label", "createdAt", "accessTokenHash", "validUntil", "lastUsedAt", "userId") SELECT "id", "keyId", "label", "createdAt", "accessTokenHash", "validUntil", "lastUsedAt", "userId" FROM "auth_token"`, + ); + await queryRunner.query(`DROP TABLE "auth_token"`); + await queryRunner.query( + `ALTER TABLE "temporary_auth_token" RENAME TO "auth_token"`, + ); + await queryRunner.query(`DROP INDEX "IDX_928dd947355b0837366470a916"`); + await queryRunner.query( + `CREATE TABLE "temporary_history_entry" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "pinStatus" boolean NOT NULL, "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "userId" integer, "noteId" integer, CONSTRAINT "FK_42b8ae461cb58747a24340e6c64" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_8f3595373fc9f6a32f126270422" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_history_entry"("id", "pinStatus", "updatedAt", "userId", "noteId") SELECT "id", "pinStatus", "updatedAt", "userId", "noteId" FROM "history_entry"`, + ); + await queryRunner.query(`DROP TABLE "history_entry"`); + await queryRunner.query( + `ALTER TABLE "temporary_history_entry" RENAME TO "history_entry"`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_928dd947355b0837366470a916" ON "history_entry" ("noteId", "userId") `, + ); + await queryRunner.query( + `CREATE TABLE "temporary_media_upload" ("id" varchar PRIMARY KEY NOT NULL, "backendType" varchar NOT NULL, "fileUrl" varchar NOT NULL, "backendData" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer, "userId" integer, CONSTRAINT "FK_edba6d4e0f3bcf6605772f0af6b" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT "FK_73ce66b082df1df2003e305e9ac" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_media_upload"("id", "backendType", "fileUrl", "backendData", "createdAt", "noteId", "userId") SELECT "id", "backendType", "fileUrl", "backendData", "createdAt", "noteId", "userId" FROM "media_upload"`, + ); + await queryRunner.query(`DROP TABLE "media_upload"`); + await queryRunner.query( + `ALTER TABLE "temporary_media_upload" RENAME TO "media_upload"`, + ); + await queryRunner.query(`DROP INDEX "IDX_ee1744842a9ef3ffbc05a7016a"`); + await queryRunner.query( + `CREATE TABLE "temporary_note_group_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "groupId" integer, "noteId" integer, CONSTRAINT "FK_743ea3d9e0e26d7cbb9c174e56b" FOREIGN KEY ("groupId") REFERENCES "group" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_fd5c9329d8b45cb160676f8d8c1" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_note_group_permission"("id", "canEdit", "groupId", "noteId") SELECT "id", "canEdit", "groupId", "noteId" FROM "note_group_permission"`, + ); + await queryRunner.query(`DROP TABLE "note_group_permission"`); + await queryRunner.query( + `ALTER TABLE "temporary_note_group_permission" RENAME TO "note_group_permission"`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_ee1744842a9ef3ffbc05a7016a" ON "note_group_permission" ("groupId", "noteId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_5a3e91233d8878f98f5ad86b71"`); + await queryRunner.query( + `CREATE TABLE "temporary_note_user_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "userId" integer, "noteId" integer, CONSTRAINT "FK_03cea81e07bab8864de026d517d" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION, CONSTRAINT "FK_bc1ca3a87a9d662350d281a7f16" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_note_user_permission"("id", "canEdit", "userId", "noteId") SELECT "id", "canEdit", "userId", "noteId" FROM "note_user_permission"`, + ); + await queryRunner.query(`DROP TABLE "note_user_permission"`); + await queryRunner.query( + `ALTER TABLE "temporary_note_user_permission" RENAME TO "note_user_permission"`, + ); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_5a3e91233d8878f98f5ad86b71" ON "note_user_permission" ("userId", "noteId") `, + ); + await queryRunner.query( + `CREATE TABLE "temporary_alias" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "primary" boolean, "noteId" integer, CONSTRAINT "UQ_89f27e45cc5c1e43abd9132c9b9" UNIQUE ("name"), CONSTRAINT "Only one primary alias per note" UNIQUE ("noteId", "primary"), CONSTRAINT "FK_63012a303e6ca53144a8b7b64b0" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_alias"("id", "name", "primary", "noteId") SELECT "id", "name", "primary", "noteId" FROM "alias"`, + ); + await queryRunner.query(`DROP TABLE "alias"`); + await queryRunner.query(`ALTER TABLE "temporary_alias" RENAME TO "alias"`); + await queryRunner.query( + `CREATE TABLE "temporary_note" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "publicId" text NOT NULL, "viewCount" integer NOT NULL DEFAULT (0), "version" integer NOT NULL DEFAULT (2), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "ownerId" integer, CONSTRAINT "FK_b09836eba01a8653c0628a78af8" FOREIGN KEY ("ownerId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_note"("id", "publicId", "viewCount", "version", "createdAt", "ownerId") SELECT "id", "publicId", "viewCount", "version", "createdAt", "ownerId" FROM "note"`, + ); + await queryRunner.query(`DROP TABLE "note"`); + await queryRunner.query(`ALTER TABLE "temporary_note" RENAME TO "note"`); + await queryRunner.query( + `CREATE TABLE "temporary_revision" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "patch" text NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "content" text NOT NULL, "length" integer NOT NULL, "yjsStateVector" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer, CONSTRAINT "FK_8ac498c7c70de43d01b94fe7905" FOREIGN KEY ("noteId") REFERENCES "note" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_revision"("id", "patch", "title", "description", "content", "length", "yjsStateVector", "createdAt", "noteId") SELECT "id", "patch", "title", "description", "content", "length", "yjsStateVector", "createdAt", "noteId" FROM "revision"`, + ); + await queryRunner.query(`DROP TABLE "revision"`); + await queryRunner.query( + `ALTER TABLE "temporary_revision" RENAME TO "revision"`, + ); + await queryRunner.query( + `CREATE TABLE "temporary_edit" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "startPos" integer NOT NULL, "endPos" integer NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "authorId" integer, CONSTRAINT "FK_bbab22ed1a0e243b28623f4f48a" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_edit"("id", "startPos", "endPos", "createdAt", "updatedAt", "authorId") SELECT "id", "startPos", "endPos", "createdAt", "updatedAt", "authorId" FROM "edit"`, + ); + await queryRunner.query(`DROP TABLE "edit"`); + await queryRunner.query(`ALTER TABLE "temporary_edit" RENAME TO "edit"`); + await queryRunner.query(`DROP INDEX "IDX_28c5d1d16da7908c97c9bc2f74"`); + await queryRunner.query( + `CREATE TABLE "temporary_session" ("id" varchar(255) PRIMARY KEY NOT NULL, "expiredAt" bigint NOT NULL, "json" text NOT NULL, "destroyedAt" datetime, "authorId" integer, CONSTRAINT "FK_e5da4837ed9d236532b3215a84e" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_session"("id", "expiredAt", "json", "destroyedAt", "authorId") SELECT "id", "expiredAt", "json", "destroyedAt", "authorId" FROM "session"`, + ); + await queryRunner.query(`DROP TABLE "session"`); + await queryRunner.query( + `ALTER TABLE "temporary_session" RENAME TO "session"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_28c5d1d16da7908c97c9bc2f74" ON "session" ("expiredAt") `, + ); + await queryRunner.query( + `CREATE TABLE "temporary_author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "color" integer NOT NULL, "userId" integer, CONSTRAINT "FK_645811deaaaa772f9e6c2a4b927" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_author"("id", "color", "userId") SELECT "id", "color", "userId" FROM "author"`, + ); + await queryRunner.query(`DROP TABLE "author"`); + await queryRunner.query( + `ALTER TABLE "temporary_author" RENAME TO "author"`, + ); + await queryRunner.query( + `CREATE TABLE "temporary_identity" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "providerType" varchar NOT NULL, "providerName" text, "syncSource" boolean NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "providerUserId" text, "oAuthAccessToken" text, "passwordHash" text, "userId" integer, CONSTRAINT "FK_12915039d2868ab654567bf5181" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE CASCADE ON UPDATE NO ACTION)`, + ); + await queryRunner.query( + `INSERT INTO "temporary_identity"("id", "providerType", "providerName", "syncSource", "createdAt", "updatedAt", "providerUserId", "oAuthAccessToken", "passwordHash", "userId") SELECT "id", "providerType", "providerName", "syncSource", "createdAt", "updatedAt", "providerUserId", "oAuthAccessToken", "passwordHash", "userId" FROM "identity"`, + ); + await queryRunner.query(`DROP TABLE "identity"`); + await queryRunner.query( + `ALTER TABLE "temporary_identity" RENAME TO "identity"`, + ); + await queryRunner.query(`DROP INDEX "IDX_bfa303089d367a2e3c02b002b8"`); + await queryRunner.query(`DROP INDEX "IDX_427107c650638bcb2f1e167d2e"`); + await queryRunner.query( + `CREATE TABLE "temporary_group_members_user" ("groupId" integer NOT NULL, "userId" integer NOT NULL, CONSTRAINT "FK_bfa303089d367a2e3c02b002b8f" FOREIGN KEY ("groupId") REFERENCES "group" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_427107c650638bcb2f1e167d2e5" FOREIGN KEY ("userId") REFERENCES "user" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION, PRIMARY KEY ("groupId", "userId"))`, + ); + await queryRunner.query( + `INSERT INTO "temporary_group_members_user"("groupId", "userId") SELECT "groupId", "userId" FROM "group_members_user"`, + ); + await queryRunner.query(`DROP TABLE "group_members_user"`); + await queryRunner.query( + `ALTER TABLE "temporary_group_members_user" RENAME TO "group_members_user"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_bfa303089d367a2e3c02b002b8" ON "group_members_user" ("groupId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_427107c650638bcb2f1e167d2e" ON "group_members_user" ("userId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_3382f45eefeb40f91e45cfd418"`); + await queryRunner.query(`DROP INDEX "IDX_19dbafe2a8b456c0ef40858d49"`); + await queryRunner.query( + `CREATE TABLE "temporary_revision_tags_tag" ("revisionId" integer NOT NULL, "tagId" integer NOT NULL, CONSTRAINT "FK_3382f45eefeb40f91e45cfd4180" FOREIGN KEY ("revisionId") REFERENCES "revision" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_19dbafe2a8b456c0ef40858d49f" FOREIGN KEY ("tagId") REFERENCES "tag" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION, PRIMARY KEY ("revisionId", "tagId"))`, + ); + await queryRunner.query( + `INSERT INTO "temporary_revision_tags_tag"("revisionId", "tagId") SELECT "revisionId", "tagId" FROM "revision_tags_tag"`, + ); + await queryRunner.query(`DROP TABLE "revision_tags_tag"`); + await queryRunner.query( + `ALTER TABLE "temporary_revision_tags_tag" RENAME TO "revision_tags_tag"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_3382f45eefeb40f91e45cfd418" ON "revision_tags_tag" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_19dbafe2a8b456c0ef40858d49" ON "revision_tags_tag" ("tagId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_52c6a61e1a646768391c7854fe"`); + await queryRunner.query(`DROP INDEX "IDX_470886feb50e30114e39c42698"`); + await queryRunner.query( + `CREATE TABLE "temporary_revision_edits_edit" ("revisionId" integer NOT NULL, "editId" integer NOT NULL, CONSTRAINT "FK_52c6a61e1a646768391c7854feb" FOREIGN KEY ("revisionId") REFERENCES "revision" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_470886feb50e30114e39c426987" FOREIGN KEY ("editId") REFERENCES "edit" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION, PRIMARY KEY ("revisionId", "editId"))`, + ); + await queryRunner.query( + `INSERT INTO "temporary_revision_edits_edit"("revisionId", "editId") SELECT "revisionId", "editId" FROM "revision_edits_edit"`, + ); + await queryRunner.query(`DROP TABLE "revision_edits_edit"`); + await queryRunner.query( + `ALTER TABLE "temporary_revision_edits_edit" RENAME TO "revision_edits_edit"`, + ); + await queryRunner.query( + `CREATE INDEX "IDX_52c6a61e1a646768391c7854fe" ON "revision_edits_edit" ("revisionId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_470886feb50e30114e39c42698" ON "revision_edits_edit" ("editId") `, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP INDEX "IDX_470886feb50e30114e39c42698"`); + await queryRunner.query(`DROP INDEX "IDX_52c6a61e1a646768391c7854fe"`); + await queryRunner.query( + `ALTER TABLE "revision_edits_edit" RENAME TO "temporary_revision_edits_edit"`, + ); + await queryRunner.query( + `CREATE TABLE "revision_edits_edit" ("revisionId" integer NOT NULL, "editId" integer NOT NULL, PRIMARY KEY ("revisionId", "editId"))`, + ); + await queryRunner.query( + `INSERT INTO "revision_edits_edit"("revisionId", "editId") SELECT "revisionId", "editId" FROM "temporary_revision_edits_edit"`, + ); + await queryRunner.query(`DROP TABLE "temporary_revision_edits_edit"`); + await queryRunner.query( + `CREATE INDEX "IDX_470886feb50e30114e39c42698" ON "revision_edits_edit" ("editId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_52c6a61e1a646768391c7854fe" ON "revision_edits_edit" ("revisionId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_19dbafe2a8b456c0ef40858d49"`); + await queryRunner.query(`DROP INDEX "IDX_3382f45eefeb40f91e45cfd418"`); + await queryRunner.query( + `ALTER TABLE "revision_tags_tag" RENAME TO "temporary_revision_tags_tag"`, + ); + await queryRunner.query( + `CREATE TABLE "revision_tags_tag" ("revisionId" integer NOT NULL, "tagId" integer NOT NULL, PRIMARY KEY ("revisionId", "tagId"))`, + ); + await queryRunner.query( + `INSERT INTO "revision_tags_tag"("revisionId", "tagId") SELECT "revisionId", "tagId" FROM "temporary_revision_tags_tag"`, + ); + await queryRunner.query(`DROP TABLE "temporary_revision_tags_tag"`); + await queryRunner.query( + `CREATE INDEX "IDX_19dbafe2a8b456c0ef40858d49" ON "revision_tags_tag" ("tagId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_3382f45eefeb40f91e45cfd418" ON "revision_tags_tag" ("revisionId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_427107c650638bcb2f1e167d2e"`); + await queryRunner.query(`DROP INDEX "IDX_bfa303089d367a2e3c02b002b8"`); + await queryRunner.query( + `ALTER TABLE "group_members_user" RENAME TO "temporary_group_members_user"`, + ); + await queryRunner.query( + `CREATE TABLE "group_members_user" ("groupId" integer NOT NULL, "userId" integer NOT NULL, PRIMARY KEY ("groupId", "userId"))`, + ); + await queryRunner.query( + `INSERT INTO "group_members_user"("groupId", "userId") SELECT "groupId", "userId" FROM "temporary_group_members_user"`, + ); + await queryRunner.query(`DROP TABLE "temporary_group_members_user"`); + await queryRunner.query( + `CREATE INDEX "IDX_427107c650638bcb2f1e167d2e" ON "group_members_user" ("userId") `, + ); + await queryRunner.query( + `CREATE INDEX "IDX_bfa303089d367a2e3c02b002b8" ON "group_members_user" ("groupId") `, + ); + await queryRunner.query( + `ALTER TABLE "identity" RENAME TO "temporary_identity"`, + ); + await queryRunner.query( + `CREATE TABLE "identity" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "providerType" varchar NOT NULL, "providerName" text, "syncSource" boolean NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "providerUserId" text, "oAuthAccessToken" text, "passwordHash" text, "userId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "identity"("id", "providerType", "providerName", "syncSource", "createdAt", "updatedAt", "providerUserId", "oAuthAccessToken", "passwordHash", "userId") SELECT "id", "providerType", "providerName", "syncSource", "createdAt", "updatedAt", "providerUserId", "oAuthAccessToken", "passwordHash", "userId" FROM "temporary_identity"`, + ); + await queryRunner.query(`DROP TABLE "temporary_identity"`); + await queryRunner.query( + `ALTER TABLE "author" RENAME TO "temporary_author"`, + ); + await queryRunner.query( + `CREATE TABLE "author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "color" integer NOT NULL, "userId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "author"("id", "color", "userId") SELECT "id", "color", "userId" FROM "temporary_author"`, + ); + await queryRunner.query(`DROP TABLE "temporary_author"`); + await queryRunner.query(`DROP INDEX "IDX_28c5d1d16da7908c97c9bc2f74"`); + await queryRunner.query( + `ALTER TABLE "session" RENAME TO "temporary_session"`, + ); + await queryRunner.query( + `CREATE TABLE "session" ("id" varchar(255) PRIMARY KEY NOT NULL, "expiredAt" bigint NOT NULL, "json" text NOT NULL, "destroyedAt" datetime, "authorId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "session"("id", "expiredAt", "json", "destroyedAt", "authorId") SELECT "id", "expiredAt", "json", "destroyedAt", "authorId" FROM "temporary_session"`, + ); + await queryRunner.query(`DROP TABLE "temporary_session"`); + await queryRunner.query( + `CREATE INDEX "IDX_28c5d1d16da7908c97c9bc2f74" ON "session" ("expiredAt") `, + ); + await queryRunner.query(`ALTER TABLE "edit" RENAME TO "temporary_edit"`); + await queryRunner.query( + `CREATE TABLE "edit" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "startPos" integer NOT NULL, "endPos" integer NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "authorId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "edit"("id", "startPos", "endPos", "createdAt", "updatedAt", "authorId") SELECT "id", "startPos", "endPos", "createdAt", "updatedAt", "authorId" FROM "temporary_edit"`, + ); + await queryRunner.query(`DROP TABLE "temporary_edit"`); + await queryRunner.query( + `ALTER TABLE "revision" RENAME TO "temporary_revision"`, + ); + await queryRunner.query( + `CREATE TABLE "revision" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "patch" text NOT NULL, "title" text NOT NULL, "description" text NOT NULL, "content" text NOT NULL, "length" integer NOT NULL, "yjsStateVector" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "revision"("id", "patch", "title", "description", "content", "length", "yjsStateVector", "createdAt", "noteId") SELECT "id", "patch", "title", "description", "content", "length", "yjsStateVector", "createdAt", "noteId" FROM "temporary_revision"`, + ); + await queryRunner.query(`DROP TABLE "temporary_revision"`); + await queryRunner.query(`ALTER TABLE "note" RENAME TO "temporary_note"`); + await queryRunner.query( + `CREATE TABLE "note" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "publicId" text NOT NULL, "viewCount" integer NOT NULL DEFAULT (0), "version" integer NOT NULL DEFAULT (2), "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "ownerId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "note"("id", "publicId", "viewCount", "version", "createdAt", "ownerId") SELECT "id", "publicId", "viewCount", "version", "createdAt", "ownerId" FROM "temporary_note"`, + ); + await queryRunner.query(`DROP TABLE "temporary_note"`); + await queryRunner.query(`ALTER TABLE "alias" RENAME TO "temporary_alias"`); + await queryRunner.query( + `CREATE TABLE "alias" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "primary" boolean, "noteId" integer, CONSTRAINT "UQ_89f27e45cc5c1e43abd9132c9b9" UNIQUE ("name"), CONSTRAINT "Only one primary alias per note" UNIQUE ("noteId", "primary"))`, + ); + await queryRunner.query( + `INSERT INTO "alias"("id", "name", "primary", "noteId") SELECT "id", "name", "primary", "noteId" FROM "temporary_alias"`, + ); + await queryRunner.query(`DROP TABLE "temporary_alias"`); + await queryRunner.query(`DROP INDEX "IDX_5a3e91233d8878f98f5ad86b71"`); + await queryRunner.query( + `ALTER TABLE "note_user_permission" RENAME TO "temporary_note_user_permission"`, + ); + await queryRunner.query( + `CREATE TABLE "note_user_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "userId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "note_user_permission"("id", "canEdit", "userId", "noteId") SELECT "id", "canEdit", "userId", "noteId" FROM "temporary_note_user_permission"`, + ); + await queryRunner.query(`DROP TABLE "temporary_note_user_permission"`); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_5a3e91233d8878f98f5ad86b71" ON "note_user_permission" ("userId", "noteId") `, + ); + await queryRunner.query(`DROP INDEX "IDX_ee1744842a9ef3ffbc05a7016a"`); + await queryRunner.query( + `ALTER TABLE "note_group_permission" RENAME TO "temporary_note_group_permission"`, + ); + await queryRunner.query( + `CREATE TABLE "note_group_permission" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "canEdit" boolean NOT NULL, "groupId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "note_group_permission"("id", "canEdit", "groupId", "noteId") SELECT "id", "canEdit", "groupId", "noteId" FROM "temporary_note_group_permission"`, + ); + await queryRunner.query(`DROP TABLE "temporary_note_group_permission"`); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_ee1744842a9ef3ffbc05a7016a" ON "note_group_permission" ("groupId", "noteId") `, + ); + await queryRunner.query( + `ALTER TABLE "media_upload" RENAME TO "temporary_media_upload"`, + ); + await queryRunner.query( + `CREATE TABLE "media_upload" ("id" varchar PRIMARY KEY NOT NULL, "backendType" varchar NOT NULL, "fileUrl" varchar NOT NULL, "backendData" text, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "noteId" integer, "userId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "media_upload"("id", "backendType", "fileUrl", "backendData", "createdAt", "noteId", "userId") SELECT "id", "backendType", "fileUrl", "backendData", "createdAt", "noteId", "userId" FROM "temporary_media_upload"`, + ); + await queryRunner.query(`DROP TABLE "temporary_media_upload"`); + await queryRunner.query(`DROP INDEX "IDX_928dd947355b0837366470a916"`); + await queryRunner.query( + `ALTER TABLE "history_entry" RENAME TO "temporary_history_entry"`, + ); + await queryRunner.query( + `CREATE TABLE "history_entry" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "pinStatus" boolean NOT NULL, "updatedAt" datetime NOT NULL DEFAULT (datetime('now')), "userId" integer, "noteId" integer)`, + ); + await queryRunner.query( + `INSERT INTO "history_entry"("id", "pinStatus", "updatedAt", "userId", "noteId") SELECT "id", "pinStatus", "updatedAt", "userId", "noteId" FROM "temporary_history_entry"`, + ); + await queryRunner.query(`DROP TABLE "temporary_history_entry"`); + await queryRunner.query( + `CREATE UNIQUE INDEX "IDX_928dd947355b0837366470a916" ON "history_entry" ("noteId", "userId") `, + ); + await queryRunner.query( + `ALTER TABLE "auth_token" RENAME TO "temporary_auth_token"`, + ); + await queryRunner.query( + `CREATE TABLE "auth_token" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "keyId" varchar NOT NULL, "label" varchar NOT NULL, "createdAt" datetime NOT NULL DEFAULT (datetime('now')), "accessTokenHash" varchar NOT NULL, "validUntil" datetime NOT NULL, "lastUsedAt" date, "userId" integer, CONSTRAINT "UQ_9d2cf0a2cc3df58b87cbbc43e48" UNIQUE ("keyId"), CONSTRAINT "UQ_15b2228d06b08bb4cc98876c651" UNIQUE ("accessTokenHash"))`, + ); + await queryRunner.query( + `INSERT INTO "auth_token"("id", "keyId", "label", "createdAt", "accessTokenHash", "validUntil", "lastUsedAt", "userId") SELECT "id", "keyId", "label", "createdAt", "accessTokenHash", "validUntil", "lastUsedAt", "userId" FROM "temporary_auth_token"`, + ); + await queryRunner.query(`DROP TABLE "temporary_auth_token"`); + await queryRunner.query(`DROP INDEX "IDX_470886feb50e30114e39c42698"`); + await queryRunner.query(`DROP INDEX "IDX_52c6a61e1a646768391c7854fe"`); + await queryRunner.query(`DROP TABLE "revision_edits_edit"`); + await queryRunner.query(`DROP INDEX "IDX_19dbafe2a8b456c0ef40858d49"`); + await queryRunner.query(`DROP INDEX "IDX_3382f45eefeb40f91e45cfd418"`); + await queryRunner.query(`DROP TABLE "revision_tags_tag"`); + await queryRunner.query(`DROP INDEX "IDX_427107c650638bcb2f1e167d2e"`); + await queryRunner.query(`DROP INDEX "IDX_bfa303089d367a2e3c02b002b8"`); + await queryRunner.query(`DROP TABLE "group_members_user"`); + await queryRunner.query(`DROP TABLE "identity"`); + await queryRunner.query(`DROP TABLE "user"`); + await queryRunner.query(`DROP TABLE "author"`); + await queryRunner.query(`DROP INDEX "IDX_28c5d1d16da7908c97c9bc2f74"`); + await queryRunner.query(`DROP TABLE "session"`); + await queryRunner.query(`DROP TABLE "edit"`); + await queryRunner.query(`DROP TABLE "revision"`); + await queryRunner.query(`DROP TABLE "tag"`); + await queryRunner.query(`DROP TABLE "note"`); + await queryRunner.query(`DROP TABLE "alias"`); + await queryRunner.query(`DROP INDEX "IDX_5a3e91233d8878f98f5ad86b71"`); + await queryRunner.query(`DROP TABLE "note_user_permission"`); + await queryRunner.query(`DROP INDEX "IDX_ee1744842a9ef3ffbc05a7016a"`); + await queryRunner.query(`DROP TABLE "note_group_permission"`); + await queryRunner.query(`DROP TABLE "group"`); + await queryRunner.query(`DROP TABLE "media_upload"`); + await queryRunner.query(`DROP INDEX "IDX_928dd947355b0837366470a916"`); + await queryRunner.query(`DROP TABLE "history_entry"`); + await queryRunner.query(`DROP TABLE "auth_token"`); + } +} diff --git a/backend/src/ormconfig.ts b/backend/src/ormconfig.ts new file mode 100644 index 000000000..9ef531b20 --- /dev/null +++ b/backend/src/ormconfig.ts @@ -0,0 +1,28 @@ +/* + * SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file) + * + * SPDX-License-Identifier: AGPL-3.0-only + */ +import { NestFactory } from '@nestjs/core'; +import { NestExpressApplication } from '@nestjs/platform-express'; +import { DataSource } from 'typeorm'; + +import { AppModule } from './app.module'; +import { AppConfig } from './config/app.config'; +import { Loglevel } from './config/loglevel.enum'; +import { ConsoleLoggerService } from './logger/console-logger.service'; + +async function buildDataSource(): Promise { + // We create a new app instance to let it discover entities + const app = await NestFactory.create(AppModule, { + logger: new ConsoleLoggerService({ loglevel: Loglevel.TRACE } as AppConfig), + }); + const dataSource = app.get(DataSource); + + // The migration CLI does not want an existing connection + await dataSource.destroy(); + + return dataSource; +} + +export default buildDataSource(); diff --git a/backend/test/test-setup.ts b/backend/test/test-setup.ts index 2c7d0bb85..adabfbcfd 100644 --- a/backend/test/test-setup.ts +++ b/backend/test/test-setup.ts @@ -200,8 +200,9 @@ export class TestSetupBuilder { type: 'sqlite', database: ':memory:', autoLoadEntities: true, - synchronize: true, dropSchema: true, + migrations: [`src/migrations/sqlite-*.ts`], + migrationsRun: true, }; case 'postgres': case 'mariadb': @@ -211,8 +212,11 @@ export class TestSetupBuilder { username: 'hedgedoc', password: 'hedgedoc', autoLoadEntities: true, - synchronize: true, dropSchema: true, + migrations: [ + `src/migrations/${process.env.HEDGEDOC_TEST_DB_TYPE}-*.ts`, + ], + migrationsRun: true, }; default: throw new Error('Unknown database type in HEDGEDOC_TEST_DB_TYPE');