diff --git a/artifacts/api-server/src/index.ts b/artifacts/api-server/src/index.ts index 251ed56..1a3cfed 100644 --- a/artifacts/api-server/src/index.ts +++ b/artifacts/api-server/src/index.ts @@ -18,6 +18,26 @@ if (Number.isNaN(port) || port <= 0) { throw new Error(`Invalid PORT value: "${rawPort}"`); } +async function ensureSessionsTable(): Promise { + try { + const exists = await db.execute( + sql`SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'sessions')`, + ); + const rows = exists.rows as [{ exists: boolean }]; + if (rows[0]?.exists) return; + + await db.execute( + sql`CREATE TABLE "sessions" ("sid" varchar NOT NULL, "sess" json NOT NULL, "expire" timestamp(6) NOT NULL)`, + ); + await db.execute( + sql`ALTER TABLE "sessions" ADD PRIMARY KEY ("sid")`, + ); + logger.info("Sessions table created"); + } catch (err) { + logger.error({ err }, "Failed to ensure sessions table"); + } +} + async function seedAdminUser(): Promise { try { const [row] = await db.select({ count: sql`count(*)::int` }).from(usersTable); @@ -49,5 +69,6 @@ app.listen(port, (err) => { } logger.info({ port }, "Server listening"); + ensureSessionsTable(); seedAdminUser(); }); diff --git a/lib/db/src/schema/index.ts b/lib/db/src/schema/index.ts index 3ba00f4..481450a 100644 --- a/lib/db/src/schema/index.ts +++ b/lib/db/src/schema/index.ts @@ -2,3 +2,4 @@ export * from "./tools"; export * from "./ratings"; export * from "./users"; export * from "./audit-logs"; +export * from "./sessions"; diff --git a/lib/db/src/schema/sessions.ts b/lib/db/src/schema/sessions.ts new file mode 100644 index 0000000..6e74565 --- /dev/null +++ b/lib/db/src/schema/sessions.ts @@ -0,0 +1,7 @@ +import { pgTable, text, json, timestamp } from "drizzle-orm/pg-core"; + +export const sessionsTable = pgTable("sessions", { + sid: text("sid").notNull().primaryKey(), + sess: json("sess").notNull(), + expire: timestamp("expire", { withTimezone: true, precision: 6 }).notNull(), +});