migrations

This commit is contained in:
miloschwartz 2025-08-18 15:43:48 -07:00
parent 4bcb4a1590
commit 49f84bccad
No known key found for this signature in database
2 changed files with 20 additions and 15 deletions

View file

@ -3,8 +3,6 @@ import { sql } from "drizzle-orm";
const version = "1.9.0";
await migration();
export default async function migration() {
console.log(`Running setup script ${version}...`);

View file

@ -11,8 +11,15 @@ export default async function migration() {
const db = new Database(location);
const resourceSiteMap = new Map<number, number>();
let firstSiteId: number = 1;
try {
// Get the first siteId to use as default
const firstSite = db.prepare("SELECT siteId FROM sites LIMIT 1").get() as { siteId: number } | undefined;
if (firstSite) {
firstSiteId = firstSite.siteId;
}
const resources = db
.prepare(
"SELECT resourceId, siteId FROM resources WHERE siteId IS NOT NULL"
@ -109,7 +116,7 @@ ALTER TABLE 'exitNodes' ADD 'lastPing' integer;--> statement-breakpoint
ALTER TABLE 'exitNodes' ADD 'type' text DEFAULT 'gerbil';--> statement-breakpoint
ALTER TABLE 'olms' ADD 'version' text;--> statement-breakpoint
ALTER TABLE 'orgs' ADD 'createdAt' text;--> statement-breakpoint
ALTER TABLE 'targets' ADD 'siteId' integer NOT NULL DEFAULT 1 REFERENCES sites(siteId);`);
ALTER TABLE 'targets' ADD 'siteId' integer NOT NULL DEFAULT ${firstSiteId || 1} REFERENCES sites(siteId);`);
// for each resource, get all of its targets, and update the siteId to be the previously stored siteId
for (const [resourceId, siteId] of resourceSiteMap) {