Skip to content

Commit da62ebf

Browse files
committed
PEER-255 Fix Seeding logic
Signed-off-by: SeeuSim <[email protected]>
1 parent 14e0ef5 commit da62ebf

File tree

7 files changed

+77
-23
lines changed

7 files changed

+77
-23
lines changed

backend/user/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 "users" (
214
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
315
"email" varchar(255) NOT NULL,

backend/user/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": "d0b8800a-d4b6-49f8-b2cf-57f4a64fc64a",
2+
"id": "f01e3d91-1038-48b8-a073-a6a9a8a308fd",
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.users": {
839
"name": "users",
940
"schema": "",
@@ -86,7 +117,15 @@
86117
}
87118
}
88119
},
89-
"enums": {},
120+
"enums": {
121+
"public.action": {
122+
"name": "action",
123+
"schema": "public",
124+
"values": [
125+
"SEED"
126+
]
127+
}
128+
},
90129
"schemas": {},
91130
"sequences": {},
92131
"_meta": {

backend/user/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": 1727623675374,
8+
"when": 1728143079049,
99
"tag": "0000_initial_schema",
1010
"breakpoints": true
1111
}

backend/user/entrypoint.sh

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#!/bin/sh
2-
if [ ! -f db-init ]
3-
then
4-
echo "migrating..."
2+
3+
# Drizzle will handle its own logic to remove conflicts
54
npm run db:prod:migrate
6-
echo "migration complete"
7-
echo "seeding..."
5+
6+
# Checks admin table and will not seed if data exists
87
npm run db:prod:seed
9-
echo "seeding complete"
10-
touch .db-init
11-
echo "db-init created"
12-
fi
138

149
npm run start

backend/user/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 { integer, pgTable, smallint, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';
1+
import { integer, pgEnum, pgTable, smallint, timestamp, uuid, varchar } from 'drizzle-orm/pg-core';
22

33
// Define the user table
44
export const users = pgTable('users', {
@@ -12,3 +12,11 @@ export const users = pgTable('users', {
1212
unlockTime: timestamp('unlock_time', { precision: 6, withTimezone: true }), // If failed counts > limit, block all attempts until this time.
1313
attemptedQuestions: integer('attempted_questions').array(),
1414
});
15+
16+
export const actionEnum = pgEnum('action', ['SEED']);
17+
18+
export const admin = pgTable('admin', {
19+
id: uuid('id').primaryKey().notNull().defaultRandom(),
20+
createdAt: timestamp('created_at').defaultNow(),
21+
action: actionEnum('action').notNull(),
22+
});

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { generatePasswordHash } from '@/lib/passwords';
2-
import { db, users as usersTable } from '.';
2+
import { admin as adminTable, db, users as usersTable } from '.';
3+
import { eq } from 'drizzle-orm';
34

45
const TEST_USER_CREDENTIALS = {
56
username: 'testuser01',
@@ -13,6 +14,13 @@ const main = async () => {
1314
await db.transaction(async (tx) => {
1415
// Clear all users
1516
try {
17+
const seedRecords = await tx.select().from(adminTable).where(eq(adminTable.action, 'SEED'));
18+
if (seedRecords && seedRecords.length > 0) {
19+
console.info(
20+
`[Users]: Seeded already at: ${(seedRecords[seedRecords.length - 1].createdAt ?? new Date()).toLocaleString()}`
21+
);
22+
return;
23+
}
1624
await tx.delete(usersTable);
1725

1826
const password = generatePasswordHash(TEST_USER_CREDENTIALS.password);
@@ -24,6 +32,7 @@ const main = async () => {
2432
password,
2533
})
2634
.onConflictDoNothing();
35+
await tx.insert(adminTable).values({ action: 'SEED' });
2736
} catch (error) {
2837
console.error('[Users]: An error occurred while seeding: ' + String(error));
2938
process.exit(1);

docker-compose.yaml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ services:
8484
# Docker Compose Specific for Service Discovery
8585
- EXPRESS_DB_HOST=user-db
8686
- EXPRESS_DB_PORT=5432
87-
volumes:
88-
- user-service:/data/user-express
8987
depends_on:
9088
user-db:
9189
condition: service_healthy
@@ -96,13 +94,6 @@ services:
9694
entrypoint: ["/bin/sh", "entrypoint.sh"]
9795

9896
volumes:
99-
user-service:
100-
external: false
101-
question-service:
102-
external: false
103-
collab-service:
104-
external: false
105-
10697
# Persistent Volumes for Databases
10798
user-db-docker:
10899
external: true

0 commit comments

Comments
 (0)