Skip to content

Fix user API key migration failing on populated databases - #90

Open
turegjorup wants to merge 2 commits into
developfrom
fix/user-api-key-migration-existing-rows
Open

Fix user API key migration failing on populated databases#90
turegjorup wants to merge 2 commits into
developfrom
fix/user-api-key-migration-existing-rows

Conversation

@turegjorup

@turegjorup turegjorup commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Link to ticket

https://leantime.itkdev.dk/_#/tickets/showTicket/7910

Description

Version20260702123347 from #88 adds user.api_key as NOT NULL and then indexes it uniquely, so every existing user gets the same empty string and the migration fails on the second row. Staging and production have not run it yet, but both would fail on the next release.

It now generates a key per existing user first, matching User::__construct, so existing users gain a dormant API key. Keys come from PHP because SQL's RAND() and UUID() are not cryptographically secure.

The new CI job migrates a populated database, unlike the existing jobs. It cannot go red on this bug, whose migration is already in this PR's base, so it is forward protection only.

Screenshot of the result

No user interface changes.

Checklist

  • My code is covered by test cases.
  • My code passes our test (all our tests).
  • My code passes our static analysis suite.
  • My code passes our continuous integration process.

Additional comments or questions

Existing users gain a working API key, which is the one decision here a follow-up migration cannot undo: afterwards the keys cannot be told apart from ones the application generated. The alternative was a nullable column, dropped because User::__construct already gives every new user a key.

The existing Doctrine jobs migrate an empty database, so a migration that
cannot cope with existing rows passes them unnoticed. Build the database as
it looks before the pull request, fixtures included, then apply the pull
request's migrations on top.

Fixtures created a single user, and one row cannot collide with itself, so
add a few more.
Version20260702123347 added user.api_key as NOT NULL and then put a unique
index on it, so every existing user got the same empty string and the index
collided on the second row. Any database holding more than one user failed
to migrate.

Generate a key for each existing user before creating the index, matching
App\Entity\User::__construct, which gives every user created since #88 a key
of its own. The keys come from PHP rather than SQL because they are
credentials and SQL's RAND() and UUID() are not cryptographically secure.
@turegjorup turegjorup self-assigned this Jul 30, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 37.63%. Comparing base (bae8493) to head (554ddd0).
⚠️ Report is 11 commits behind head on develop.

Additional details and impacted files
@@              Coverage Diff              @@
##             develop      #90      +/-   ##
=============================================
+ Coverage      37.14%   37.63%   +0.49%     
- Complexity       948      955       +7     
=============================================
  Files            133      135       +2     
  Lines           2972     2997      +25     
=============================================
+ Hits            1104     1128      +24     
- Misses          1868     1869       +1     
Flag Coverage Δ
unittests 37.63% <ø> (+0.49%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@turegjorup
turegjorup requested a review from rimi-itk July 30, 2026 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants