|
| 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) { |
| 33 | + // Check if target ID is already taken by another role |
| 34 | + const conflictingRole = await knex('RolePortal') |
| 35 | + .where('id', role.targetId) |
| 36 | + .andWhere('name', '!=', role.name) |
| 37 | + .first() |
| 38 | + .transacting(trx); |
| 39 | + |
| 40 | + if (conflictingRole) { |
| 41 | + // Generate a temporary ID for the conflicting role |
| 42 | + const tempId = `temp-${Date.now()}-${Math.random().toString(36).substring(7)}`; |
| 43 | + await knex('RolePortal') |
| 44 | + .where('id', role.targetId) |
| 45 | + .update('id', tempId) |
| 46 | + .transacting(trx); |
| 47 | + } |
| 48 | + |
| 49 | + // Update foreign key references before changing the main ID |
| 50 | + await knex('User_RolePortal') |
| 51 | + .where('role_portal_id', existingRole.id) |
| 52 | + .update('role_portal_id', role.targetId) |
| 53 | + .transacting(trx); |
| 54 | + |
| 55 | + await knex('RolePortal_CapabilityPortal') |
| 56 | + .where('role_portal_id', existingRole.id) |
| 57 | + .update('role_portal_id', role.targetId) |
| 58 | + .transacting(trx); |
| 59 | + |
| 60 | + // Update the role ID |
| 61 | + await knex('RolePortal') |
| 62 | + .where('id', existingRole.id) |
| 63 | + .update('id', role.targetId) |
| 64 | + .transacting(trx); |
| 65 | + } else { |
| 66 | + // Create the role if it doesn't exist |
| 67 | + await knex('RolePortal') |
| 68 | + .insert({ |
| 69 | + id: role.targetId, |
| 70 | + name: role.name, |
| 71 | + }) |
| 72 | + .transacting(trx); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + await trx.commit(); |
| 77 | + } catch (err) { |
| 78 | + await trx.rollback(); |
| 79 | + throw err; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * @param { import("knex").Knex } knex |
| 85 | + * @returns { Promise<void> } |
| 86 | + */ |
| 87 | +export async function down() { |
| 88 | + // This migration standardizes role IDs, rollback is complex and potentially destructive |
| 89 | + // We'll leave the roles as-is since rolling back ID changes could break references |
| 90 | + console.warn( |
| 91 | + 'Rollback not implemented for role ID standardization - roles kept as-is' |
| 92 | + ); |
| 93 | +} |
0 commit comments