Skip to content

refactor(database): share Schema.Create mutex across callers - #1195

Merged
kalbasit merged 1 commit into
mainfrom
refactor-database-share-schema-create-mutex
May 22, 2026
Merged

kalbasit merged 1 commit into
mainfrom
refactor-database-share-schema-create-mutex

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Ent's migrate.Tables is a package-level slice that Atlas's
setupTables mutates during Schema.Create. Anyone in the process
calling Schema.Create concurrently with a different mutex would
race on that slice, and the race detector would catch it the
moment a second caller is added.

Until now there was a single caller (pkg/database/migrate's
fresh-install path) plus an unrelated test helper in
pkg/database that used its own private sync.Mutex. They didn't
contend because the second never ran in the same test binary
as the first. The upcoming testhelper.CreateMigrateDatabase
rewrite (next commit) will add a third caller, at which point
the two mutexes would no longer be enough.

This commit consolidates them by lifting the mutex into
pkg/database as the exported SchemaCreateMu. Every code path
that calls Schema.Create acquires it. The lifted mutex lives
next to Client because both relate to Ent client lifecycle
and pkg/database is the natural home; pkg/database/migrate
already imports pkg/database so the dependency direction stays
clean.

No behaviour change beyond the test-time race elimination.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. go Pull requests that update go code labels May 21, 2026
@kalbasit

kalbasit commented May 21, 2026 •

Copy link
Copy Markdown
Owner Author

This change is part of the following stack:

Change managed by git-spice.

This was referenced May 21, 2026
@kalbasit
kalbasit force-pushed the feat-cache-with-ent-transaction branch from 654fe0b to 6ebe384 Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the refactor-database-share-schema-create-mutex branch from adeb330 to c45bf81 Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the feat-cache-with-ent-transaction branch from 6ebe384 to d51f567 Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the refactor-database-share-schema-create-mutex branch from c45bf81 to 2192f36 Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the feat-cache-with-ent-transaction branch 12 times, most recently from e2c4993 to f92d4a6 Compare May 22, 2026 17:53
Base automatically changed from feat-cache-with-ent-transaction to main May 22, 2026 18:06
@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels May 22, 2026
Ent's migrate.Tables is a package-level slice that Atlas's
setupTables mutates during Schema.Create. Anyone in the process
calling Schema.Create concurrently with a different mutex would
race on that slice, and the race detector would catch it the
moment a second caller is added.

Until now there was a single caller (pkg/database/migrate's
fresh-install path) plus an unrelated test helper in
pkg/database that used its own private sync.Mutex. They didn't
contend because the second never ran in the same test binary
as the first. The upcoming testhelper.CreateMigrateDatabase
rewrite (next commit) will add a third caller, at which point
the two mutexes would no longer be enough.

This commit consolidates them by lifting the mutex into
pkg/database as the exported `SchemaCreateMu`. Every code path
that calls Schema.Create acquires it. The lifted mutex lives
next to `Client` because both relate to Ent client lifecycle
and pkg/database is the natural home; pkg/database/migrate
already imports pkg/database so the dependency direction stays
clean.

No behaviour change beyond the test-time race elimination.
@kalbasit
kalbasit force-pushed the refactor-database-share-schema-create-mutex branch from 2192f36 to 6e3a419 Compare May 22, 2026 18:07
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels May 22, 2026
@coderabbitai

coderabbitai Bot commented May 22, 2026 •

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f39d17e7-fd78-4b6b-a5fe-d8285ebbba58

📥 Commits

Reviewing files that changed from the base of the PR and between cbd6db0 and 6e3a419.

📒 Files selected for processing (3)
  • pkg/database/client.go
  • pkg/database/client_test.go
  • pkg/database/migrate/fresh.go

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Improved database initialization stability by preventing concurrent access conflicts during schema creation.

Walkthrough

This PR consolidates Ent schema creation concurrency control by introducing an exported process-wide SchemaCreateMu mutex in the database package. It replaces separate file-local mutexes in the migration code and test helpers, ensuring all paths calling ent.Schema.Create are serialized through a single shared lock.

Changes

Ent schema creation concurrency consolidation

Layer / File(s) Summary
Define process-wide schema creation mutex
pkg/database/client.go
New exported SchemaCreateMu mutex with sync import and documentation indicating it must be held around all ent.Schema.Create calls to prevent concurrent mutation races.
Consolidate migration to shared mutex
pkg/database/migrate/fresh.go
Local entCreateMu and sync import are removed; freshInstall now locks/unlocks ncpsdb.SchemaCreateMu around Ent schema creation, including proper unlock on error paths.
Update test helper to use shared mutex
pkg/database/client_test.go
Test helper freshSchemaSQLite replaces its use of the removed entCreateMu with the shared database.SchemaCreateMu, ensuring unlock on both success and failure paths.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • kalbasit/ncps#1191: Introduced test-local entCreateMu that this PR consolidates into a shared exported mutex for broader use across migration and test code.

Suggested labels

size:XL, merge-stack

Poem

🐰 A mutex shared, no more scattered locks,
Schema creation marches in synchronized blocks.
One guard for all, from fresh install to test—
Concurrency races now put to rest! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: refactoring to share a Schema.Create mutex across multiple callers in the database package.
Description check ✅ Passed The description is directly related to the changeset, explaining the motivation for consolidating mutexes, the technical problem being solved, and implementation details.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

@kalbasit
kalbasit enabled auto-merge (squash) May 22, 2026 18:08
@codecov

codecov Bot commented May 22, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 4.94%. Comparing base (cbd6db0) to head (6e3a419).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #1195   +/-   ##
=====================================
  Coverage   4.94%   4.94%           
=====================================
  Files          6       6           
  Lines        445     445           
=====================================
  Hits          22      22           
  Misses       419     419           
  Partials       4       4           

☔ View full report in Codecov by Sentry.
📢 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.

@kalbasit
kalbasit merged commit d644bbc into main May 22, 2026
24 of 27 checks passed
@kalbasit
kalbasit deleted the refactor-database-share-schema-create-mutex branch May 22, 2026 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code merge-stack size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant