|
| 1 | +/** |
| 2 | + * @param { import("knex").Knex } knex |
| 3 | + * @returns { Promise<void> } |
| 4 | + */ |
| 5 | +export async function up(knex) { |
| 6 | + const trx = await knex.transaction(); |
| 7 | + |
| 8 | + try { |
| 9 | + // Define the target role IDs that match our portal constants |
| 10 | + const targetRoles = [ |
| 11 | + { |
| 12 | + name: 'ADMIN', |
| 13 | + targetId: '6b632cf2-9105-46ec-a463-ad59ab58c770', |
| 14 | + }, |
| 15 | + { |
| 16 | + name: 'USER', |
| 17 | + targetId: '7a234567-8901-4def-9012-3456789abcde', |
| 18 | + }, |
| 19 | + { |
| 20 | + name: 'ADMIN_ORGA', |
| 21 | + targetId: '40cfe630-c272-42f9-8fcf-f219e2f4278c', |
| 22 | + }, |
| 23 | + ]; |
| 24 | + |
| 25 | + for (const role of targetRoles) { |
| 26 | + // Check if role exists |
| 27 | + const existingRole = await knex('RolePortal') |
| 28 | + .where('name', role.name) |
| 29 | + .first() |
| 30 | + .transacting(trx); |
| 31 | + |
| 32 | + if (existingRole && existingRole.id === role.targetId) { |
| 33 | + // Role already has correct ID, skip |
| 34 | + continue; |
| 35 | + } |
| 36 | + |
| 37 | + if (existingRole) { |
| 38 | + // Check if target ID is already taken by another role |
| 39 | + const conflictingRole = await knex('RolePortal') |
| 40 | + .where('id', role.targetId) |
| 41 | + .andWhere('name', '!=', role.name) |
| 42 | + .first() |
| 43 | + .transacting(trx); |
| 44 | + |
| 45 | + if (conflictingRole) { |
| 46 | + // Generate a temporary ID for the conflicting role |
| 47 | + const tempId = `temp-${Date.now()}-${Math.random().toString(36).substring(7)}`; |
| 48 | + await knex('RolePortal') |
| 49 | + .where('id', role.targetId) |
| 50 | + .update('id', tempId) |
| 51 | + .transacting(trx); |
| 52 | + } |
| 53 | + |
| 54 | + // Create the role with target ID first |
| 55 | + await knex('RolePortal') |
| 56 | + .insert({ |
| 57 | + id: role.targetId, |
| 58 | + name: `${role.name}_temp`, |
| 59 | + }) |
| 60 | + .transacting(trx); |
| 61 | + |
| 62 | + // Update foreign key references to point to new role |
| 63 | + await knex('User_RolePortal') |
| 64 | + .where('role_portal_id', existingRole.id) |
| 65 | + .update('role_portal_id', role.targetId) |
| 66 | + .transacting(trx); |
| 67 | + |
| 68 | + await knex('RolePortal_CapabilityPortal') |
| 69 | + .where('role_portal_id', existingRole.id) |
| 70 | + .update('role_portal_id', role.targetId) |
| 71 | + .transacting(trx); |
| 72 | + |
| 73 | + // Delete the old role and rename the temp one |
| 74 | + await knex('RolePortal') |
| 75 | + .where('id', existingRole.id) |
| 76 | + .del() |
| 77 | + .transacting(trx); |
| 78 | + |
| 79 | + await knex('RolePortal') |
| 80 | + .where('id', role.targetId) |
| 81 | + .update('name', role.name) |
| 82 | + .transacting(trx); |
| 83 | + } else { |
| 84 | + // Create the role if it doesn't exist |
| 85 | + await knex('RolePortal') |
| 86 | + .insert({ |
| 87 | + id: role.targetId, |
| 88 | + name: role.name, |
| 89 | + }) |
| 90 | + .transacting(trx); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + await trx.commit(); |
| 95 | + } catch (err) { |
| 96 | + await trx.rollback(); |
| 97 | + throw err; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * @param { import("knex").Knex } knex |
| 103 | + * @returns { Promise<void> } |
| 104 | + */ |
| 105 | +export async function down() { |
| 106 | + // This migration standardizes role IDs, rollback is complex and potentially destructive |
| 107 | + // We'll leave the roles as-is since rolling back ID changes could break references |
| 108 | + console.warn( |
| 109 | + 'Rollback not implemented for role ID standardization - roles kept as-is' |
| 110 | + ); |
| 111 | +} |
0 commit comments