Conversation
db/migration_test.go had three tests disabled with skips explaining
they needed a mockable migrate.Migrate or a real database:
- TestMigrationHandler_Step
- TestMigrationHandler_RunMigrations
- TestNewMigrationHandler (empty body, just a placeholder)
The blocker was that MigrationHandler embedded *migrate.Migrate
directly and MigrationStep / ForceVersion called globals.LogAndExit
on both success and failure — so callers could not observe outcomes
and tests could not run in-process.
Refactor:
- db/migration.go
- Introduce a Migrator interface (Steps, Version, Up, Force) that
captures the subset of *migrate.Migrate MigrationHandler uses.
- Replace the *migrate.Migrate embedding with a Migrator field.
- Add NewMigrationHandlerWith(m Migrator) for test injection.
- MigrationStep now returns (uint, error); ForceVersion returns
error. Neither calls LogAndExit — that decision belongs to the
CLI layer, not the library.
- RunMigrations still returns error (unchanged shape); implementation
routes through the new interface field.
- cmd/cservice-api/main.go
- runMigrations now handles the errors from MigrationStep and
ForceVersion, calling globals.LogAndExit with the same messages
that previously lived in the library.
- db/migration_test.go
- Add a hand-rolled mockMigrator (no mockery / no external mock
library) that records last-call args and returns configurable
errors.
- Un-skip TestMigrationHandler_Step with a table covering up-one,
down-one, Steps failure (asserts the wrapped error mentions the
direction), and Version failure.
- Un-skip TestMigrationHandler_RunMigrations with a table covering
clean apply, the "no change" success convention, and other-error
propagation.
- Add TestMigrationHandler_ForceVersion covering happy path and
error propagation.
- Delete the placeholder TestNewMigrationHandler (empty body, only a
t.Skip — was dead scaffolding, not a test).
Breaking API changes on the exported MigrationHandler:
- MigrationStep(int) → MigrationStep(int) (uint, error)
- ForceVersion(int) → ForceVersion(int) error
Only main.go consumed these; caller updated in the same commit. The
project is pre-1.0 (README: "WORK IN PROGRESS ... DO NOT USE IN
PRODUCTION") so this is an acceptable break.
Net effect: three previously-unreachable code paths in MigrationHandler
now have real unit test coverage without needing a live Postgres.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| Duplication | 0 |
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
db/migration_test.go had three tests disabled with skips explaining
they needed a mockable migrate.Migrate or a real database:
The blocker was that MigrationHandler embedded *migrate.Migrate
directly and MigrationStep / ForceVersion called globals.LogAndExit
on both success and failure — so callers could not observe outcomes
and tests could not run in-process.
Refactor:
db/migration.go
captures the subset of *migrate.Migrate MigrationHandler uses.
error. Neither calls LogAndExit — that decision belongs to the
CLI layer, not the library.
routes through the new interface field.
cmd/cservice-api/main.go
ForceVersion, calling globals.LogAndExit with the same messages
that previously lived in the library.
db/migration_test.go
library) that records last-call args and returns configurable
errors.
down-one, Steps failure (asserts the wrapped error mentions the
direction), and Version failure.
clean apply, the "no change" success convention, and other-error
propagation.
error propagation.
t.Skip — was dead scaffolding, not a test).
Breaking API changes on the exported MigrationHandler:
Only main.go consumed these; caller updated in the same commit. The
project is pre-1.0 (README: "WORK IN PROGRESS ... DO NOT USE IN
PRODUCTION") so this is an acceptable break.
Net effect: three previously-unreachable code paths in MigrationHandler
now have real unit test coverage without needing a live Postgres.