Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add migration for versions #369

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions apps/studio/prisma/generated/generatedEnums.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
export const ResourceState = {
Draft: "Draft",
Published: "Published",
} as const
export type ResourceState = (typeof ResourceState)[keyof typeof ResourceState]
Draft: "Draft",
Published: "Published"
} as const;
export type ResourceState = (typeof ResourceState)[keyof typeof ResourceState];
export const ResourceType = {
Page: "Page",
Folder: "Folder",
} as const
export type ResourceType = (typeof ResourceType)[keyof typeof ResourceType]
Page: "Page",
Folder: "Folder"
} as const;
export type ResourceType = (typeof ResourceType)[keyof typeof ResourceType];
export const RoleType = {
Admin: "Admin",
Editor: "Editor",
Publisher: "Publisher",
} as const
export type RoleType = (typeof RoleType)[keyof typeof RoleType]
Admin: "Admin",
Editor: "Editor",
Publisher: "Publisher"
} as const;
export type RoleType = (typeof RoleType)[keyof typeof RoleType];
183 changes: 95 additions & 88 deletions apps/studio/prisma/generated/generatedTypes.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,97 @@
import type { ColumnType, GeneratedAlways } from "kysely"
import type { ColumnType, GeneratedAlways } from "kysely";
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;

import type { ResourceState, ResourceType, RoleType } from "./generatedEnums"
import type { ResourceState, ResourceType, RoleType } from "./generatedEnums";

export type Generated<T> =
T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>
export type Timestamp = ColumnType<Date, Date | string, Date | string>

