Skip to content

Commit

Permalink
add migration for versions
Browse files Browse the repository at this point in the history
  • Loading branch information
harishv7 committed Jul 28, 2024
1 parent c73b6e7 commit 01d7dcb
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 29 deletions.
10 changes: 9 additions & 1 deletion apps/studio/prisma/generated/generatedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface Resource {
permalink: string
siteId: number
parentId: string | null
mainBlobId: string | null
versionId: string | null
draftBlobId: string | null
state: Generated<ResourceState | null>
type: ResourceType
Expand Down Expand Up @@ -77,6 +77,13 @@ export interface VerificationToken {
attempts: Generated<number>
expires: Timestamp
}
export interface Version {
id: GeneratedAlways<string>
versionNum: string
resourceId: string
blobId: string
publishedAt: Generated<Timestamp>
}
export interface DB {
Blob: Blob
Footer: Footer
Expand All @@ -87,4 +94,5 @@ export interface DB {
SiteMember: SiteMember
User: User
VerificationToken: VerificationToken
Version: Version
}
1 change: 1 addition & 0 deletions apps/studio/prisma/generated/selectableTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export type Site = Selectable<T.Site>
export type SiteMember = Selectable<T.SiteMember>
export type User = Selectable<T.User>
export type VerificationToken = Selectable<T.VerificationToken>
export type Version = Selectable<T.Version>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Warnings:
- You are about to drop the column `mainBlobId` on the `Resource` table. All the data in the column will be lost.
*/
-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_mainBlobId_fkey";

-- DropIndex
DROP INDEX "Resource_mainBlobId_key";

-- AlterTable
ALTER TABLE "Resource" DROP COLUMN "mainBlobId",
ADD COLUMN "versionId" BIGINT;

-- CreateTable
CREATE TABLE "Version" (
"id" BIGSERIAL NOT NULL,
"versionNum" BIGINT NOT NULL,
"resourceId" BIGINT NOT NULL,
"blobId" BIGINT NOT NULL,
"publishedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "Version_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Version_resourceId_key" ON "Version"("resourceId");

-- AddForeignKey
ALTER TABLE "Version" ADD CONSTRAINT "Version_resourceId_fkey" FOREIGN KEY ("resourceId") REFERENCES "Resource"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Version" ADD CONSTRAINT "Version_blobId_fkey" FOREIGN KEY ("blobId") REFERENCES "Blob"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
35 changes: 22 additions & 13 deletions apps/studio/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ model VerificationToken {
expires DateTime
}

model Version {
id BigInt @id @default(autoincrement())
versionNum BigInt
resourceId BigInt @unique
resource Resource @relation("CurrentVersion", fields: [resourceId], references: [id])
blobId BigInt
blob Blob @relation(fields: [blobId], references: [id])
publishedAt DateTime @default(now())
}

model Resource {
id BigInt @id @default(autoincrement())
title String
Expand All @@ -46,16 +56,25 @@ model Resource {
permission Permission[]
mainBlob Blob? @relation("MainBlob", fields: [mainBlobId], references: [id])
mainBlobId BigInt? @unique
versionId BigInt?
version Version? @relation("CurrentVersion")
draftBlob Blob? @relation("DraftBlob", fields: [draftBlobId], references: [id])
draftBlobId BigInt? @unique
draftBlob Blob? @relation(fields: [draftBlobId], references: [id])
state ResourceState? @default(Draft)
type ResourceType
}

model Blob {
id BigInt @id @default(autoincrement())
/// @kyselyType(PrismaJson.BlobJsonContent)
/// [BlobJsonContent]
content Json
versions Version[]
draftResource Resource?
}

enum ResourceState {
Draft
Published
Expand Down Expand Up @@ -124,16 +143,6 @@ model Footer {
content Json
}

model Blob {
id BigInt @id @default(autoincrement())
/// @kyselyType(PrismaJson.BlobJsonContent)
/// [BlobJsonContent]
content Json
mainResource Resource? @relation("MainBlob")
draftResource Resource? @relation("DraftBlob")
}

model SiteMember {
userId String
user User @relation(fields: [userId], references: [id])
Expand Down
6 changes: 3 additions & 3 deletions apps/studio/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ async function main() {
await db
.insertInto("Resource")
.values({
mainBlobId: String(blobId),
draftBlobId: String(blobId),
permalink: "home",
siteId,
type: "Page",
Expand All @@ -232,8 +232,8 @@ async function main() {

.onConflict((oc) =>
oc
.column("mainBlobId")
.doUpdateSet((eb) => ({ mainBlobId: eb.ref("excluded.mainBlobId") })),
.column("draftBlobId")
.doUpdateSet((eb) => ({ draftBlobId: eb.ref("excluded.draftBlobId") })),
)
.executeTakeFirstOrThrow()

Expand Down
2 changes: 1 addition & 1 deletion apps/studio/src/server/modules/folder/folder.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const folderRouter = router({
.where("parentId", "=", String(input.resourceId))
.execute()
const children = childrenResult.map((c) => {
if (c.draftBlobId || c.mainBlobId) {
if (c.draftBlobId || c.versionId) {
return {
id: c.id,
permalink: c.permalink,
Expand Down
2 changes: 1 addition & 1 deletion apps/studio/src/server/modules/page/page.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const pageRouter = router({
"Resource.id",
"Resource.permalink",
"Resource.title",
"Resource.mainBlobId",
"Resource.versionId",
"Resource.draftBlobId",
"Resource.type",
])
Expand Down
7 changes: 4 additions & 3 deletions apps/studio/src/server/modules/resource/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const defaultResourceSelect: SelectExpression<DB, "Resource">[] = [
"Resource.permalink",
"Resource.siteId",
"Resource.parentId",
"Resource.mainBlobId",
"Resource.versionId",
"Resource.draftBlobId",
"Resource.type",
"Resource.state",
Expand Down Expand Up @@ -81,8 +81,9 @@ export const getFullPageById = async (args: {
}

return getById(args)
.where("Resource.mainBlobId", "is not", null)
.innerJoin("Blob", "Resource.mainBlobId", "Blob.id")
.where("Resource.versionId", "is not", null)
.innerJoin("Version", "Resource.versionId", "Version.id")
.innerJoin("Blob", "Version.blobId", "Blob.id")
.select(defaultResourceWithBlobSelect)
.executeTakeFirst()
}
Expand Down
3 changes: 1 addition & 2 deletions apps/studio/src/server/modules/resource/resource.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
type IsomerPageSchemaType,
type IsomerSiteProps,
} from "@opengovsg/isomer-components"
import { type SetRequired } from "type-fest"

import type { Resource } from "~server/db"

Expand All @@ -12,7 +11,7 @@ export type PageContent = Omit<
>

// TODO: Technically mainBlobId is not required before 1st publish
export type Page = SetRequired<Resource, "mainBlobId">
export type Page = Resource

export interface Navbar {
items: IsomerSiteProps["navBarItems"]
Expand Down
10 changes: 5 additions & 5 deletions apps/studio/tests/msw/handlers/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ const pageListQuery = (wait?: DelayMode | number) => {
id: "4",
permalink: "test-page-1",
title: "Test page 1",
mainBlobId: "3",
draftBlobId: null,
versionId: null,
draftBlobId: "3",
type: "Page",
},
{
id: "5",
permalink: "test-page-2",
title: "Test page 2",
mainBlobId: "4",
draftBlobId: null,
versionId: null,
draftBlobId: "4",
type: "Page",
},
{
id: "6",
permalink: "folder",
title: "Test folder 1",
mainBlobId: null,
versionId: null,
draftBlobId: null,
type: "Folder",
},
Expand Down

0 comments on commit 01d7dcb

Please sign in to comment.