Skip to content

refactor(testhelper): switch to migrate.Up for schema setup - #1197

Merged
kalbasit merged 1 commit into
mainfrom
refactor-testhelper-use-migrate-up
May 22, 2026
Merged

kalbasit merged 1 commit into
mainfrom
refactor-testhelper-use-migrate-up

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

Replace the dbmate exec.Command invocations in
CreateMigrateDatabase / MigratePostgresDatabase /
MigrateMySQLDatabase with the same pkg/database/migrate.Up
flow ncps migrate up uses in production: empty DB →
ent.Schema.Create → final schema. Three motivations:

  1. The dbmate-only path produces a schema without the §10b
    surrogate id columns on weak entities
    (narinfo_references, narinfo_signatures,
    narinfo_nar_files, nar_file_chunks). Ent's edge traversals
    (WithReferences / WithSignatures /
    WithNarInfoNarFiles) generate SQL that selects those
    columns; tests against the dbmate schema fail with
    "no such column: narinfo_references.id". Blocked
    §11.2's cascading conversions in pkg/cache. Now unblocked.

  2. Eliminates the external dbmate binary dependency from
    the test path. Tests now exercise the production migrate
    code on every run, so schema-bug regressions surface in
    unit tests instead of integration.

  3. Drops the need for runtime.Caller-derived
    db/migrations/<dialect>/ path lookups. The migrations
    are loaded from migrations/ via the embedded
    migrations.FS, the same source of truth ncps ships in
    its binary.

Required prerequisite: the previous commit's Ent schema fix
for last_accessed_at DEFAULT CURRENT_TIMESTAMP. Without it,
contract tests that read LastAccessedAt.Time would see
NULL instead of the expected insert-time timestamp.

Full -race test suite passes against SQLite + Postgres +
MySQL with the deps stack running. Lint clean.

@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:L This PR changes 100-499 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 fix-ent-preserve-current-timestamp-default branch from 9d66305 to 394ceb3 Compare May 22, 2026 05:33
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from 7476f56 to d47e51b Compare May 22, 2026 05:33
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from 394ceb3 to c261c89 Compare May 22, 2026 05:46
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from d47e51b to 6caee71 Compare May 22, 2026 05:46
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from c261c89 to faa691a Compare May 22, 2026 05:59
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from 6caee71 to b63df34 Compare May 22, 2026 05:59
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from faa691a to 82320c8 Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from b63df34 to 7eb0409 Compare May 22, 2026 06:47
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from 82320c8 to ed01afa Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from 7eb0409 to 69fd9fc Compare May 22, 2026 07:06
@kalbasit
kalbasit force-pushed the fix-ent-preserve-current-timestamp-default branch from ed01afa to aa2db41 Compare May 22, 2026 18:07
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from 69fd9fc to 756cb1b Compare May 22, 2026 18:07
@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: d4c9b517-e169-4e4c-a158-f93801ce704d

📥 Commits

Reviewing files that changed from the base of the PR and between d963c81 and 196e69e.

📒 Files selected for processing (3)
  • testhelper/mysql.go
  • testhelper/postgres.go
  • testhelper/sqlite.go

📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Replaced external CLI-based test database migrations with an in-process migration runner using embedded migration files for MySQL, PostgreSQL, and SQLite, improving consistency, reliability, and test timing.
  • Chores
    • No public/exported function signatures were changed.

Walkthrough

Three test helper functions are refactored to run embedded migrations in-process via migrate.Up: MySQL, PostgreSQL, and SQLite helpers now open connections, select dialect-specific migrations from migrations.FS, and execute migrations without spawning external processes.

Changes

Database test helper migration refactoring

