From 7e91038a24b4fc984ba7d5c17ff00dee7d0eea42 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Thu, 26 Jan 2023 14:06:25 +0100 Subject: [PATCH] chore: optimize prisma migration --- .../migration.sql | 91 ------------- .../migration.sql | 67 ++++++++++ backend/prisma/schema.prisma | 4 +- backend/prisma/seed/config.seed.ts | 123 +++++++++--------- backend/src/config/config.service.ts | 2 +- 5 files changed, 132 insertions(+), 155 deletions(-) delete mode 100644 backend/prisma/migrations/20230126094911_reverse_share/migration.sql create mode 100644 backend/prisma/migrations/20230126125501_reverse_shares/migration.sql diff --git a/backend/prisma/migrations/20230126094911_reverse_share/migration.sql b/backend/prisma/migrations/20230126094911_reverse_share/migration.sql deleted file mode 100644 index 95e445a..0000000 --- a/backend/prisma/migrations/20230126094911_reverse_share/migration.sql +++ /dev/null @@ -1,91 +0,0 @@ -/* - Warnings: - - - The primary key for the `Config` table will be changed. If it partially fails, the table could be left without primary key constraint. - - Added the required column `id` to the `Config` table without a default value. This is not possible if the table is not empty. - -*/ --- CreateTable -CREATE TABLE "ReverseShare" ( - "id" TEXT NOT NULL PRIMARY KEY, - "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - "token" TEXT NOT NULL, - "shareExpiration" DATETIME NOT NULL, - "maxShareSize" TEXT NOT NULL, - "sendEmailNotification" BOOLEAN NOT NULL, - "used" BOOLEAN NOT NULL DEFAULT false, - "creatorId" TEXT NOT NULL, - "shareId" TEXT, - CONSTRAINT "ReverseShare_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT "ReverseShare_shareId_fkey" FOREIGN KEY ("shareId") REFERENCES "Share" ("id") ON DELETE CASCADE ON UPDATE CASCADE -); - --- RedefineTables -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_Config" ( - "id" INTEGER, - "updatedAt" DATETIME NOT NULL, - "key" TEXT NOT NULL, - "type" TEXT NOT NULL, - "value" TEXT NOT NULL, - "description" TEXT NOT NULL, - "category" TEXT NOT NULL, - "obscured" BOOLEAN NOT NULL DEFAULT false, - "secret" BOOLEAN NOT NULL DEFAULT true, - "locked" BOOLEAN NOT NULL DEFAULT false -); -INSERT INTO "new_Config" ("category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value") SELECT "category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value" FROM "Config"; -DROP TABLE "Config"; -ALTER TABLE "new_Config" RENAME TO "Config"; -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; - --- CreateIndex -CREATE UNIQUE INDEX "ReverseShare_token_key" ON "ReverseShare"("token"); - --- CreateIndex -CREATE UNIQUE INDEX "ReverseShare_shareId_key" ON "ReverseShare"("shareId"); - - --- Add ids to existing settings - - -UPDATE Config SET id = 1 WHERE key = "SETUP_FINISHED"; -UPDATE Config SET id = 2 WHERE key = "APP_URL"; -UPDATE Config SET id = 3 WHERE key = "SHOW_HOME_PAGE"; -UPDATE Config SET id = 4 WHERE key = "ALLOW_REGISTRATION"; -UPDATE Config SET id = 5 WHERE key = "ALLOW_UNAUTHENTICATED_SHARES"; -UPDATE Config SET id = 6 WHERE key = "MAX_SHARE_SIZE"; -UPDATE Config SET id = 7 WHERE key = "JWT_SECRET"; -UPDATE Config SET id = 8 WHERE key = "TOTP_SECRET"; -UPDATE Config SET id = 9, key = "ENABLE_SHARE_EMAIL_RECIPIENTS" WHERE key = "ENABLE_EMAIL_RECIPIENTS"; -UPDATE Config SET id = 10, key = "SHARE_RECEPIENTS_EMAIL_MESSAGE" WHERE key = "EMAIL_MESSAGE"; -UPDATE Config SET id = 11, key = "SHARE_RECEPIENTS_EMAIL_SUBJECT" WHERE key = "EMAIL_SUBJECT"; -UPDATE Config SET id = 15 WHERE key = "SMTP_HOST"; -UPDATE Config SET id = 16 WHERE key = "SMTP_PORT"; -UPDATE Config SET id = 17 WHERE key = "SMTP_EMAIL"; -UPDATE Config SET id = 18 WHERE key = "SMTP_USERNAME"; -UPDATE Config SET id = 19 WHERE key = "SMTP_PASSWORD"; - -INSERT INTO Config (`id`, `key`, `description`, `type`, `value`, `category`, `secret`, `updatedAt`) VALUES (14, "SMTP_ENABLED", "Whether SMTP is enabled. Only set this to true if you entered the host, port, email, user and password of your SMTP server.", "boolean", IFNULL((SELECT value FROM Config WHERE key="ENABLE_SHARE_EMAIL_RECIPIENTS"), "false"), "smtp", 0, strftime('%s', 'now')); - -PRAGMA foreign_keys=OFF; -CREATE TABLE "new_Config" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - "updatedAt" DATETIME NOT NULL, - "key" TEXT NOT NULL, - "type" TEXT NOT NULL, - "value" TEXT NOT NULL, - "description" TEXT NOT NULL, - "category" TEXT NOT NULL, - "obscured" BOOLEAN NOT NULL DEFAULT false, - "secret" BOOLEAN NOT NULL DEFAULT true, - "locked" BOOLEAN NOT NULL DEFAULT false -); -INSERT INTO "new_Config" ("id", "category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value") SELECT "id", "category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value" FROM "Config"; -DROP TABLE "Config"; -ALTER TABLE "new_Config" RENAME TO "Config"; -DELETE from Config WHERE key="MAX_FILE_SIZE"; -CREATE UNIQUE INDEX "Config_key_key" ON "Config"("key"); -PRAGMA foreign_key_check; -PRAGMA foreign_keys=ON; diff --git a/backend/prisma/migrations/20230126125501_reverse_shares/migration.sql b/backend/prisma/migrations/20230126125501_reverse_shares/migration.sql new file mode 100644 index 0000000..1129288 --- /dev/null +++ b/backend/prisma/migrations/20230126125501_reverse_shares/migration.sql @@ -0,0 +1,67 @@ +/* + Warnings: + + - Added the required column `order` to the `Config` table without a default value. This is not possible if the table is not empty. + +*/ +-- CreateTable +CREATE TABLE "ReverseShare" ( + "id" TEXT NOT NULL PRIMARY KEY, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "token" TEXT NOT NULL, + "shareExpiration" DATETIME NOT NULL, + "maxShareSize" TEXT NOT NULL, + "sendEmailNotification" BOOLEAN NOT NULL, + "used" BOOLEAN NOT NULL DEFAULT false, + "creatorId" TEXT NOT NULL, + "shareId" TEXT, + CONSTRAINT "ReverseShare_creatorId_fkey" FOREIGN KEY ("creatorId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT "ReverseShare_shareId_fkey" FOREIGN KEY ("shareId") REFERENCES "Share" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_Config" ( + "updatedAt" DATETIME NOT NULL, + "key" TEXT NOT NULL PRIMARY KEY, + "type" TEXT NOT NULL, + "value" TEXT NOT NULL, + "description" TEXT NOT NULL, + "category" TEXT NOT NULL, + "obscured" BOOLEAN NOT NULL DEFAULT false, + "secret" BOOLEAN NOT NULL DEFAULT true, + "locked" BOOLEAN NOT NULL DEFAULT false, + "order" INTEGER NOT NULL +); +INSERT INTO "new_Config" ("category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value", "order") SELECT "category", "description", "key", "locked", "obscured", "secret", "type", "updatedAt", "value", 0 FROM "Config"; +DROP TABLE "Config"; +ALTER TABLE "new_Config" RENAME TO "Config"; +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; + +-- CreateIndex +CREATE UNIQUE INDEX "ReverseShare_token_key" ON "ReverseShare"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "ReverseShare_shareId_key" ON "ReverseShare"("shareId"); + +-- Custom migration +UPDATE Config SET `order` = 0 WHERE key = "SETUP_FINISHED"; +UPDATE Config SET `order` = 0 WHERE key = "JWT_SECRET"; +UPDATE Config SET `order` = 0 WHERE key = "TOTP_SECRET"; + +UPDATE Config SET `order` = 1 WHERE key = "APP_URL"; +UPDATE Config SET `order` = 2 WHERE key = "SHOW_HOME_PAGE"; +UPDATE Config SET `order` = 3 WHERE key = "ALLOW_REGISTRATION"; +UPDATE Config SET `order` = 4 WHERE key = "ALLOW_UNAUTHENTICATED_SHARES"; +UPDATE Config SET `order` = 5 WHERE key = "MAX_SHARE_SIZE"; +UPDATE Config SET `order` = 6, key = "ENABLE_SHARE_EMAIL_RECIPIENTS" WHERE key = "ENABLE_EMAIL_RECIPIENTS"; +UPDATE Config SET `order` = 7, key = "SHARE_RECEPIENTS_EMAIL_MESSAGE" WHERE key = "EMAIL_MESSAGE"; +UPDATE Config SET `order` = 8, key = "SHARE_RECEPIENTS_EMAIL_SUBJECT" WHERE key = "EMAIL_SUBJECT"; +UPDATE Config SET `order` = 12 WHERE key = "SMTP_HOST"; +UPDATE Config SET `order` = 13 WHERE key = "SMTP_PORT"; +UPDATE Config SET `order` = 14 WHERE key = "SMTP_EMAIL"; +UPDATE Config SET `order` = 15 WHERE key = "SMTP_USERNAME"; +UPDATE Config SET `order` = 16 WHERE key = "SMTP_PASSWORD"; + +INSERT INTO Config (`order`, `key`, `description`, `type`, `value`, `category`, `secret`, `updatedAt`) VALUES (11, "SMTP_ENABLED", "Whether SMTP is enabled. Only set this to true if you entered the host, port, email, user and password of your SMTP server.", "boolean", IFNULL((SELECT value FROM Config WHERE key="ENABLE_SHARE_EMAIL_RECIPIENTS"), "false"), "smtp", 0, strftime('%s', 'now')); diff --git a/backend/prisma/schema.prisma b/backend/prisma/schema.prisma index 7c85ae3..c9444b0 100644 --- a/backend/prisma/schema.prisma +++ b/backend/prisma/schema.prisma @@ -118,10 +118,9 @@ model ShareSecurity { } model Config { - id Int @id updatedAt DateTime @updatedAt - key String @unique + key String @id type String value String description String @@ -129,4 +128,5 @@ model Config { obscured Boolean @default(false) secret Boolean @default(true) locked Boolean @default(false) + order Int } diff --git a/backend/prisma/seed/config.seed.ts b/backend/prisma/seed/config.seed.ts index c57cf70..07f9e50 100644 --- a/backend/prisma/seed/config.seed.ts +++ b/backend/prisma/seed/config.seed.ts @@ -3,9 +3,9 @@ import * as crypto from "crypto"; const configVariables: Prisma.ConfigCreateInput[] = [ { - id: 1, + order: 0, key: "SETUP_FINISHED", - description: "Whether the setup has been finished", + description: "Status of the setup wizard", type: "boolean", value: "false", category: "internal", @@ -13,53 +13,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ locked: true, }, { - id: 2, - key: "APP_URL", - description: "On which URL Pingvin Share is available", - type: "string", - value: "http://localhost:3000", - category: "general", - secret: false, - }, - { - id: 3, - key: "SHOW_HOME_PAGE", - description: "Whether to show the home page", - type: "boolean", - value: "true", - category: "general", - secret: false, - }, - { - id: 4, - key: "ALLOW_REGISTRATION", - description: "Whether registration is allowed", - type: "boolean", - value: "true", - category: "share", - secret: false, - }, - { - id: 5, - key: "ALLOW_UNAUTHENTICATED_SHARES", - description: "Whether unauthorized users can create shares", - type: "boolean", - value: "false", - category: "share", - secret: false, - }, - { - id: 6, - - key: "MAX_SHARE_SIZE", - description: "Maximum share size in bytes", - type: "number", - value: "1073741824", - category: "share", - secret: false, - }, - { - id: 7, + order: 0, key: "JWT_SECRET", description: "Long random string used to sign JWT tokens", type: "string", @@ -68,7 +22,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ locked: true, }, { - id: 8, + order: 0, key: "TOTP_SECRET", description: "A 16 byte random string used to generate TOTP secrets", type: "string", @@ -77,7 +31,54 @@ const configVariables: Prisma.ConfigCreateInput[] = [ locked: true, }, { - id: 9, + order: 1, + key: "APP_URL", + description: "On which URL Pingvin Share is available", + type: "string", + value: "http://localhost:3000", + category: "general", + secret: false, + }, + { + order: 2, + key: "SHOW_HOME_PAGE", + description: "Whether to show the home page", + type: "boolean", + value: "true", + category: "general", + secret: false, + }, + { + order: 3, + key: "ALLOW_REGISTRATION", + description: "Whether registration is allowed", + type: "boolean", + value: "true", + category: "share", + secret: false, + }, + { + order: 4, + key: "ALLOW_UNAUTHENTICATED_SHARES", + description: "Whether unauthorized users can create shares", + type: "boolean", + value: "false", + category: "share", + secret: false, + }, + { + order: 5, + + key: "MAX_SHARE_SIZE", + description: "Maximum share size in bytes", + type: "number", + value: "1073741824", + category: "share", + secret: false, + }, + + { + order: 6, key: "ENABLE_SHARE_EMAIL_RECIPIENTS", description: "Whether to allow emails to share recipients. Only enable this if you have enabled SMTP.", @@ -87,7 +88,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ secret: false, }, { - id: 10, + order: 7, key: "SHARE_RECEPIENTS_EMAIL_MESSAGE", description: "Message which gets sent to the share recipients. {creator} and {shareUrl} will be replaced with the creator's name and the share URL.", @@ -97,7 +98,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "email", }, { - id: 11, + order: 8, key: "SHARE_RECEPIENTS_EMAIL_SUBJECT", description: "Subject of the email which gets sent to the share recipients.", @@ -106,7 +107,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "email", }, { - id: 12, + order: 9, key: "REVERSE_SHARE_EMAIL_MESSAGE", description: "Message which gets sent when someone created a share with your reverse share link. {shareUrl} will be replaced with the creator's name and the share URL.", @@ -116,7 +117,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "email", }, { - id: 13, + order: 10, key: "REVERSE_SHARE_EMAIL_SUBJECT", description: "Subject of the email which gets sent when someone created a share with your reverse share link.", @@ -125,7 +126,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "email", }, { - id: 14, + order: 11, key: "SMTP_ENABLED", description: "Whether SMTP is enabled. Only set this to true if you entered the host, port, email, user and password of your SMTP server.", @@ -135,7 +136,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ secret: false, }, { - id: 15, + order: 12, key: "SMTP_HOST", description: "Host of the SMTP server", type: "string", @@ -143,7 +144,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "smtp", }, { - id: 16, + order: 13, key: "SMTP_PORT", description: "Port of the SMTP server", type: "number", @@ -151,7 +152,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "smtp", }, { - id: 17, + order: 14, key: "SMTP_EMAIL", description: "Email address which the emails get sent from", type: "string", @@ -159,7 +160,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "smtp", }, { - id: 18, + order: 15, key: "SMTP_USERNAME", description: "Username of the SMTP server", type: "string", @@ -167,7 +168,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [ category: "smtp", }, { - id: 19, + order: 16, key: "SMTP_PASSWORD", description: "Password of the SMTP server", type: "string", diff --git a/backend/src/config/config.service.ts b/backend/src/config/config.service.ts index 25f878b..5c97a28 100644 --- a/backend/src/config/config.service.ts +++ b/backend/src/config/config.service.ts @@ -29,7 +29,7 @@ export class ConfigService { async listForAdmin() { return await this.prisma.config.findMany({ - orderBy: { id: "asc" }, + orderBy: { order: "asc" }, where: { locked: { equals: false } }, }); }