Refactor roles management to use role code as primary key

- Updated the roles table schema to use role code as the primary key, enhancing readability in junction tables.
- Modified related tables (user_roles, product_role_visibility) to reference role code instead of role ID.
- Adjusted seeding logic and utility functions to accommodate the new primary key structure, ensuring consistent role management across the application.
- Added a migration script to facilitate the database schema changes.
This commit is contained in:
Bastian Masanek
2025-11-03 10:45:11 +01:00
parent a6d09d76fd
commit 68ab42f2f4
5 changed files with 93 additions and 38 deletions

View File

@@ -173,7 +173,7 @@ async function seed() {
const roleCodes = productData.roles || []
for (const roleCode of roleCodes) {
// Find role by code
// Find role display name for logging
const role = insertedRoles.find((r) => r.code === roleCode)
if (!role) {
console.warn(` ⚠️ Role '${roleCode}' not found for product ${product.name}`)
@@ -184,14 +184,14 @@ async function seed() {
const existing = await db.query.productRoleVisibility.findFirst({
where: and(
eq(productRoleVisibility.productId, product.id),
eq(productRoleVisibility.roleId, role.id)
eq(productRoleVisibility.roleCode, roleCode)
),
})
if (!existing) {
await db.insert(productRoleVisibility).values({
productId: product.id,
roleId: role.id,
roleCode, // Direct reference to roles.code (PK)
})
assignmentCount++
console.log(` - ${product.name}${role.displayName}`)