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

update to use bigint for resourceId and blobId #365

Merged
merged 1 commit into from
Jul 29, 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
12 changes: 6 additions & 6 deletions apps/studio/prisma/generated/generatedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type Generated<T> =
export type Timestamp = ColumnType<Date, Date | string, Date | string>

export interface Blob {
id: GeneratedAlways<number>
id: GeneratedAlways<string>
/**
* @kyselyType(PrismaJson.BlobJsonContent)
* [BlobJsonContent]
Expand All @@ -36,18 +36,18 @@ export interface Navbar {
}
export interface Permission {
id: GeneratedAlways<number>
resourceId: number
resourceId: string
userId: string
role: RoleType
}
export interface Resource {
id: GeneratedAlways<number>
id: GeneratedAlways<string>
title: string
permalink: string
siteId: number
parentId: number | null
mainBlobId: number | null
draftBlobId: number | null
parentId: string | null
mainBlobId: string | null
draftBlobId: string | null
state: Generated<ResourceState | null>
type: ResourceType
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 primary key for the `Resource` table will be changed. If it partially fails, the table could be left without primary key constraint.

*/
-- DropForeignKey
ALTER TABLE "Permission" DROP CONSTRAINT "Permission_resourceId_fkey";

-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_draftBlobId_fkey";

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

-- DropForeignKey
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_parentId_fkey";

-- AlterTable
ALTER TABLE "Blob" DROP CONSTRAINT "Blob_pkey",
ALTER COLUMN "id" SET DATA TYPE BIGINT,
ADD CONSTRAINT "Blob_pkey" PRIMARY KEY ("id");

-- AlterTable
ALTER TABLE "Permission" ALTER COLUMN "resourceId" SET DATA TYPE BIGINT;

-- AlterTable
ALTER TABLE "Resource" DROP CONSTRAINT "Resource_pkey",
ALTER COLUMN "id" SET DATA TYPE BIGINT,
ALTER COLUMN "parentId" SET DATA TYPE BIGINT,
ALTER COLUMN "draftBlobId" SET DATA TYPE BIGINT,
ALTER COLUMN "mainBlobId" SET DATA TYPE BIGINT,
ADD CONSTRAINT "Resource_pkey" PRIMARY KEY ("id");

-- 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_mainBlobId_fkey" FOREIGN KEY ("mainBlobId") REFERENCES "Blob"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Resource" ADD CONSTRAINT "Resource_draftBlobId_fkey" FOREIGN KEY ("draftBlobId") 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;
16 changes: 8 additions & 8 deletions apps/studio/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,23 @@ model VerificationToken {
}

model Resource {
id Int @id @default(autoincrement())
id BigInt @id @default(autoincrement())
title String
permalink String
siteId Int
site Site @relation(fields: [siteId], references: [id])
parentId Int?
parentId BigInt?
parent Resource? @relation("ParentRelation", fields: [parentId], references: [id])

children Resource[] @relation("ParentRelation")

permission Permission[]

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

draftBlob Blob? @relation("DraftBlob", fields: [draftBlobId], references: [id])
draftBlobId Int? @unique
draftBlob Blob? @relation("DraftBlob", fields: [draftBlobId], references: [id])
draftBlobId BigInt? @unique

state ResourceState? @default(Draft)
type ResourceType
Expand Down Expand Up @@ -79,7 +79,7 @@ model User {

model Permission {
id Int @id @default(autoincrement())
resourceId Int
resourceId BigInt
resource Resource @relation(fields: [resourceId], references: [id])
userId String
user User @relation(fields: [userId], references: [id])
Expand Down Expand Up @@ -125,7 +125,7 @@ model Footer {
}

model Blob {
id Int @id @default(autoincrement())
id BigInt @id @default(autoincrement())
/// @kyselyType(PrismaJson.BlobJsonContent)
/// [BlobJsonContent]
content Json
Expand Down
8 changes: 4 additions & 4 deletions apps/studio/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ async function main() {
)
.execute()

let blobId = 1
let blobId = BigInt(1)
const dedupeBlobId = await db
.selectFrom("Blob")
.where("Blob.id", "=", blobId)
.where("Blob.id", "=", String(blobId))
.select("Blob.id")
.executeTakeFirst()
if (!dedupeBlobId) {
Expand All @@ -217,13 +217,13 @@ async function main() {
.values({ content: jsonb(PAGE_BLOB) })
.returning("id")
.executeTakeFirstOrThrow()
blobId = id
blobId = BigInt(id)
}

await db
.insertInto("Resource")
.values({
mainBlobId: blobId,
mainBlobId: String(blobId),
harishv7 marked this conversation as resolved.
Show resolved Hide resolved
permalink: "home",
siteId,
type: "Page",
Expand Down
4 changes: 2 additions & 2 deletions apps/studio/src/server/modules/folder/folder.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export const folderRouter = router({
const folderResult = await ctx.db
.selectFrom("Resource")
.selectAll("Resource")
.where("id", "=", input.resourceId)
.where("id", "=", String(input.resourceId))
.executeTakeFirstOrThrow()
const childrenResult = await ctx.db
.selectFrom("Resource")
.selectAll("Resource")
.where("parentId", "=", input.resourceId)
.where("parentId", "=", String(input.resourceId))
.execute()
const children = childrenResult.map((c) => {
if (c.draftBlobId || c.mainBlobId) {
Expand Down
4 changes: 2 additions & 2 deletions apps/studio/src/server/modules/page/page.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const pageRouter = router({
.where("Resource.siteId", "=", siteId)

if (resourceId) {
query = query.where("Resource.parentId", "=", resourceId)
query = query.where("Resource.parentId", "=", String(resourceId))
}
return query
.select([
Expand Down Expand Up @@ -126,7 +126,7 @@ export const pageRouter = router({
title,
permalink,
siteId,
parentId: folderId,
parentId: String(folderId),
draftBlobId: blob.id,
type: ResourceType.Page,
})
Expand Down
10 changes: 5 additions & 5 deletions apps/studio/src/server/modules/resource/resource.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const getById = ({
}) =>
db
.selectFrom("Resource")
.where("Resource.id", "=", resourceId)
.where("Resource.id", "=", String(resourceId))
.where("siteId", "=", siteId)

// NOTE: Throw here to fail early if our invariant that a page has a `blobId` is violated
Expand Down Expand Up @@ -102,7 +102,7 @@ export const updatePageById = (
return tx
.updateTable("Resource")
.set(rest)
.where("id", "=", id)
.where("id", "=", String(id))
.executeTakeFirstOrThrow()
})
}
Expand All @@ -120,7 +120,7 @@ export const updateBlobById = (props: {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore excessive deep type instantiaton
.set({ content })
.where("Resource.id", "=", id)
.where("Resource.id", "=", String(id))
.executeTakeFirstOrThrow()
)
}
Expand Down Expand Up @@ -155,8 +155,8 @@ export const moveResource = async (
) => {
return db
.updateTable("Resource")
.set({ parentId: newParentId })
.set({ parentId: String(newParentId) })
.where("siteId", "=", siteId)
.where("id", "=", resourceId)
.where("id", "=", String(resourceId))
.executeTakeFirstOrThrow()
}
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 @@ -10,23 +10,23 @@ const pageListQuery = (wait?: DelayMode | number) => {
}
return [
{
id: 4,
id: "4",
permalink: "test-page-1",
title: "Test page 1",
mainBlobId: 3,
mainBlobId: "3",
draftBlobId: null,
type: "Page",
},
{
id: 5,
id: "5",
permalink: "test-page-2",
title: "Test page 2",
mainBlobId: 4,
mainBlobId: "4",
draftBlobId: null,
type: "Page",
},
{
id: 6,
id: "6",
permalink: "folder",
title: "Test folder 1",
mainBlobId: null,
Expand Down
Loading