31 lines
923 B
TypeScript
31 lines
923 B
TypeScript
/**
|
|
* Adds `users.role` and makes every legacy `is_admin` user an admin.
|
|
*
|
|
* Idempotent: safe to run against a fresh db (already created from schema.sql
|
|
* with the `role` column) or an existing one that predates it.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import type { DatabaseSync } from "node:sqlite";
|
|
|
|
/** Applies this migration to `db`. */
|
|
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';`,
|
|
);
|
|
}
|
|
|
|
// Backfill existing admins to role 'admin'. Guarded because the legacy
|
|
// is_admin column is absent on fresh databases (and dropped by 0005).
|
|
if (userCols.some((c) => c.name === "is_admin")) {
|
|
db.exec(
|
|
`UPDATE users SET role = 'admin' WHERE is_admin = 1 AND role != 'admin';`,
|
|
);
|
|
}
|
|
}
|