feat: Add database initialization script and default admin user seeding.

This commit is contained in:
Michael committed 2026-03-10 11:41:44 +01:00
1 parent 47872a994f
commit 086da9dfde
2 files changed
+12 -12

No files matched your search

+11
View File
@@ -63,6 +63,17 @@ async function main() {
`); `);
console.log("[DB Init] Table session_snapshots is verified/created."); console.log("[DB Init] Table session_snapshots is verified/created.");
await tempPool.query(`
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"username" varchar(50) NOT NULL UNIQUE,
"password_hash" text NOT NULL,
"role" varchar(20) NOT NULL DEFAULT 'user',
"created_at" timestamp DEFAULT now()
);
`);
console.log("[DB Init] Table users is verified/created.");
await tempPool.end(); await tempPool.end();
console.log("[DB Init] Initialization successful. Exiting."); console.log("[DB Init] Initialization successful. Exiting.");
process.exit(0); process.exit(0);
+1 -12
View File
@@ -10,18 +10,7 @@ import { count, eq, sql } from "drizzle-orm";
*/ */
export async function ensureAdminExists() { export async function ensureAdminExists() {
try { try {
// 1. Auto-repair: Ensure table exists // 1. Check if admin specifically exists
await db.execute(sql`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
role VARCHAR(20) NOT NULL DEFAULT 'user',
created_at TIMESTAMP DEFAULT NOW()
);
`);
// 2. Check if admin specifically exists
const adminUser = await db.select() const adminUser = await db.select()
.from(users) .from(users)
.where(eq(users.username, "admin")) .where(eq(users.username, "admin"))