Update migrations

This commit is contained in:
Owen
2025-09-14 17:35:21 -07:00
parent eea0b86d6d
commit 08d6183c9b
2 changed files with 59 additions and 2 deletions

View File

@@ -11,7 +11,11 @@ export default async function migration() {
try { try {
const resources = await db.execute(sql` const resources = await db.execute(sql`
SELECT "resourceId" FROM "resources" WHERE "siteId" IS NOT NULL SELECT "resourceId" FROM "resources"
`);
const siteResources = await db.execute(sql`
SELECT "siteResourceId" FROM "siteResources"
`); `);
await db.execute(sql`BEGIN`); await db.execute(sql`BEGIN`);
@@ -28,6 +32,10 @@ export default async function migration() {
sql`ALTER TABLE "resources" ADD COLUMN "niceId" text DEFAULT '' NOT NULL;` sql`ALTER TABLE "resources" ADD COLUMN "niceId" text DEFAULT '' NOT NULL;`
); );
await db.execute(
sql`ALTER TABLE "siteResources" ADD COLUMN "niceId" text DEFAULT '' NOT NULL;`
);
await db.execute( await db.execute(
sql`ALTER TABLE "userOrgs" ADD COLUMN "autoProvisioned" boolean DEFAULT false;` sql`ALTER TABLE "userOrgs" ADD COLUMN "autoProvisioned" boolean DEFAULT false;`
); );
@@ -65,6 +73,27 @@ export default async function migration() {
`); `);
} }
for (const resource of siteResources.rows) {
// Generate a unique name and ensure it's unique
let niceId = "";
let loops = 0;
while (true) {
if (loops > 100) {
throw new Error("Could not generate a unique name");
}
niceId = generateName();
if (!usedNiceIds.includes(niceId)) {
usedNiceIds.push(niceId);
break;
}
loops++;
}
await db.execute(sql`
UPDATE "siteResources" SET "niceId" = ${niceId} WHERE "siteResourceId" = ${resource.siteResourceId}
`);
}
await db.execute(sql`COMMIT`); await db.execute(sql`COMMIT`);
console.log(`Migrated database`); console.log(`Migrated database`);
} catch (e) { } catch (e) {

View File

@@ -17,10 +17,16 @@ export default async function migration() {
try { try {
const resources = db const resources = db
.prepare( .prepare(
"SELECT resourceId FROM resources WHERE siteId IS NOT NULL" "SELECT resourceId FROM resources"
) )
.all() as Array<{ resourceId: number }>; .all() as Array<{ resourceId: number }>;
const siteResources = db
.prepare(
"SELECT siteResourceId FROM siteResources"
)
.all() as Array<{ siteResourceId: number }>;
db.transaction(() => { db.transaction(() => {
db.exec(` db.exec(`
ALTER TABLE 'exitNodes' ADD 'region' text; ALTER TABLE 'exitNodes' ADD 'region' text;
@@ -30,6 +36,7 @@ export default async function migration() {
ALTER TABLE 'targets' ADD 'pathMatchType' text; ALTER TABLE 'targets' ADD 'pathMatchType' text;
ALTER TABLE 'targets' ADD 'path' text; ALTER TABLE 'targets' ADD 'path' text;
ALTER TABLE 'resources' ADD 'headers' text; ALTER TABLE 'resources' ADD 'headers' text;
ALTER TABLE 'siteResources' ADD 'niceId' text NOT NULL;
`); // this diverges from the schema a bit because the schema does not have a default on niceId but was required for the migration and I dont think it will effect much down the line... `); // this diverges from the schema a bit because the schema does not have a default on niceId but was required for the migration and I dont think it will effect much down the line...
const usedNiceIds: string[] = []; const usedNiceIds: string[] = [];
@@ -54,6 +61,27 @@ export default async function migration() {
`UPDATE resources SET niceId = ? WHERE resourceId = ?` `UPDATE resources SET niceId = ? WHERE resourceId = ?`
).run(niceId, resourceId.resourceId); ).run(niceId, resourceId.resourceId);
} }
for (const resourceId of siteResources) {
// Generate a unique name and ensure it's unique
let niceId = "";
let loops = 0;
while (true) {
if (loops > 100) {
throw new Error("Could not generate a unique name");
}
niceId = generateName();
if (!usedNiceIds.includes(niceId)) {
usedNiceIds.push(niceId);
break;
}
loops++;
}
db.prepare(
`UPDATE siteResources SET niceId = ? WHERE siteResourceId = ?`
).run(niceId, resourceId.siteResourceId);
}
})(); })();
console.log(`Migrated database`); console.log(`Migrated database`);