Refactor database initialization by removing comments

Removed comments related to initial schema bootstrap and version marker for clarity. Simplified the database initialization logic.
This commit is contained in:
Kosztyk 2026-01-14 15:18:31 +02:00 committed by GitHub
parent 6507bc72fe
commit a30ab9973c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -7,9 +7,6 @@ const db = new Database("./data/mydb.sqlite", { create: true });
// Always keep foreign keys on (SQLite defaults to off). // Always keep foreign keys on (SQLite defaults to off).
db.exec("PRAGMA foreign_keys = ON;"); db.exec("PRAGMA foreign_keys = ON;");
// -----------------------------------------------------------------------------
// Initial schema bootstrap (fresh install)
// -----------------------------------------------------------------------------
const hasAnyTable = db.query("SELECT 1 FROM sqlite_master WHERE type='table' LIMIT 1").get(); const hasAnyTable = db.query("SELECT 1 FROM sqlite_master WHERE type='table' LIMIT 1").get();
if (!hasAnyTable) { if (!hasAnyTable) {
db.exec(` db.exec(`
@ -43,9 +40,7 @@ PRAGMA user_version = 1;
`); `);
} }
// -----------------------------------------------------------------------------
// Version marker (kept for backwards-compatibility; schema is enforced below).
// -----------------------------------------------------------------------------
const dbVersion = (db.query("PRAGMA user_version").get() as { user_version?: number }) const dbVersion = (db.query("PRAGMA user_version").get() as { user_version?: number })
.user_version; .user_version;
@ -54,11 +49,6 @@ if ((dbVersion ?? 0) === 0) {
console.log("Updated database to version 1."); console.log("Updated database to version 1.");
} }
// -----------------------------------------------------------------------------
// One-time migration: Policy A
// If the `role` column is newly added AND there are existing users,
// make the oldest user (lowest id) an admin.
// -----------------------------------------------------------------------------
const userColumns = db.query("PRAGMA table_info(users)").all() as { name: string }[]; const userColumns = db.query("PRAGMA table_info(users)").all() as { name: string }[];
const hasRoleColumn = userColumns.some((col) => col.name === "role"); const hasRoleColumn = userColumns.some((col) => col.name === "role");