mirror of
https://github.com/fosrl/pangolin.git
synced 2026-04-01 07:26:38 +00:00
Add userInviteRoles migration
This commit is contained in:
@@ -20,6 +20,19 @@ export default async function migration() {
|
|||||||
`Found ${existingUserOrgRoles.length} existing userOrgs role assignment(s) to migrate`
|
`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 {
|
try {
|
||||||
await db.execute(sql`BEGIN`);
|
await db.execute(sql`BEGIN`);
|
||||||
|
|
||||||
@@ -174,6 +187,29 @@ export default async function migration() {
|
|||||||
throw e;
|
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
|
// Re-insert the preserved role assignments into the new userOrgRoles table
|
||||||
if (existingUserOrgRoles.length > 0) {
|
if (existingUserOrgRoles.length > 0) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -24,6 +24,17 @@ export default async function migration() {
|
|||||||
`Found ${existingUserOrgRoles.length} existing userOrgs role assignment(s) to migrate`
|
`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.transaction(() => {
|
||||||
db.prepare(
|
db.prepare(
|
||||||
`
|
`
|
||||||
@@ -184,6 +195,25 @@ export default async function migration() {
|
|||||||
|
|
||||||
db.pragma("foreign_keys = ON");
|
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
|
// Re-insert the preserved role assignments into the new userOrgRoles table
|
||||||
if (existingUserOrgRoles.length > 0) {
|
if (existingUserOrgRoles.length > 0) {
|
||||||
const insertUserOrgRole = db.prepare(
|
const insertUserOrgRole = db.prepare(
|
||||||
|
|||||||
Reference in New Issue
Block a user