This commit is contained in:
@@ -23,3 +23,32 @@ export function migrateAuthSchema(handle: Database.Database): void {
|
||||
CREATE INDEX IF NOT EXISTS auth_audit_created_at ON auth_audit(created_at);
|
||||
`);
|
||||
}
|
||||
|
||||
/** Run with foreign keys disabled by the migration runner before its transaction. */
|
||||
export function migrateSuperViewerRole(handle: Database.Database): void {
|
||||
const schema = handle
|
||||
.prepare("SELECT sql FROM sqlite_master WHERE type='table' AND name='auth_users'")
|
||||
.get() as { sql: string };
|
||||
if (schema.sql.includes("'super_viewer'")) return;
|
||||
if (handle.pragma('foreign_keys', { simple: true }))
|
||||
throw new Error('The auth role migration requires foreign keys to be disabled');
|
||||
|
||||
// Rebuild the parent table without renaming it first, so session references
|
||||
// keep pointing to auth_users and existing sessions are not cascade-deleted.
|
||||
handle.exec(`
|
||||
CREATE TABLE auth_users_super_viewer (
|
||||
id TEXT PRIMARY KEY, username TEXT NOT NULL COLLATE NOCASE UNIQUE,
|
||||
display_name TEXT NOT NULL, password_hash TEXT NOT NULL,
|
||||
role TEXT NOT NULL CHECK(role IN ('admin','super_viewer','operator','viewer')),
|
||||
disabled INTEGER NOT NULL DEFAULT 0, must_change_password INTEGER NOT NULL DEFAULT 0,
|
||||
created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL, last_login_at INTEGER
|
||||
);
|
||||
INSERT INTO auth_users_super_viewer (
|
||||
id,username,display_name,password_hash,role,disabled,must_change_password,
|
||||
created_at,updated_at,last_login_at
|
||||
) SELECT id,username,display_name,password_hash,role,disabled,must_change_password,
|
||||
created_at,updated_at,last_login_at FROM auth_users;
|
||||
DROP TABLE auth_users;
|
||||
ALTER TABLE auth_users_super_viewer RENAME TO auth_users;
|
||||
`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user