Layer / File(s) Summary
MySQL migration helper refactoring
testhelper/mysql.go
MigrateMySQLDatabase updates imports, opens the MySQL DB via database.Open, selects migrations.FS/mysql with fs.Sub, and runs migrate.Up with database.TypeMySQL instead of computing local paths and invoking dbmate.
PostgreSQL migration helper refactoring
testhelper/postgres.go
Imports updated to remove runtime/os/exec/filepath and add migrations, migrate, and io/fs. MigratePostgresDatabase opens the DB, selects migrations.FS/postgres, and calls migrate.Up with the PostgreSQL dialect.
SQLite migration helper refactoring
testhelper/sqlite.go
Imports updated for io/fs and migrate. CreateMigrateDatabase creates parent directory, opens SQLite with pragma options (_fk=1, WAL, busy timeout), selects migrations.FS/sqlite, runs migrate.Up, and performs wal_checkpoint(TRUNCATE) before returning.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

  • kalbasit/ncps#1188: Introduces the pkg/database/migrate API and migrations.FS embedded filesystem used by this refactor.

Poem

🐰 I hopped through code with whiskers bright,

swapped shell calls for in-process light.
MySQL, Postgres, SQLite all sing,
migrations snug as a springtime thing.
The rabbit cheers — tests take flight.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title directly reflects the main change: switching from dbmate CLI to migrate.Up for schema setup in testhelper functions across three database types.
Description check ✅ Passed The description provides comprehensive context explaining the refactoring motivation, technical benefits, and migration approach aligned with the code changes in mysql.go, postgres.go, and sqlite.go.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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 force-pushed the fix-ent-preserve-current-timestamp-default branch from aa2db41 to dcf22dc Compare May 22, 2026 19:02
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch 2 times, most recently from 7f0de59 to d963c81 Compare May 22, 2026 20:44
Base automatically changed from fix-ent-preserve-current-timestamp-default to main May 22, 2026 20:51
Replace the dbmate exec.Command invocations in
CreateMigrateDatabase / MigratePostgresDatabase /
MigrateMySQLDatabase with the same pkg/database/migrate.Up
flow `ncps migrate up` uses in production: empty DB →
ent.Schema.Create → final schema. Three motivations:

1. The dbmate-only path produces a schema without the §10b
   surrogate `id` columns on weak entities
   (narinfo_references, narinfo_signatures,
   narinfo_nar_files, nar_file_chunks). Ent's edge traversals
   (WithReferences / WithSignatures /
   WithNarInfoNarFiles) generate SQL that selects those
   columns; tests against the dbmate schema fail with
   "no such column: narinfo_references.id". Blocked
   §11.2's cascading conversions in pkg/cache. Now unblocked.

2. Eliminates the external dbmate binary dependency from
   the test path. Tests now exercise the production migrate
   code on every run, so schema-bug regressions surface in
   unit tests instead of integration.

3. Drops the need for `runtime.Caller`-derived
   `db/migrations/<dialect>/` path lookups. The migrations
   are loaded from `migrations/` via the embedded
   `migrations.FS`, the same source of truth ncps ships in
   its binary.

Required prerequisite: the previous commit's Ent schema fix
for `last_accessed_at DEFAULT CURRENT_TIMESTAMP`. Without it,
contract tests that read `LastAccessedAt.Time` would see
NULL instead of the expected insert-time timestamp.

Full -race test suite passes against SQLite + Postgres +
MySQL with the deps stack running. Lint clean.
@kalbasit
kalbasit force-pushed the refactor-testhelper-use-migrate-up branch from d963c81 to 196e69e Compare May 22, 2026 20:51
@kalbasit
kalbasit enabled auto-merge (squash) May 22, 2026 20:52
@codecov

codecov Bot commented May 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 42 lines in your changes missing coverage. Please review.
✅ Project coverage is 5.68%. Comparing base (57749da) to head (196e69e).

Files with missing lines Patch % Lines
testhelper/sqlite.go 0.00% 16 Missing ⚠️
testhelper/mysql.go 0.00% 13 Missing ⚠️
testhelper/postgres.go 0.00% 13 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##            main   #1197      +/-   ##
========================================
- Coverage   6.26%   5.68%   -0.59%     
========================================
  Files          6       6              
  Lines        335     387      +52     
========================================
+ Hits          21      22       +1     
- Misses       310     361      +51     
  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 5e92e11 into main May 22, 2026
14 of 16 checks passed
@kalbasit
kalbasit deleted the refactor-testhelper-use-migrate-up branch May 22, 2026 21:04
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:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant