Repository navigation
refactor!: drop user.client_id, backfill canonical identifier columns - #3066
Conversation
Removes `client_id` / `client` from the User domain interface and
UserEntity. The column has been effectively unused since migration
1766830857009 deleted all rows where it was non-null ("user client_id
referenced wrong table"). The user provisioning synchronizer drops the
now-redundant `client_id` lookup key.
Adds paired postgres + mysql migration `1779267068441-Default`:
- canonical-name backfill: collision pre-check (aborts on duplicate
after `LOWER(TRIM(name))`) + `UPDATE` to canonical form across nine
name tables (clients, robots, users, realms, roles, scopes,
permissions, policies, identity-providers) and `auth_users.email`.
Pre-check group-by mirrors each entity's `@Unique([...])` declaration.
- DDL: drops `auth_users.client_id` FK, index, column.
`down()` restores the column / index / FK; the canonical UPDATEs are
not reversed (lossy).
BREAKING CHANGE: `User.client_id` and `User.client` are removed from
`@authup/core-kit`.
|
Warning Rate limit exceeded
You’ve run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThis PR removes the optional ChangesClient association removal from User
🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR removes the deprecated User.client_id/User.client fields and corresponding DB mapping, and introduces a data+schema migration that (1) canonicalizes name (and auth_users.email) values via LOWER(TRIM(...)) with a collision pre-check, then (2) drops the auth_users.client_id FK/index/column for both Postgres and MySQL.
Changes:
- Remove
client_id/clientfrom the@authup/core-kitUserinterface and the server-coreUserEntitymapping. - Update user provisioning synchronization lookup to stop using the removed
client_idkey. - Add Postgres + MySQL migrations to pre-check for canonical-name collisions, backfill canonical values, and drop the
auth_users.client_idcolumn (with a partial down migration restoring only the dropped column/index/FK).
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/core-kit/src/domains/user/entity.ts | Drops client_id/client from the shared User domain interface (breaking API). |
| apps/server-core/src/core/provisioning/synchronizer/user/module.ts | Removes client_id from the provisioning lookup key when resolving existing users. |
| apps/server-core/src/adapters/database/migrations/postgres/1779267068441-Default.ts | Adds Postgres migration to collision-check + canonicalize names/emails and drop auth_users.client_id. |
| apps/server-core/src/adapters/database/migrations/mysql/1779267068441-Default.ts | Adds MySQL migration to collision-check + canonicalize names/emails and drop auth_users.client_id. |
| apps/server-core/src/adapters/database/domains/user/entity.ts | Removes the client_id column and client relation from the TypeORM UserEntity. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| for (const table of NAME_TABLES) { | ||
| await queryRunner.query(` | ||
| UPDATE "${table}" | ||
| SET "name" = LOWER(TRIM("name")) | ||
| WHERE "name" <> LOWER(TRIM("name")) | ||
| `); |
| for (const table of NAME_TABLES) { | ||
| await queryRunner.query(` | ||
| UPDATE \`${table}\` | ||
| SET \`name\` = LOWER(TRIM(\`name\`)) | ||
| WHERE \`name\` <> LOWER(TRIM(\`name\`)) | ||
| `); |
…HERE MySQL's default `utf8mb4_0900_ai_ci` collation is case-insensitive, so `name <> LOWER(TRIM(name))` evaluated `'TestRealm' = 'testrealm'` and the UPDATE silently skipped every mixed-case row. The migration was a no-op on MySQL deployments. Forcing `BINARY` on the LHS of the comparison switches the predicate to byte-by-byte equality so the WHERE clause matches mixed-case storage correctly. The `LOWER(TRIM(...))` SET expression is unaffected (those functions return the canonical bytes regardless of collation). Caught by smoke-testing the migration end-to-end against a real MySQL container before merge.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In
`@apps/server-core/src/adapters/database/migrations/mysql/1779267068441-Default.ts`:
- Around line 8-18: The pre-check in the migration (driven by
NAME_COLLISION_CHECKS) is too strict because nullable client_id values get
grouped together; update the collision pre-check SQL for the auth_roles and
auth_permissions entries so they exclude NULL client_id values by adding a WHERE
client_id IS NOT NULL before the GROUP BY in the pre-check generation logic for
both MySQL and Postgres migrations; specifically modify the pre-check for the
entries referencing 'auth_roles' and 'auth_permissions' so the generated query
includes the client_id IS NOT NULL predicate to match the database UNIQUE
semantics.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6e44aeee-2edc-4ca1-9bb9-03cd00cecf1e
📒 Files selected for processing (5)
apps/server-core/src/adapters/database/domains/user/entity.tsapps/server-core/src/adapters/database/migrations/mysql/1779267068441-Default.tsapps/server-core/src/adapters/database/migrations/postgres/1779267068441-Default.tsapps/server-core/src/core/provisioning/synchronizer/user/module.tspackages/core-kit/src/domains/user/entity.ts
💤 Files with no reviewable changes (2)
- apps/server-core/src/core/provisioning/synchronizer/user/module.ts
- packages/core-kit/src/domains/user/entity.ts
…antics SQL GROUP BY collapses NULL values into a single group, but UNIQUE constraints treat NULL as distinct. The pre-check was therefore stricter than UNIQUE: two rows with the same canonical name + NULL in a keyed column (e.g. two global scopes with realm_id=NULL, or two global permissions with client_id=NULL and realm_id=NULL) would abort the migration even though they are valid under the actual unique constraint. For tables with nullable columns in the unique key, filter NULL rows out of the pre-check so the predicate matches UNIQUE semantics: - auth_roles, auth_permissions: WHERE client_id IS NOT NULL AND realm_id IS NOT NULL - auth_scopes, auth_policies: WHERE realm_id IS NOT NULL The canonical UPDATE itself is unchanged — it rewrites all rows regardless of NULLs, which is safe because NULL-distinct rows can never collide post-UPDATE. Caught by CodeRabbit review and confirmed by seeded smoke test: two global scopes (NULL realm_id) with the same canonical name now coexist through the migration (instead of aborting), while a realm-bound collision still triggers the abort correctly.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Summary
client_id/clientcolumns and relation fromUser/UserEntity. The column has been functionally dead since migration1766830857009deleted every row where it was non-null ("user client_id referenced wrong table"). The provisioning synchronizer drops the now-redundantclient_idlookup key.postgres+mysqlmigration1779267068441-Defaultthat bundles:LOWER(TRIM(name)), thenUPDATE … = LOWER(TRIM(…))across nine name tables (auth_clients,auth_robots,auth_users,auth_realms,auth_roles,auth_scopes,auth_permissions,auth_policies,auth_identity_providers) andauth_users.email. Pre-checkGROUP BYmirrors each entity's@Unique([...])declaration.auth_users.client_idFK, index, and column.down()restores the column / index / FK only; canonicalUPDATEs are not reversed (lossy by definition — original casing is not recoverable).Breaking change
User.client_idandUser.clientare removed from@authup/core-kit. Note the[key: string]: anyescape hatch on theUserinterface means any consumer reading these fields still typechecks but returnsundefinedat runtime.Test plan
npm run build --workspace=apps/server-core— cleannpm run test --workspace=apps/server-core— 799/799 passednpx eslint --fixon all changed files — cleanup/down/ re-upagainst fresh Postgres + MySQL containers — see belowSmoke-test details
auth_users.client_idconfirmed dropped → revert → column restored with correct FK target (auth_clients, not the originally-buggyauth_realms) → re-apply → column dropped again. ✅'TestRealm'+'[email protected]', ran migration → names and emails canonicalized to lowercase. ✅'CollideMe'+'collideme'(Postgres is case-sensitive, both rows persist) → migration aborted with actionable error:Canonical-name migration aborted: "auth_realms" has 1 collision group(s) on LOWER(TRIM(name)). ✅ec8972dfa): MySQL's defaultutf8mb4_0900_ai_cicollation treats'TestRealm' = 'testrealm', so the originalWHERE name <> LOWER(TRIM(name))predicate was always false on mixed-case rows and the UPDATE silently no-op'd. Switched the WHERE clause toWHERE BINARY name <> LOWER(TRIM(name))(and same foremail) to force byte-level comparison. Re-verified end-to-end. ✅Notes
down()for canonical backfill is intentionally a no-op (cannot recover original casing); subsequent writes continue to canonicalize, so the schema stays consistent.dataSource.synchronize()instead. The MySQLBINARYbug above was only caught by manual smoke testing. Opened CI: migrations are not exercised against MySQL or PostgreSQL #3067 to add a dedicated CI job that runs migrations end-to-end. Until that lands, every migration PR should be smoke-tested manually against both DBs.Summary by CodeRabbit
Bug Fixes
Refactor