37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
"use strict";
|
|
/**
|
|
* One-shot migration: grant super_admin to all users who currently have admin role.
|
|
*
|
|
* From repo root (Docker / VPS):
|
|
* npm run phase-b:grant-super-admin
|
|
*
|
|
* From backend/ (local dev, DB reachable):
|
|
* npm run grant-super-admin
|
|
*/
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const connection_1 = require("../db/connection");
|
|
async function main() {
|
|
const adminRole = await (0, connection_1.query)(`SELECT id FROM roles WHERE name = 'admin'`);
|
|
const superRole = await (0, connection_1.query)(`SELECT id FROM roles WHERE name = 'super_admin'`);
|
|
if (!adminRole[0]) {
|
|
throw new Error('Role "admin" not found. Ensure the database is initialized (at least one admin user should exist).');
|
|
}
|
|
if (!superRole[0]) {
|
|
throw new Error('Role "super_admin" not found. Run migrations first:\n' +
|
|
' From repo root: npm run db:migrate\n' +
|
|
' Then retry: npm run phase-b:grant-super-admin');
|
|
}
|
|
const result = await (0, connection_1.query)(`INSERT INTO user_roles (user_id, role_id)
|
|
SELECT ur.user_id, $2
|
|
FROM user_roles ur
|
|
WHERE ur.role_id = $1
|
|
ON CONFLICT DO NOTHING
|
|
RETURNING user_id`, [adminRole[0].id, superRole[0].id]);
|
|
console.log(`Granted super_admin to ${result.length} administrator(s).`);
|
|
process.exit(0);
|
|
}
|
|
main().catch((e) => {
|
|
console.error(e);
|
|
process.exit(1);
|
|
});
|
|
//# sourceMappingURL=grantSuperAdminToAdmins.js.map
|