mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-16 06:08:33 -05:00
[PR #3013] [CLOSED] Add unique index on roles (orgId, name) #33423
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/fosrl/pangolin/pull/3013
Author: @rinseaid
Created: 5/6/2026
Status: ❌ Closed
Base:
main← Head:roles-unique-orgid-name-index📝 Commits (1)
39f6973feat: add unique index on roles (orgId, name) with data migration📊 Changes
6 files changed (+369 additions, -5 deletions)
View changed files
📝
server/db/pg/schema/schema.ts(+4 -1)📝
server/db/sqlite/schema/schema.ts(+5 -2)📝
server/setup/migrationsPg.ts(+3 -1)📝
server/setup/migrationsSqlite.ts(+3 -1)➕
server/setup/scriptsPg/1.19.0.ts(+170 -0)➕
server/setup/scriptsSqlite/1.19.0.ts(+184 -0)📄 Description
By creating this pull request, I grant the project maintainers an unlimited,
perpetual license to use, modify, and redistribute these contributions under any terms they
choose, including both the AGPLv3 and the Fossorial Commercial license terms. I
represent that I have the right to grant this license for all contributed content.
Preface
This PR was developed with Claude Code (claude-opus-4-6). The migration logic, edge case handling, and scoped dedup approach went through multiple review rounds (11 agents across 2 passes) that caught several bugs that would have caused data loss in production, including a type coercion bug where postgres returns
MIN()as a string instead of an integer, and a connection pool race where rawBEGIN/COMMITdoesn't pin a connection. Both PostgreSQL and SQLite migrations were tested against real databases with intentionally messy data (triple duplicates, PK conflicts, legitimate duplicate rows in FK tables that need to survive). I reviewed, tested, and validated all changes.Description
The
rolestable has no unique constraint on(orgId, name). Multiple code paths use a select-then-insert pattern that can produce duplicate rows under concurrent requests: thecreateRoleAPI handler and the blueprint auto-create logic in #2980.This adds a versioned migration (1.19.0, both PG and SQLite) that cleans up any existing duplicates before creating the index. For each duplicate group (same orgId + name), the migration keeps the lowest roleId and repoints all foreign key references from the duplicates to the survivor across the 7 tables that reference
roles.roleId(userOrgRoles, roleSiteResources, roleActions, roleSites, roleResources, userInviteRoles, roleClients). Any rows that become identical after the repoint are deduplicated. The duplicate roles are then deleted and the unique index is created.Schema files are updated to declare the
uniqueIndexso Drizzle stays in sync with what the migration creates.How to test?
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.