From 39aaee794e1329f5b3d26e9e225aa7df57618207 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Tue, 25 Jun 2024 16:23:39 +0800 Subject: [PATCH 1/6] fix: schema update to auto-increment --- apps/studio/prisma/schema.prisma | 42 ++++++++++++++++---------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/apps/studio/prisma/schema.prisma b/apps/studio/prisma/schema.prisma index 113250f8d0..9b534173eb 100644 --- a/apps/studio/prisma/schema.prisma +++ b/apps/studio/prisma/schema.prisma @@ -1,4 +1,4 @@ -// This is your Prisma schema file, +// This is your Prisma schema file,/ // learn more about it in the docs: https://pris.ly/d/prisma-schema datasource db { @@ -27,13 +27,13 @@ model VerificationToken { } model Resource { - id String @id @default(cuid()) + id Int @id @default(autoincrement()) name String - siteId String + siteId Int site Site @relation(fields: [siteId], references: [id]) - parentId String? + parentId Int? parent Resource? @relation("parentRelation", fields: [parentId], references: [id]) - blobId String? @unique + blobId Int? @unique blob Blob? @relation(fields: [blobId], references: [id]) children Resource[] @relation("parentRelation") @@ -42,7 +42,7 @@ model Resource { } model User { - id String @id @default(cuid()) + id Int @id @default(autoincrement()) name String email String @unique phone String @@ -53,10 +53,10 @@ model User { } model Permission { - id String @id @default(cuid()) - resourceId String + id Int @id @default(autoincrement()) + resourceId Int resource Resource @relation(fields: [resourceId], references: [id]) - userId String + userId Int user User @relation(fields: [userId], references: [id]) role RoleType @@ -64,7 +64,7 @@ model Permission { } model Site { - id String @id @default(cuid()) + id Int @id @default(autoincrement()) name String @unique resources Resource[] siteMembers SiteMember[] @@ -78,21 +78,21 @@ model Site { } model Navbar { - id String @id @default(cuid()) - siteId String @unique - site Site @relation(fields: [siteId], references: [id]) + id Int @id @default(autoincrement()) + siteId Int @unique + site Site @relation(fields: [siteId], references: [id]) content Json } model Footer { - id String @id @default(cuid()) - siteId String @unique - site Site @relation(fields: [siteId], references: [id]) + id Int @id @default(autoincrement()) + siteId Int @unique + site Site @relation(fields: [siteId], references: [id]) content Json } model Blob { - id String @id @default(cuid()) + id Int @id @default(autoincrement()) content Json // Should the blob be deleted on deletion of the resource? @@ -100,10 +100,10 @@ model Blob { } model SiteMember { - userId String - user User @relation(fields: [userId], references: [id]) - siteId String - site Site @relation(fields: [siteId], references: [id]) + userId Int + user User @relation(fields: [userId], references: [id]) + siteId Int + site Site @relation(fields: [siteId], references: [id]) @@id([siteId, userId]) } From d223e355e8b40ef8275e18c12528f6b6200d0e76 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Tue, 25 Jun 2024 16:29:05 +0800 Subject: [PATCH 2/6] chore: migration --- .../studio/prisma/generated/generatedTypes.ts | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/apps/studio/prisma/generated/generatedTypes.ts b/apps/studio/prisma/generated/generatedTypes.ts index 0b4602d322..b1df2e163a 100644 --- a/apps/studio/prisma/generated/generatedTypes.ts +++ b/apps/studio/prisma/generated/generatedTypes.ts @@ -1,4 +1,4 @@ -import type { ColumnType } from "kysely" +import type { ColumnType, GeneratedAlways } from "kysely" export type Generated = T extends ColumnType ? ColumnType @@ -8,43 +8,43 @@ export type Timestamp = ColumnType import type { RoleType } from "./generatedEnums" export type Blob = { - id: string + id: GeneratedAlways content: unknown } export type Footer = { - id: string - siteId: string + id: GeneratedAlways + siteId: number content: unknown } export type Navbar = { - id: string - siteId: string + id: GeneratedAlways + siteId: number content: unknown } export type Permission = { - id: string - resourceId: string - userId: string + id: GeneratedAlways + resourceId: number + userId: number role: RoleType } export type Resource = { - id: string + id: GeneratedAlways name: string - siteId: string - parentId: string | null - blobId: string | null + siteId: number + parentId: number | null + blobId: number | null } export type Site = { - id: string + id: GeneratedAlways name: string config: unknown } export type SiteMember = { - userId: string - siteId: string + userId: number + siteId: number } export type User = { - id: string + id: GeneratedAlways name: string email: string phone: string From a989b631c23d76ac2a9385eb171c52ef0eee5d01 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Tue, 25 Jun 2024 16:33:31 +0800 Subject: [PATCH 3/6] chore: update seed file --- apps/studio/prisma/seed.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/apps/studio/prisma/seed.ts b/apps/studio/prisma/seed.ts index f73aaaee59..979a952d16 100644 --- a/apps/studio/prisma/seed.ts +++ b/apps/studio/prisma/seed.ts @@ -51,10 +51,9 @@ const NAV_BAR_ITEMS = [ ] async function main() { - await db + const { id } = await db .insertInto('Site') .values({ - id: '1', name: 'Ministry of Trade and Industry', config: { theme: 'isomer-next', @@ -66,13 +65,13 @@ async function main() { isGovernment: true, } satisfies SiteConfig, }) - .execute() + .returning('id') + .executeTakeFirstOrThrow() await db .insertInto('Footer') .values({ - id: '1', - siteId: '1', + siteId: id, content: { name: 'A foot', contactUsLink: '/contact-us', @@ -86,8 +85,7 @@ async function main() { await db .insertInto('Navbar') .values({ - id: '1', - siteId: '1', + siteId: id, content: { name: 'navi', url: 'www.isomer.gov.sg', From e638a4853cddd8a4e5a3dd0f314969e946d7a4c7 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Tue, 25 Jun 2024 16:48:24 +0800 Subject: [PATCH 4/6] chore: migrations --- .../migration.sql | 0 .../migration.sql | 155 ++++++++++++++++++ 2 files changed, 155 insertions(+) rename apps/studio/prisma/migrations/{20240624130652_ => 20240624130652_drop_footer_name}/migration.sql (100%) create mode 100644 apps/studio/prisma/migrations/20240625084750_set_id_auto_increment/migration.sql diff --git a/apps/studio/prisma/migrations/20240624130652_/migration.sql b/apps/studio/prisma/migrations/20240624130652_drop_footer_name/migration.sql similarity index 100% rename from apps/studio/prisma/migrations/20240624130652_/migration.sql rename to apps/studio/prisma/migrations/20240624130652_drop_footer_name/migration.sql diff --git a/apps/studio/prisma/migrations/20240625084750_set_id_auto_increment/migration.sql b/apps/studio/prisma/migrations/20240625084750_set_id_auto_increment/migration.sql new file mode 100644 index 0000000000..784886f54b --- /dev/null +++ b/apps/studio/prisma/migrations/20240625084750_set_id_auto_increment/migration.sql @@ -0,0 +1,155 @@ +/* + Warnings: + + - The primary key for the `Blob` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Blob` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `Footer` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Footer` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `Navbar` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Navbar` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `Permission` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Permission` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `Resource` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The `parentId` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The `blobId` column on the `Resource` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `Site` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `Site` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - The primary key for the `SiteMember` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint. + - The `id` column on the `User` table would be dropped and recreated. This will lead to data loss if there is data in the column. + - Changed the type of `siteId` on the `Footer` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `siteId` on the `Navbar` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `resourceId` on the `Permission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `userId` on the `Permission` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `siteId` on the `Resource` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `userId` on the `SiteMember` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + - Changed the type of `siteId` on the `SiteMember` table. No cast exists, the column would be dropped and recreated, which cannot be done if there is data, since the column is required. + +*/ +-- DropForeignKey +ALTER TABLE "Footer" DROP CONSTRAINT "Footer_siteId_fkey"; + +-- DropForeignKey +ALTER TABLE "Navbar" DROP CONSTRAINT "Navbar_siteId_fkey"; + +-- DropForeignKey +ALTER TABLE "Permission" DROP CONSTRAINT "Permission_resourceId_fkey"; + +-- DropForeignKey +ALTER TABLE "Permission" DROP CONSTRAINT "Permission_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Resource" DROP CONSTRAINT "Resource_blobId_fkey"; + +-- DropForeignKey +ALTER TABLE "Resource" DROP CONSTRAINT "Resource_parentId_fkey"; + +-- DropForeignKey +ALTER TABLE "Resource" DROP CONSTRAINT "Resource_siteId_fkey"; + +-- DropForeignKey +ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_siteId_fkey"; + +-- DropForeignKey +ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_userId_fkey"; + +-- AlterTable +ALTER TABLE "Blob" DROP CONSTRAINT "Blob_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +ADD CONSTRAINT "Blob_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "Footer" DROP CONSTRAINT "Footer_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +DROP COLUMN "siteId", +ADD COLUMN "siteId" INTEGER NOT NULL, +ADD CONSTRAINT "Footer_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "Navbar" DROP CONSTRAINT "Navbar_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +DROP COLUMN "siteId", +ADD COLUMN "siteId" INTEGER NOT NULL, +ADD CONSTRAINT "Navbar_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "Permission" DROP CONSTRAINT "Permission_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +DROP COLUMN "resourceId", +ADD COLUMN "resourceId" INTEGER NOT NULL, +DROP COLUMN "userId", +ADD COLUMN "userId" INTEGER NOT NULL, +ADD CONSTRAINT "Permission_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "Resource" DROP CONSTRAINT "Resource_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +DROP COLUMN "siteId", +ADD COLUMN "siteId" INTEGER NOT NULL, +DROP COLUMN "parentId", +ADD COLUMN "parentId" INTEGER, +DROP COLUMN "blobId", +ADD COLUMN "blobId" INTEGER, +ADD CONSTRAINT "Resource_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "Site" DROP CONSTRAINT "Site_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +ADD CONSTRAINT "Site_pkey" PRIMARY KEY ("id"); + +-- AlterTable +ALTER TABLE "SiteMember" DROP CONSTRAINT "SiteMember_pkey", +DROP COLUMN "userId", +ADD COLUMN "userId" INTEGER NOT NULL, +DROP COLUMN "siteId", +ADD COLUMN "siteId" INTEGER NOT NULL, +ADD CONSTRAINT "SiteMember_pkey" PRIMARY KEY ("siteId", "userId"); + +-- AlterTable +ALTER TABLE "User" DROP CONSTRAINT "User_pkey", +DROP COLUMN "id", +ADD COLUMN "id" SERIAL NOT NULL, +ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id"); + +-- CreateIndex +CREATE UNIQUE INDEX "Footer_siteId_key" ON "Footer"("siteId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Navbar_siteId_key" ON "Navbar"("siteId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Resource_blobId_key" ON "Resource"("blobId"); + +-- AddForeignKey +ALTER TABLE "Resource" ADD CONSTRAINT "Resource_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Resource" ADD CONSTRAINT "Resource_parentId_fkey" FOREIGN KEY ("parentId") REFERENCES "Resource"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Resource" ADD CONSTRAINT "Resource_blobId_fkey" FOREIGN KEY ("blobId") REFERENCES "Blob"("id") ON DELETE SET NULL ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Permission" ADD CONSTRAINT "Permission_resourceId_fkey" FOREIGN KEY ("resourceId") REFERENCES "Resource"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Permission" ADD CONSTRAINT "Permission_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Navbar" ADD CONSTRAINT "Navbar_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Footer" ADD CONSTRAINT "Footer_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "SiteMember" ADD CONSTRAINT "SiteMember_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "SiteMember" ADD CONSTRAINT "SiteMember_siteId_fkey" FOREIGN KEY ("siteId") REFERENCES "Site"("id") ON DELETE RESTRICT ON UPDATE CASCADE; From 7a1a9f1c74391c90453f555221c855c544750d2d Mon Sep 17 00:00:00 2001 From: seaerchin Date: Tue, 25 Jun 2024 17:03:00 +0800 Subject: [PATCH 5/6] chore: update seed with types --- apps/studio/prisma/seed.ts | 7 ++----- apps/studio/src/server/modules/resource/resource.types.ts | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/apps/studio/prisma/seed.ts b/apps/studio/prisma/seed.ts index 979a952d16..0448dc183f 100644 --- a/apps/studio/prisma/seed.ts +++ b/apps/studio/prisma/seed.ts @@ -3,6 +3,7 @@ * * @link https://www.prisma.io/docs/guides/database/seed-database */ +import { type IsomerSitemap } from '@opengovsg/isomer-components' import { type SiteConfig } from '~/server/modules/site/site.types' import { type Navbar, @@ -86,11 +87,7 @@ async function main() { .insertInto('Navbar') .values({ siteId: id, - content: { - name: 'navi', - url: 'www.isomer.gov.sg', - items: NAV_BAR_ITEMS, - } satisfies Navbar, + content: { items: NAV_BAR_ITEMS } satisfies Navbar, }) .execute() } diff --git a/apps/studio/src/server/modules/resource/resource.types.ts b/apps/studio/src/server/modules/resource/resource.types.ts index 13194fa037..6e716271e5 100644 --- a/apps/studio/src/server/modules/resource/resource.types.ts +++ b/apps/studio/src/server/modules/resource/resource.types.ts @@ -1,5 +1,9 @@ -export interface NavbarItem { - name: string +import { type IsomerSiteProps } from '@opengovsg/isomer-components' + +export type Navbar = { items: IsomerSiteProps['navBarItems'] } + +export interface FooterItem { + title: string url: string description?: string } From 83abfb4c43fc37e94151981ad8138fd44b429d50 Mon Sep 17 00:00:00 2001 From: seaerchin Date: Wed, 26 Jun 2024 13:06:40 +0800 Subject: [PATCH 6/6] chore: schema/seed remove / add type --- apps/studio/prisma/schema.prisma | 2 +- apps/studio/prisma/seed.ts | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/studio/prisma/schema.prisma b/apps/studio/prisma/schema.prisma index 9b534173eb..223b90206e 100644 --- a/apps/studio/prisma/schema.prisma +++ b/apps/studio/prisma/schema.prisma @@ -1,4 +1,4 @@ -// This is your Prisma schema file,/ +// This is your Prisma schema file, // learn more about it in the docs: https://pris.ly/d/prisma-schema datasource db { diff --git a/apps/studio/prisma/seed.ts b/apps/studio/prisma/seed.ts index 0448dc183f..af1fc9f5f2 100644 --- a/apps/studio/prisma/seed.ts +++ b/apps/studio/prisma/seed.ts @@ -3,7 +3,6 @@ * * @link https://www.prisma.io/docs/guides/database/seed-database */ -import { type IsomerSitemap } from '@opengovsg/isomer-components' import { type SiteConfig } from '~/server/modules/site/site.types' import { type Navbar, @@ -11,7 +10,7 @@ import { } from '~/server/modules/resource/resource.types' import { db } from '../src/server/modules/database' -const NAV_BAR_ITEMS = [ +const NAV_BAR_ITEMS: Navbar['items'] = [ { name: 'Expandable nav item', url: '/item-one',