export interface Blob {
id: GeneratedAlways<string>
/**
* @kyselyType(PrismaJson.BlobJsonContent)
* [BlobJsonContent]
*/
content: PrismaJson.BlobJsonContent
}
export interface Footer {
id: GeneratedAlways<number>
siteId: number
/**
* @kyselyType(PrismaJson.FooterJsonContent)
* [FooterJsonContent]
*/
content: PrismaJson.FooterJsonContent
}
export interface Navbar {
id: GeneratedAlways<number>
siteId: number
/**
* @kyselyType(PrismaJson.NavbarJsonContent)
* [NavbarJsonContent]
*/
content: PrismaJson.NavbarJsonContent
}
export interface Permission {
id: GeneratedAlways<number>
resourceId: string
userId: string
role: RoleType
}
export interface Resource {
id: GeneratedAlways<string>
title: string
permalink: string
siteId: number
parentId: string | null
mainBlobId: string | null
draftBlobId: string | null
state: Generated<ResourceState | null>
type: ResourceType
}
export interface Site {
id: GeneratedAlways<number>
name: string
/**
* @kyselyType(PrismaJson.SiteJsonConfig)
* [SiteJsonConfig]
*/
config: PrismaJson.SiteJsonConfig
}
export interface SiteMember {
userId: string
siteId: number
}
export interface User {
id: string
name: string
email: string
phone: string
preferredName: string | null
}
export interface VerificationToken {
identifier: string
token: string
attempts: Generated<number>
expires: Timestamp
}
export interface DB {
Blob: Blob
Footer: Footer
Navbar: Navbar
Permission: Permission
Resource: Resource
Site: Site
SiteMember: SiteMember
User: User
VerificationToken: VerificationToken
}
export type Blob = {
id: GeneratedAlways<string>;
/**
* @kyselyType(PrismaJson.BlobJsonContent)
* [BlobJsonContent]
*/
content: PrismaJson.BlobJsonContent;
};
export type Footer = {
id: GeneratedAlways<number>;
siteId: number;
/**
* @kyselyType(PrismaJson.FooterJsonContent)
* [FooterJsonContent]
*/
content: PrismaJson.FooterJsonContent;
};
export type Navbar = {
id: GeneratedAlways<number>;
siteId: number;
/**
* @kyselyType(PrismaJson.NavbarJsonContent)
* [NavbarJsonContent]
*/
content: PrismaJson.NavbarJsonContent;
};
export type Permission = {
id: GeneratedAlways<number>;
resourceId: string;
userId: string;
role: RoleType;
};
export type Resource = {
id: GeneratedAlways<string>;
title: string;
permalink: string;
siteId: number;
parentId: string | null;
publishedVersionId: string | null;
draftBlobId: string | null;
state: Generated<ResourceState | null>;
type: ResourceType;
};
export type Site = {
id: GeneratedAlways<number>;
name: string;
/**
* @kyselyType(PrismaJson.SiteJsonConfig)
* [SiteJsonConfig]
*/
config: PrismaJson.SiteJsonConfig;
};
export type SiteMember = {
userId: string;
siteId: number;
};
export type User = {
id: string;
name: string;
email: string;
phone: string;
preferredName: string | null;
};
export type VerificationToken = {
identifier: string;
token: string;
attempts: Generated<number>;
expires: Timestamp;
};
export type Version = {
id: GeneratedAlways<string>;
versionNum: number;
resourceId: string;
blobId: string;
publishedAt: Generated<Timestamp>;
publishedBy: string;
};
export type DB = {
Blob: Blob;
Footer: Footer;
Navbar: Navbar;
Permission: Permission;
Resource: Resource;
Site: Site;
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,43 @@
/*
Warnings:

- You are about to drop the column `mainBlobId` on the `Resource` table. All the data in the column will be lost.
- A unique constraint covering the columns `[publishedVersionId]` on the table `Resource` will be added. If there are existing duplicate values, this will fail.

*/
-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_mainBlobId_fkey";

-- DropIndex
DROP INDEX "Resource_mainBlobId_key";

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

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

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

-- CreateIndex
CREATE UNIQUE INDEX "Version_blobId_key" ON "Version"("blobId");

-- CreateIndex
CREATE UNIQUE INDEX "Resource_publishedVersionId_key" ON "Resource"("publishedVersionId");

-- AddForeignKey
ALTER TABLE "Version" ADD CONSTRAINT "Version_blobId_fkey" FOREIGN KEY ("blobId") REFERENCES "Blob"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Version" ADD CONSTRAINT "Version_publishedBy_fkey" FOREIGN KEY ("publishedBy") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Resource" ADD CONSTRAINT "Resource_publishedVersionId_fkey" FOREIGN KEY ("publishedVersionId") REFERENCES "Version"("id") ON DELETE SET NULL ON UPDATE CASCADE;
40 changes: 26 additions & 14 deletions apps/studio/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ model VerificationToken {
expires DateTime
}

model Version {
id BigInt @id @default(autoincrement())
versionNum Int
resourceId BigInt
resources Resource[]
blobId BigInt @unique
blob Blob @relation(fields: [blobId], references: [id])
harishv7 marked this conversation as resolved.
Show resolved Hide resolved
publishedAt DateTime @default(now())
publishedBy String
publisher User @relation(fields: [publishedBy], references: [id])
}
harishv7 marked this conversation as resolved.
Show resolved Hide resolved

model Resource {
id BigInt @id @default(autoincrement())
title String
Expand All @@ -46,16 +58,25 @@ model Resource {

permission Permission[]

mainBlob Blob? @relation("MainBlob", fields: [mainBlobId], references: [id])
mainBlobId BigInt? @unique
publishedVersionId BigInt? @unique
publishedVersion Version? @relation(fields: [publishedVersionId], references: [id])

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
draftResource Resource?
version Version?
}

enum ResourceState {
Draft
Published
Expand All @@ -73,8 +94,9 @@ model User {
phone String
preferredName String?

permission Permission[]
permissions Permission[]
siteMembers SiteMember[]
versions Version[]
}

model Permission {
Expand Down Expand Up @@ -124,16 +146,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.publishedVersionId) {
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 @@ -78,7 +78,7 @@ export const pageRouter = router({
"Resource.id",
"Resource.permalink",
"Resource.title",
"Resource.mainBlobId",
"Resource.publishedVersionId",
"Resource.draftBlobId",
"Resource.type",
])
Expand Down
Loading
Loading