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"; const version = "1.9.0";
await migration();
export default async function migration() { export default async function migration() {
console.log(`Running setup script ${version}...`); console.log(`Running setup script ${version}...`);

View file

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