Skip to content

Commit

Permalink
Add id to user_item
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkremenetsky committed Mar 21, 2024
1 parent 346f6c5 commit f153425
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
2 changes: 2 additions & 0 deletions packages/core/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ model feed {
}

model user_item {
id String @unique @default(cuid())
item_id String
in_read_later Boolean @default(false)
marked_read Boolean
Expand All @@ -99,6 +100,7 @@ model user_item {
user user @relation(fields: [user_id], references: [id], onDelete: Cascade)
created_at DateTime @default(now())
@@index([id])
@@index([item_id])
@@id([item_id, user_id])
}
Expand Down
42 changes: 42 additions & 0 deletions packages/db/migrations/add_id_to_user_item.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PrismaClient } from "@prisma/client";

const cuid = require("cuid");

const prisma = new PrismaClient();

async function populateUserItemIds() {
const userItems = await prisma.user_item.findMany({
where: {
id: null,
},
});

for (const userItem of userItems) {
await prisma.user_item.update({
where: {
item_id_user_id: {
item_id: userItem.item_id,
user_id: userItem.user_id,
},
},
data: {
id: cuid(),
},
});
}
}

const main = async () => {
populateUserItemIds();
prisma
.$disconnect()
.catch((error) => {
console.error(error);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
};

main();
8 changes: 5 additions & 3 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
"license": "MIT",
"scripts": {
"clean": "rm -rf .turbo node_modules",
"db:generate": "pnpm with-env prisma generate",
"db:push": "pnpm with-env prisma db push --skip-generate",
"generate": "pnpm with-env prisma generate",
"push": "prisma db push",
"studio": "pnpm with-env prisma studio --port 5556",
"with-env": "dotenv -e ../../.env --"
"with-env": "dotenv -e ../../.env --",
"add_id": "pnpm tsx migrations/add_id_to_user_item.ts"
},
"dependencies": {
"@prisma/client": "5.11.0",
"@prisma/extension-accelerate": "0.6.2",
"@types/ioredis": "^5.0.0",
"@upstash/redis": "^1.22.0",
"cuid": "^3.0.0",
"ioredis": "^5.3.2",
"pg": "^8.11.3",
"prisma-redis-middleware": "^4.8.0"
Expand Down
2 changes: 2 additions & 0 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ model feed {
}

model user_item {
id String @unique @default(cuid())
item_id String
in_read_later Boolean @default(false)
marked_read Boolean
Expand All @@ -140,6 +141,7 @@ model user_item {
user user @relation(fields: [user_id], references: [id], onDelete: Cascade)
created_at DateTime @default(now())
@@index([id])
@@index([item_id])
@@id([item_id, user_id])
}
Expand Down

0 comments on commit f153425

Please sign in to comment.