/** * Adds `users.role`, which replaces the `is_admin` flag so that moderators can * exist between users and admins. 0005 then drops the flag. * * @module */ import type { DatabaseSync } from "node:sqlite"; /** * Run once by `runMigrations`. Every user becomes a `user`, except those with * `is_admin` set, who become admins. */ export function up(db: DatabaseSync): void { const userCols = db.prepare(`PRAGMA table_info(users);`).all() as { name: string; }[]; if (!userCols.some((c) => c.name === "role")) { db.exec( `ALTER TABLE users ADD COLUMN role TEXT NOT NULL DEFAULT 'user';`, ); } // schema.sql never had is_admin. if (userCols.some((c) => c.name === "is_admin")) { db.exec( `UPDATE users SET role = 'admin' WHERE is_admin = 1 AND role != 'admin';`, ); } }