Generate the initial subnets for sites and orgs

This commit is contained in:
Owen 2025-07-16 11:56:16 -07:00
parent 300175ac67
commit 779532b1c9
No known key found for this signature in database
GPG key ID: 8271FDFFD9E0CCBD

View file

@ -155,5 +155,32 @@ export default async function migration() {
throw e;
}
db.transaction(() => {
// Update all existing orgs to have the default subnet
db.exec(`UPDATE 'orgs' SET 'subnet' = '100.90.128.0/24'`);
// Get all orgs and their sites to assign sequential IP addresses
const orgs = db.prepare(`SELECT orgId FROM 'orgs'`).all() as {
orgId: string;
}[];
for (const org of orgs) {
const sites = db
.prepare(
`SELECT siteId FROM 'sites' WHERE orgId = ? ORDER BY siteId`
)
.all(org.orgId) as { siteId: number }[];
let ipIndex = 1;
for (const site of sites) {
const address = `100.90.128.${ipIndex}/24`;
db.prepare(
`UPDATE 'sites' SET 'address' = ? WHERE siteId = ?`
).run(address, site.siteId);
ipIndex++;
}
}
})();
console.log(`${version} migration complete`);
}