Skip to content

Commit 133c64b

Browse files
committed
PEER-255 Standardize seed with questions
Signed-off-by: SeeuSim <[email protected]>
1 parent da62ebf commit 133c64b

File tree

5 files changed

+74
-8
lines changed

5 files changed

+74
-8
lines changed

backend/question/drizzle/0000_initial_schema.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
DO $$ BEGIN
2+
CREATE TYPE "public"."action" AS ENUM('SEED');
3+
EXCEPTION
4+
WHEN duplicate_object THEN null;
5+
END $$;
6+
--> statement-breakpoint
7+
CREATE TABLE IF NOT EXISTS "admin" (
8+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
9+
"created_at" timestamp DEFAULT now(),
10+
"action" "action" NOT NULL
11+
);
12+
--> statement-breakpoint
113
CREATE TABLE IF NOT EXISTS "questions" (
214
"id" serial PRIMARY KEY NOT NULL,
315
"title" varchar(255) NOT NULL,

backend/question/drizzle/meta/0000_snapshot.json

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,40 @@
11
{
2-
"id": "537c03f5-13dd-427a-ae0d-cfd203da09ed",
2+
"id": "84b2ca8d-3021-496f-8769-bbc4dada6468",
33
"prevId": "00000000-0000-0000-0000-000000000000",
44
"version": "7",
55
"dialect": "postgresql",
66
"tables": {
7+
"public.admin": {
8+
"name": "admin",
9+
"schema": "",
10+
"columns": {
11+
"id": {
12+
"name": "id",
13+
"type": "uuid",
14+
"primaryKey": true,
15+
"notNull": true,
16+
"default": "gen_random_uuid()"
17+
},
18+
"created_at": {
19+
"name": "created_at",
20+
"type": "timestamp",
21+
"primaryKey": false,
22+
"notNull": false,
23+
"default": "now()"
24+
},
25+
"action": {
26+
"name": "action",
27+
"type": "action",
28+
"typeSchema": "public",
29+
"primaryKey": false,
30+
"notNull": true
31+
}
32+
},
33+
"indexes": {},
34+
"foreignKeys": {},
35+
"compositePrimaryKeys": {},
36+
"uniqueConstraints": {}
37+
},
738
"public.questions": {
839
"name": "questions",
940
"schema": "",
@@ -59,7 +90,15 @@
5990
"uniqueConstraints": {}
6091
}
6192
},
62-
"enums": {},
93+
"enums": {
94+
"public.action": {
95+
"name": "action",
96+
"schema": "public",
97+
"values": [
98+
"SEED"
99+
]
100+
}
101+
},
63102
"schemas": {},
64103
"sequences": {},
65104
"_meta": {

backend/question/drizzle/meta/_journal.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{
66
"idx": 0,
77
"version": "7",
8-
"when": 1727595727110,
8+
"when": 1728143550719,
99
"tag": "0000_initial_schema",
1010
"breakpoints": true
1111
}

backend/question/src/lib/db/schema.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pgTable, serial, varchar, text, timestamp } from 'drizzle-orm/pg-core';
1+
import { pgEnum, pgTable, serial, text, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';
22

33
export const questions = pgTable('questions', {
44
id: serial('id').primaryKey(),
@@ -9,3 +9,11 @@ export const questions = pgTable('questions', {
99
createdAt: timestamp('created_at', { precision: 6, withTimezone: true }).defaultNow(),
1010
updatedAt: timestamp('updated_at', { precision: 6, withTimezone: true }).defaultNow(),
1111
});
12+
13+
export const actionEnum = pgEnum('action', ['SEED']);
14+
15+
export const admin = pgTable('admin', {
16+
id: uuid('id').primaryKey().notNull().defaultRandom(),
17+
createdAt: timestamp('created_at').defaultNow(),
18+
action: actionEnum('action').notNull(),
19+
});

backend/question/src/lib/db/seed.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import { sql } from 'drizzle-orm';
1+
import { eq, sql } from 'drizzle-orm';
22

3-
import { db, questions } from '@/lib/db';
3+
import { admin as adminTable, db, questions as questionTable } from '@/lib/db';
44
import { questionData } from './sample-data/questions';
55

66
const seedQuestions = async () => {
77
try {
88
await db.transaction(async (trx) => {
9+
const seedRecords = await trx.select().from(adminTable).where(eq(adminTable.action, 'SEED'));
10+
if (seedRecords && seedRecords.length > 0) {
11+
console.info(
12+
`[Users]: Seeded already at: ${(seedRecords[seedRecords.length - 1].createdAt ?? new Date()).toLocaleString()}`
13+
);
14+
return;
15+
}
916
// Delete all questions (not table)
10-
await trx.delete(questions);
17+
await trx.delete(questionTable);
1118

1219
// Reset Serial to start index 1
1320
await trx.execute(sql`
@@ -21,7 +28,7 @@ const seedQuestions = async () => {
2128

2229
for (const question of questionData) {
2330
await trx
24-
.insert(questions)
31+
.insert(questionTable)
2532
.values({ ...question, id: undefined }) // Let DB set ID
2633
.onConflictDoNothing();
2734
}

0 commit comments

Comments
 (0)