Add userInviteRoles migration

This commit is contained in:
Owen
2026-03-30 17:03:12 -07:00
parent a651e50759
commit 673b8b7af5
2 changed files with 66 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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(