diff --git a/server/setup/migrationsPg.ts b/server/setup/migrationsPg.ts index afb8d61af..ed7c7f9fd 100644 --- a/server/setup/migrationsPg.ts +++ b/server/setup/migrationsPg.ts @@ -28,8 +28,7 @@ import m19 from "./scriptsPg/1.18.4"; import m20 from "./scriptsPg/1.19.0"; import m21 from "./scriptsPg/1.20.0"; import m22 from "./scriptsPg/1.21.0"; -import m23 from "./scriptsPg/1.21.1"; -import m24 from "./scriptsPg/1.22.0"; +import m23 from "./scriptsPg/1.22.0"; // THIS CANNOT IMPORT ANYTHING FROM THE SERVER // EXCEPT FOR THE DATABASE AND THE SCHEMA @@ -58,8 +57,7 @@ const migrations = [ { version: "1.19.0", run: m20 }, { version: "1.20.0", run: m21 }, { version: "1.21.0", run: m22 }, - { version: "1.21.1", run: m23 }, - { version: "1.22.0", run: m24 } + { version: "1.22.0", run: m23 } // Add new migrations here as they are created ] as { version: string; diff --git a/server/setup/migrationsSqlite.ts b/server/setup/migrationsSqlite.ts index d68eb195e..9e5b384ee 100644 --- a/server/setup/migrationsSqlite.ts +++ b/server/setup/migrationsSqlite.ts @@ -47,8 +47,7 @@ import m41 from "./scriptsSqlite/1.19.0"; import m42 from "./scriptsSqlite/1.19.1"; import m43 from "./scriptsSqlite/1.20.0"; import m44 from "./scriptsSqlite/1.21.0"; -import m45 from "./scriptsSqlite/1.21.1"; -import m46 from "./scriptsSqlite/1.22.0"; +import m45 from "./scriptsSqlite/1.22.0"; // THIS CANNOT IMPORT ANYTHING FROM THE SERVER // EXCEPT FOR THE DATABASE AND THE SCHEMA @@ -94,8 +93,7 @@ const migrations = [ { version: "1.19.1", run: m42 }, { version: "1.20.0", run: m43 }, { version: "1.21.0", run: m44 }, - { version: "1.21.1", run: m45 }, - { version: "1.22.0", run: m46 } + { version: "1.22.0", run: m45 } // Add new migrations here as they are created ] as const; diff --git a/server/setup/scriptsPg/1.21.1.ts b/server/setup/scriptsPg/1.21.1.ts deleted file mode 100644 index d4c375bd8..000000000 --- a/server/setup/scriptsPg/1.21.1.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { db } from "@server/db/pg/driver"; -import { sql } from "drizzle-orm"; - -const version = "1.21.1"; - -const actionsToGrant = ["getSiteResource", "listSiteResources"] as const; - -export default async function migration() { - console.log(`Running setup script ${version}...`); - - try { - await db.execute(sql`BEGIN`); - - for (const actionId of actionsToGrant) { - await db.execute(sql` - INSERT INTO "roleActions" ("roleId", "actionId", "orgId") - SELECT r."roleId", ${actionId}, r."orgId" - FROM "roles" r - WHERE COALESCE(r."isAdmin", false) = false - AND NOT EXISTS ( - SELECT 1 FROM "roleActions" ra - WHERE ra."roleId" = r."roleId" - AND ra."actionId" = ${actionId} - AND ra."orgId" = r."orgId" - ); - `); - } - - await db.execute(sql`COMMIT`); - console.log(`Finished setup script ${version}`); - } catch (e) { - await db.execute(sql`ROLLBACK`); - console.log("Unable to migrate database"); - console.log(e); - throw e; - } -} diff --git a/server/setup/scriptsPg/1.22.0.ts b/server/setup/scriptsPg/1.22.0.ts index 499a432af..6b8f19b6c 100644 --- a/server/setup/scriptsPg/1.22.0.ts +++ b/server/setup/scriptsPg/1.22.0.ts @@ -9,6 +9,8 @@ import { fromZodError } from "zod-validation-error"; const version = "1.22.0"; +const actionsToGrant = ["getSiteResource", "listSiteResources"] as const; + export default async function migration() { console.log(`Running setup script ${version}...`); @@ -16,9 +18,23 @@ export default async function migration() { await db.execute(sql`BEGIN`); await db.execute(sql` - `); + for (const actionId of actionsToGrant) { + await db.execute(sql` + INSERT INTO "roleActions" ("roleId", "actionId", "orgId") + SELECT r."roleId", ${actionId}, r."orgId" + FROM "roles" r + WHERE COALESCE(r."isAdmin", false) = false + AND NOT EXISTS ( + SELECT 1 FROM "roleActions" ra + WHERE ra."roleId" = r."roleId" + AND ra."actionId" = ${actionId} + AND ra."orgId" = r."orgId" + ); + `); + } + await db.execute(sql`COMMIT`); console.log("Migrated database"); } catch (e) { diff --git a/server/setup/scriptsSqlite/1.21.1.ts b/server/setup/scriptsSqlite/1.21.1.ts deleted file mode 100644 index 405749dfd..000000000 --- a/server/setup/scriptsSqlite/1.21.1.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { APP_PATH } from "@server/lib/consts"; -import Database from "better-sqlite3"; -import path from "path"; - -const version = "1.21.1"; - -const actionsToGrant = ["getSiteResource", "listSiteResources"] as const; - -export default async function migration() { - console.log(`Running setup script ${version}...`); - - const location = path.join(APP_PATH, "db", "db.sqlite"); - const db = new Database(location); - - try { - db.transaction(() => { - const insertRoleAction = db.prepare(` - INSERT INTO 'roleActions' ("roleId", "actionId", "orgId") - SELECT r."roleId", ?, r."orgId" - FROM 'roles' r - WHERE COALESCE(r."isAdmin", 0) = 0 - AND NOT EXISTS ( - SELECT 1 FROM 'roleActions' ra - WHERE ra."roleId" = r."roleId" - AND ra."actionId" = ? - AND ra."orgId" = r."orgId" - ); - `); - - for (const actionId of actionsToGrant) { - insertRoleAction.run(actionId, actionId); - } - })(); - - console.log(`Finished setup script ${version}`); - } catch (e) { - console.log("Unable to migrate database"); - console.log(e); - throw e; - } finally { - db.close(); - } -} diff --git a/server/setup/scriptsSqlite/1.22.0.ts b/server/setup/scriptsSqlite/1.22.0.ts index 854f7392c..6ca6bd19d 100644 --- a/server/setup/scriptsSqlite/1.22.0.ts +++ b/server/setup/scriptsSqlite/1.22.0.ts @@ -8,6 +8,8 @@ import { fromZodError } from "zod-validation-error"; const version = "1.22.0"; +const actionsToGrant = ["getSiteResource", "listSiteResources"] as const; + export default async function migration() { console.log(`Running setup script ${version}...`); @@ -22,6 +24,23 @@ export default async function migration() { ` ` ).run(); + + const insertRoleAction = db.prepare(` + INSERT INTO 'roleActions' ("roleId", "actionId", "orgId") + SELECT r."roleId", ?, r."orgId" + FROM 'roles' r + WHERE COALESCE(r."isAdmin", 0) = 0 + AND NOT EXISTS ( + SELECT 1 FROM 'roleActions' ra + WHERE ra."roleId" = r."roleId" + AND ra."actionId" = ? + AND ra."orgId" = r."orgId" + ); + `); + + for (const actionId of actionsToGrant) { + insertRoleAction.run(actionId, actionId); + } })(); db.pragma("foreign_keys = ON");