feat: Introduce ensureAdminExists function to seed a default admin user and create the users table if needed.

This commit is contained in:
Michael committed 2026-03-10 12:03:18 +01:00
1 parent 086da9dfde
commit 4819ad7743
1 file changed
+12 -1
+12 -1
View File
@@ -10,7 +10,18 @@ import { count, eq, sql } from "drizzle-orm";
*/
export async function ensureAdminExists() {
try {
// 1. Check if admin specifically exists
// 1. Auto-repair: Ensure table exists (Fallback if db-init failed)
// Note: Using raw SQL to avoid Drizzle mapping issues during initialization
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()
);
`);
console.log("[AUTH] users table verified/created.");
const adminUser = await db.select()
.from(users)
.where(eq(users.username, "admin"))