add sqlite migration

This commit is contained in:
miloschwartz
2025-12-21 15:32:43 -05:00
parent 0aa56d441e
commit 81149085fa
2 changed files with 102 additions and 1 deletions

View File

@@ -33,6 +33,7 @@ import m28 from "./scriptsSqlite/1.11.0";
import m29 from "./scriptsSqlite/1.11.1";
import m30 from "./scriptsSqlite/1.12.0";
import m31 from "./scriptsSqlite/1.13.0";
import m32 from "./scriptsSqlite/1.14.0";
// THIS CANNOT IMPORT ANYTHING FROM THE SERVER
// EXCEPT FOR THE DATABASE AND THE SCHEMA
@@ -64,7 +65,8 @@ const migrations = [
{ version: "1.11.0", run: m28 },
{ version: "1.11.1", run: m29 },
{ version: "1.12.0", run: m30 },
{ version: "1.13.0", run: m31 }
{ version: "1.13.0", run: m31 },
{ version: "1.14.0", run: m32 }
// Add new migrations here as they are created
] as const;

View File

@@ -0,0 +1,99 @@
import { __DIRNAME, APP_PATH } from "@server/lib/consts";
import Database from "better-sqlite3";
import path from "path";
const version = "1.14.0";
await migration();
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.pragma("foreign_keys = OFF");
db.transaction(() => {
db.prepare(
`
CREATE TABLE 'loginPageBranding' (
'loginPageBrandingId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'logoUrl' text NOT NULL,
'logoWidth' integer NOT NULL,
'logoHeight' integer NOT NULL,
'primaryColor' text,
'resourceTitle' text NOT NULL,
'resourceSubtitle' text,
'orgTitle' text,
'orgSubtitle' text
);
`
).run();
db.prepare(
`
CREATE TABLE 'loginPageBrandingOrg' (
'loginPageBrandingId' integer NOT NULL,
'orgId' text NOT NULL,
FOREIGN KEY ('loginPageBrandingId') REFERENCES 'loginPageBranding'('loginPageBrandingId') ON UPDATE no action ON DELETE cascade,
FOREIGN KEY ('orgId') REFERENCES 'orgs'('orgId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`
CREATE TABLE 'resourceHeaderAuthExtendedCompatibility' (
'headerAuthExtendedCompatibilityId' integer PRIMARY KEY AUTOINCREMENT NOT NULL,
'resourceId' integer NOT NULL,
'extendedCompatibilityIsActivated' integer NOT NULL,
FOREIGN KEY ('resourceId') REFERENCES 'resources'('resourceId') ON UPDATE no action ON DELETE cascade
);
`
).run();
db.prepare(
`ALTER TABLE 'resources' ADD 'maintenanceModeEnabled' integer DEFAULT false NOT NULL;`
).run();
db.prepare(
`ALTER TABLE 'resources' ADD 'maintenanceModeType' text DEFAULT 'forced';`
).run();
db.prepare(
`ALTER TABLE 'resources' ADD 'maintenanceTitle' text;`
).run();
db.prepare(
`ALTER TABLE 'resources' ADD 'maintenanceMessage' text;`
).run();
db.prepare(
`ALTER TABLE 'resources' ADD 'maintenanceEstimatedTime' text;`
).run();
db.prepare(
`ALTER TABLE 'siteResources' ADD 'tcpPortRangeString' text;`
).run();
db.prepare(
`ALTER TABLE 'siteResources' ADD 'udpPortRangeString' text;`
).run();
db.prepare(
`ALTER TABLE 'siteResources' ADD 'disableIcmp' integer;`
).run();
})();
db.pragma("foreign_keys = ON");
console.log(`Migrated database`);
} catch (e) {
console.log("Failed to migrate db:", e);
throw e;
}
console.log(`${version} migration complete`);
}