mirror of
https://github.com/fosrl/pangolin.git
synced 2025-12-14 12:08:11 +00:00
Migrations working
This commit is contained in:
@@ -80,33 +80,59 @@ export default async function migration() {
|
|||||||
);
|
);
|
||||||
`);
|
`);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSites" RENAME TO "clientSitesAssociationsCache";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSites" RENAME TO "clientSitesAssociationsCache";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clients" RENAME COLUMN "id" TO "clientId";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clients" RENAME COLUMN "id" TO "clientId";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" RENAME COLUMN "destinationIp" TO "destination";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" RENAME COLUMN "destinationIp" TO "destination";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSitesAssociationsCache" DROP CONSTRAINT "clientSites_clientId_clients_id_fk";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSitesAssociationsCache" DROP CONSTRAINT "clientSites_clientId_clients_id_fk";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSitesAssociationsCache" DROP CONSTRAINT "clientSites_siteId_sites_siteId_fk";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSitesAssociationsCache" DROP CONSTRAINT "clientSites_siteId_sites_siteId_fk";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "olms" DROP CONSTRAINT "olms_clientId_clients_id_fk";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "olms" DROP CONSTRAINT "olms_clientId_clients_id_fk";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "roleClients" DROP CONSTRAINT "roleClients_clientId_clients_id_fk";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "roleClients" DROP CONSTRAINT "roleClients_clientId_clients_id_fk";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "userClients" DROP CONSTRAINT "userClients_clientId_clients_id_fk";`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "userClients" DROP CONSTRAINT "userClients_clientId_clients_id_fk";`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ALTER COLUMN "protocol" DROP NOT NULL;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ALTER COLUMN "protocol" DROP NOT NULL;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ALTER COLUMN "proxyPort" DROP NOT NULL;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ALTER COLUMN "proxyPort" DROP NOT NULL;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ALTER COLUMN "destinationPort" DROP NOT NULL;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ALTER COLUMN "destinationPort" DROP NOT NULL;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSitesAssociationsCache" ADD COLUMN "publicKey" varchar;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSitesAssociationsCache" ADD COLUMN "publicKey" varchar;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clients" ADD COLUMN "userId" text;`);
|
await db.execute(sql`ALTER TABLE "clients" ADD COLUMN "userId" text;`);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clients" ADD COLUMN "niceId" varchar NOT NULL DEFAULT 'PLACEHOLDER';`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clients" ADD COLUMN "niceId" varchar NOT NULL DEFAULT 'PLACEHOLDER';`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clients" ADD COLUMN "olmId" text;`);
|
await db.execute(sql`ALTER TABLE "clients" ADD COLUMN "olmId" text;`);
|
||||||
|
|
||||||
@@ -116,59 +142,99 @@ export default async function migration() {
|
|||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "olms" ADD COLUMN "userId" text;`);
|
await db.execute(sql`ALTER TABLE "olms" ADD COLUMN "userId" text;`);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "orgs" ADD COLUMN "utilitySubnet" varchar;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "orgs" ADD COLUMN "utilitySubnet" varchar;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "session" ADD COLUMN "deviceAuthUsed" boolean DEFAULT false NOT NULL;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "session" ADD COLUMN "deviceAuthUsed" boolean DEFAULT false NOT NULL;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ADD COLUMN "mode" varchar NOT NULL DEFAULT 'host';`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ADD COLUMN "mode" varchar NOT NULL DEFAULT 'host';`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ADD COLUMN "alias" varchar;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ADD COLUMN "alias" varchar;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "siteResources" ADD COLUMN "aliasAddress" varchar;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "siteResources" ADD COLUMN "aliasAddress" varchar;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "targetHealthCheck" ADD COLUMN "hcTlsServerName" text;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "targetHealthCheck" ADD COLUMN "hcTlsServerName" text;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSiteResources" ADD CONSTRAINT "clientSiteResources_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSiteResources" ADD CONSTRAINT "clientSiteResources_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clientSiteResources" ADD CONSTRAINT "clientSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clientSiteResources" ADD CONSTRAINT "clientSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "deviceWebAuthCodes" ADD CONSTRAINT "deviceWebAuthCodes_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "deviceWebAuthCodes" ADD CONSTRAINT "deviceWebAuthCodes_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "roleSiteResources" ADD CONSTRAINT "roleSiteResources_roleId_roles_roleId_fk" FOREIGN KEY ("roleId") REFERENCES "public"."roles"("roleId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "roleSiteResources" ADD CONSTRAINT "roleSiteResources_roleId_roles_roleId_fk" FOREIGN KEY ("roleId") REFERENCES "public"."roles"("roleId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "roleSiteResources" ADD CONSTRAINT "roleSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "roleSiteResources" ADD CONSTRAINT "roleSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "userSiteResources" ADD CONSTRAINT "userSiteResources_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "userSiteResources" ADD CONSTRAINT "userSiteResources_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "userSiteResources" ADD CONSTRAINT "userSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "userSiteResources" ADD CONSTRAINT "userSiteResources_siteResourceId_siteResources_siteResourceId_fk" FOREIGN KEY ("siteResourceId") REFERENCES "public"."siteResources"("siteResourceId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "clients" ADD CONSTRAINT "clients_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "clients" ADD CONSTRAINT "clients_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "olms" ADD CONSTRAINT "olms_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE set null ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "olms" ADD CONSTRAINT "olms_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE set null ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "olms" ADD CONSTRAINT "olms_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "olms" ADD CONSTRAINT "olms_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "roleClients" ADD CONSTRAINT "roleClients_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "roleClients" ADD CONSTRAINT "roleClients_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "userClients" ADD CONSTRAINT "userClients_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`);
|
await db.execute(
|
||||||
|
sql`ALTER TABLE "userClients" ADD CONSTRAINT "userClients_clientId_clients_clientId_fk" FOREIGN KEY ("clientId") REFERENCES "public"."clients"("clientId") ON DELETE cascade ON UPDATE no action;`
|
||||||
|
);
|
||||||
|
|
||||||
// set 100.96.128.0/24 as the utility subnet on all of the orgs
|
// set 100.96.128.0/24 as the utility subnet on all of the orgs
|
||||||
await db.execute(sql`UPDATE "orgs" SET "utilitySubnet" = '100.96.128.0/24'`);
|
await db.execute(
|
||||||
|
sql`UPDATE "orgs" SET "utilitySubnet" = '100.96.128.0/24'`
|
||||||
|
);
|
||||||
|
|
||||||
// Query all of the sites to get their remoteSubnets
|
// Query all of the sites to get their remoteSubnets
|
||||||
|
|
||||||
const sitesRemoteSubnetsData = await db.execute(sql`SELECT "siteId", "remoteSubnets" FROM "sites" WHERE "remoteSubnets" IS NOT NULL
|
const sitesRemoteSubnetsData =
|
||||||
|
await db.execute(sql`SELECT "siteId", "remoteSubnets" FROM "sites" WHERE "remoteSubnets" IS NOT NULL
|
||||||
`);
|
`);
|
||||||
const sitesRemoteSubnets = sitesRemoteSubnetsData.rows as {
|
const sitesRemoteSubnets = sitesRemoteSubnetsData.rows as {
|
||||||
siteId: number;
|
siteId: number;
|
||||||
remoteSubnets: string | null;
|
remoteSubnets: string | null;
|
||||||
}[];
|
}[];
|
||||||
|
|
||||||
await db.execute(sql`ALTER TABLE "sites" DROP COLUMN "remoteSubnets";`);
|
await db.execute(sql`ALTER TABLE "sites" DROP COLUMN "remoteSubnets";`);
|
||||||
|
|
||||||
|
|
||||||
// get all of the siteResources and set the the aliasAddress to 100.96.128.x starting at .8
|
// get all of the siteResources and set the the aliasAddress to 100.96.128.x starting at .8
|
||||||
const siteResourcesData = await db.execute(sql`SELECT "siteResourceId" FROM "siteResources" ORDER BY "siteResourceId" ASC`);
|
const siteResourcesData = await db.execute(
|
||||||
|
sql`SELECT "siteResourceId" FROM "siteResources" ORDER BY "siteResourceId" ASC`
|
||||||
|
);
|
||||||
const siteResources = siteResourcesData.rows as {
|
const siteResources = siteResourcesData.rows as {
|
||||||
siteResourceId: number;
|
siteResourceId: number;
|
||||||
}[];
|
}[];
|
||||||
@@ -185,11 +251,19 @@ export default async function migration() {
|
|||||||
// For each site with remote subnets we need to create a site resource of type cidr for each remote subnet
|
// For each site with remote subnets we need to create a site resource of type cidr for each remote subnet
|
||||||
for (const site of sitesRemoteSubnets) {
|
for (const site of sitesRemoteSubnets) {
|
||||||
if (site.remoteSubnets) {
|
if (site.remoteSubnets) {
|
||||||
|
// Get the orgId for this site
|
||||||
|
const siteDataQuery = await db.execute(sql`
|
||||||
|
SELECT "orgId" FROM "sites" WHERE "siteId" = ${site.siteId}
|
||||||
|
`);
|
||||||
|
const siteData = siteDataQuery.rows[0] as { orgId: string } | undefined;
|
||||||
|
if (!siteData) continue;
|
||||||
|
|
||||||
const subnets = site.remoteSubnets.split(",");
|
const subnets = site.remoteSubnets.split(",");
|
||||||
for (const subnet of subnets) {
|
for (const subnet of subnets) {
|
||||||
|
const niceId = generateName();
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
INSERT INTO "siteResources" ("siteId", "destination", "mode", "name")
|
INSERT INTO "siteResources" ("siteId", "orgId", "niceId", "destination", "mode", "name")
|
||||||
VALUES (${site.siteId}, ${subnet.trim()}, 'cidr', 'Remote Subnet');
|
VALUES (${site.siteId}, ${siteData.orgId}, ${niceId}, ${subnet.trim()}, 'cidr', 'Remote Subnet');
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -221,6 +295,11 @@ export default async function migration() {
|
|||||||
INSERT INTO "clientSiteResources" ("clientId", "siteResourceId")
|
INSERT INTO "clientSiteResources" ("clientId", "siteResourceId")
|
||||||
VALUES (${association.clientId}, ${siteResource.siteResourceId})
|
VALUES (${association.clientId}, ${siteResource.siteResourceId})
|
||||||
`);
|
`);
|
||||||
|
// also associate in the clientSiteResourcesAssociationsCache table
|
||||||
|
await db.execute(sql`
|
||||||
|
INSERT INTO "clientSiteResourcesAssociationsCache" ("clientId", "siteResourceId")
|
||||||
|
VALUES (${association.clientId}, ${siteResource.siteResourceId})
|
||||||
|
`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +337,9 @@ export default async function migration() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Populate niceId for clients
|
// Populate niceId for clients
|
||||||
const clientsQuery = await db.execute(sql`SELECT "clientId" FROM "clients"`);
|
const clientsQuery = await db.execute(
|
||||||
|
sql`SELECT "clientId" FROM "clients"`
|
||||||
|
);
|
||||||
const clients = clientsQuery.rows as {
|
const clients = clientsQuery.rows as {
|
||||||
clientId: number;
|
clientId: number;
|
||||||
}[];
|
}[];
|
||||||
|
|||||||
@@ -327,6 +327,11 @@ export default async function migration() {
|
|||||||
`INSERT INTO 'clientSiteResources' ('clientId', 'siteResourceId') VALUES (?, ?)`
|
`INSERT INTO 'clientSiteResources' ('clientId', 'siteResourceId') VALUES (?, ?)`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// create a clientSiteResourcesAssociationsCache entry for each existing association as well
|
||||||
|
const insertClientSiteResourceCache = db.prepare(
|
||||||
|
`INSERT INTO 'clientSiteResourcesAssociationsCache' ('clientId', 'siteResourceId') VALUES (?, ?)`
|
||||||
|
);
|
||||||
|
|
||||||
// For each client-site association, find all site resources for that site
|
// For each client-site association, find all site resources for that site
|
||||||
for (const association of clientSiteAssociations) {
|
for (const association of clientSiteAssociations) {
|
||||||
const siteResources = getSiteResources.all(
|
const siteResources = getSiteResources.all(
|
||||||
@@ -341,6 +346,10 @@ export default async function migration() {
|
|||||||
association.clientId,
|
association.clientId,
|
||||||
siteResource.siteResourceId
|
siteResource.siteResourceId
|
||||||
);
|
);
|
||||||
|
insertClientSiteResourceCache.run(
|
||||||
|
association.clientId,
|
||||||
|
siteResource.siteResourceId
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user