From 673b8b7af5b03b51fbd0f90ff106eba88bd246c2 Mon Sep 17 00:00:00 2001 From: Owen Date: Mon, 30 Mar 2026 17:03:12 -0700 Subject: [PATCH] Add userInviteRoles migration --- server/setup/scriptsPg/1.17.0.ts | 36 ++++++++++++++++++++++++++++ server/setup/scriptsSqlite/1.17.0.ts | 30 +++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/server/setup/scriptsPg/1.17.0.ts b/server/setup/scriptsPg/1.17.0.ts index d953b24fb..c0489956f 100644 --- a/server/setup/scriptsPg/1.17.0.ts +++ b/server/setup/scriptsPg/1.17.0.ts @@ -20,6 +20,19 @@ export default async function migration() { `Found ${existingUserOrgRoles.length} existing userOrgs role assignment(s) to migrate` ); + // Query existing roleId data from userInvites before the transaction destroys it + const existingInviteRolesQuery = await db.execute( + sql`SELECT "inviteId", "roleId" FROM "userInvites" WHERE "roleId" IS NOT NULL` + ); + const existingUserInviteRoles = existingInviteRolesQuery.rows as { + inviteId: string; + roleId: number; + }[]; + + console.log( + `Found ${existingUserInviteRoles.length} existing userInvites role assignment(s) to migrate` + ); + try { await db.execute(sql`BEGIN`); @@ -174,6 +187,29 @@ export default async function migration() { throw e; } + // Re-insert the preserved invite role assignments into the new userInviteRoles table + if (existingUserInviteRoles.length > 0) { + try { + for (const row of existingUserInviteRoles) { + await db.execute(sql` + INSERT INTO "userInviteRoles" ("inviteId", "roleId") + VALUES (${row.inviteId}, ${row.roleId}) + ON CONFLICT DO NOTHING + `); + } + + console.log( + `Migrated ${existingUserInviteRoles.length} role assignment(s) into userInviteRoles` + ); + } catch (e) { + console.error( + "Error while migrating role assignments into userInviteRoles:", + e + ); + throw e; + } + } + // Re-insert the preserved role assignments into the new userOrgRoles table if (existingUserOrgRoles.length > 0) { try { diff --git a/server/setup/scriptsSqlite/1.17.0.ts b/server/setup/scriptsSqlite/1.17.0.ts index 866eb8fe5..10cc0973d 100644 --- a/server/setup/scriptsSqlite/1.17.0.ts +++ b/server/setup/scriptsSqlite/1.17.0.ts @@ -24,6 +24,17 @@ export default async function migration() { `Found ${existingUserOrgRoles.length} existing userOrgs role assignment(s) to migrate` ); + // Query existing roleId data from userInvites before the transaction destroys it + const existingUserInviteRoles = db + .prepare( + `SELECT "inviteId", "roleId" FROM 'userInvites' WHERE "roleId" IS NOT NULL` + ) + .all() as { inviteId: string; roleId: number }[]; + + console.log( + `Found ${existingUserInviteRoles.length} existing userInvites role assignment(s) to migrate` + ); + db.transaction(() => { db.prepare( ` @@ -184,6 +195,25 @@ export default async function migration() { db.pragma("foreign_keys = ON"); + // Re-insert the preserved invite role assignments into the new userInviteRoles table + if (existingUserInviteRoles.length > 0) { + const insertUserInviteRole = db.prepare( + `INSERT OR IGNORE INTO 'userInviteRoles' ("inviteId", "roleId") VALUES (?, ?)` + ); + + const insertAll = db.transaction(() => { + for (const row of existingUserInviteRoles) { + insertUserInviteRole.run(row.inviteId, row.roleId); + } + }); + + insertAll(); + + console.log( + `Migrated ${existingUserInviteRoles.length} role assignment(s) into userInviteRoles` + ); + } + // Re-insert the preserved role assignments into the new userOrgRoles table if (existingUserOrgRoles.length > 0) { const insertUserOrgRole = db.prepare(