Skip to content

Commit

Permalink
chore: optimize prisma migration
Browse files Browse the repository at this point in the history
  • Loading branch information
stonith404 committed Jan 26, 2023
1 parent 4a5fb54 commit 7e91038
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 130 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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'));
4 changes: 2 additions & 2 deletions backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ model ShareSecurity {
}

model Config {
id Int @id
updatedAt DateTime @updatedAt
key String @unique
key String @id
type String
value String
description String
category String
obscured Boolean @default(false)
secret Boolean @default(true)
locked Boolean @default(false)
order Int
}
73 changes: 37 additions & 36 deletions backend/prisma/seed/config.seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,35 @@ 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",
secret: false,
locked: true,
},
{
id: 2,
order: 0,
key: "JWT_SECRET",
description: "Long random string used to sign JWT tokens",
type: "string",
value: crypto.randomBytes(256).toString("base64"),
category: "internal",
locked: true,
},
{
order: 0,
key: "TOTP_SECRET",
description: "A 16 byte random string used to generate TOTP secrets",
type: "string",
value: crypto.randomBytes(16).toString("base64"),
category: "internal",
locked: true,
},
{
order: 1,
key: "APP_URL",
description: "On which URL Pingvin Share is available",
type: "string",
Expand All @@ -22,7 +40,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [
secret: false,
},
{
id: 3,
order: 2,
key: "SHOW_HOME_PAGE",
description: "Whether to show the home page",
type: "boolean",
Expand All @@ -31,7 +49,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [
secret: false,
},
{
id: 4,
order: 3,
key: "ALLOW_REGISTRATION",
description: "Whether registration is allowed",
type: "boolean",
Expand All @@ -40,7 +58,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [
secret: false,
},
{
id: 5,
order: 4,
key: "ALLOW_UNAUTHENTICATED_SHARES",
description: "Whether unauthorized users can create shares",
type: "boolean",
Expand All @@ -49,7 +67,7 @@ const configVariables: Prisma.ConfigCreateInput[] = [
secret: false,
},
{
id: 6,
order: 5,

key: "MAX_SHARE_SIZE",
description: "Maximum share size in bytes",
Expand All @@ -58,26 +76,9 @@ const configVariables: Prisma.ConfigCreateInput[] = [
category: "share",
secret: false,
},

{
id: 7,
key: "JWT_SECRET",
description: "Long random string used to sign JWT tokens",
type: "string",
value: crypto.randomBytes(256).toString("base64"),
category: "internal",
locked: true,
},
{
id: 8,
key: "TOTP_SECRET",
description: "A 16 byte random string used to generate TOTP secrets",
type: "string",
value: crypto.randomBytes(16).toString("base64"),
category: "internal",
locked: true,
},
{
id: 9,
order: 6,
key: "ENABLE_SHARE_EMAIL_RECIPIENTS",
description:
"Whether to allow emails to share recipients. Only enable this if you have enabled SMTP.",
Expand All @@ -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.",
Expand All @@ -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.",
Expand All @@ -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.",
Expand All @@ -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.",
Expand All @@ -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.",
Expand All @@ -135,39 +136,39 @@ const configVariables: Prisma.ConfigCreateInput[] = [
secret: false,
},
{
id: 15,
order: 12,
key: "SMTP_HOST",
description: "Host of the SMTP server",
type: "string",
value: "",
category: "smtp",
},
{
id: 16,
order: 13,
key: "SMTP_PORT",
description: "Port of the SMTP server",
type: "number",
value: "0",
category: "smtp",
},
{
id: 17,
order: 14,
key: "SMTP_EMAIL",
description: "Email address which the emails get sent from",
type: "string",
value: "",
category: "smtp",
},
{
id: 18,
order: 15,
key: "SMTP_USERNAME",
description: "Username of the SMTP server",
type: "string",
value: "",
category: "smtp",
},
{
id: 19,
order: 16,
key: "SMTP_PASSWORD",
description: "Password of the SMTP server",
type: "string",
Expand Down
2 changes: 1 addition & 1 deletion backend/src/config/config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
});
}
Expand Down

0 comments on commit 7e91038

Please sign in to comment.