Repository navigation
Use shared-context helpers in migration specs - #5529
philippthun wants to merge 1 commit into
Conversation
d121ca4 to
6428955
Compare
c5ef235 to
5730218
Compare
Replace raw Sequel::Migrator.run calls in migration specs with the run_migration and revert_migration helpers, and use the test_up_migration_idempotency / test_down_migration_idempotency helpers for idempotency checks. The idempotency helpers delete or insert the schema_migrations row before re-running, so the migration body is genuinely re-executed. Re-running the migrator to the same target only is a no-op and does not test idempotency. Apply each helper only where the migration body supports it: options like add_column(if_not_exists:) are honoured on PostgreSQL but ignored on MySQL, so such migrations assert idempotency in a database-gated context. Single-database migrations are exercised only there, and migrations with unguarded DDL or no reversible down assert only the steps that apply. Each added step asserts state in the same style as the rest of its spec. Update spec/migrations/Readme.md to document the four helpers. Add two custom cops: UseMigrationHelpers flags raw Sequel::Migrator.run calls in specs that include the shared context, and RequireMigrationSharedContext flags per-migration specs that omit it. Legacy and bigint-step specs are excluded via .rubocop_cc.yml.
5730218 to
4df5418
Compare
| @@ -2,6 +2,8 @@ module RuboCop | |||
| module Cop | |||
| module Migration | |||
| class TooManyMigrationRuns < Base | |||
| MIGRATION_HELPERS = %i[run_migration revert_migration test_up_migration_idempotency test_down_migration_idempotency].freeze | |||
There was a problem hiding this comment.
This counts the four helper names as migration runs without checking the file includes the migration shared context first. An unrelated spec with a coincidental let(:run_migration) called 5× would be falsely flagged "Too many migration runs" and fail CI (verified). Gate on include_context 'migration' like the other two cops do.
| end | ||
|
|
||
| def test_down_migration_idempotency | ||
| db[:schema_migrations].insert(filename: migration_filename) |
There was a problem hiding this comment.
test_down_migration_idempotency does an unguarded schema_migrations.insert on the PK → UniqueConstraintViolation if called without a preceding revert_migration. Asymmetric with the .delete-based up helper.
|
|
||
| private | ||
|
|
||
| def includes_migration_context? |
There was a problem hiding this comment.
includes_migration_context?/migration_shared_context? are byte-identical across the two new cops, could be extracted to a shared mixin.
Replace raw
Sequel::Migrator.runcalls in migration specs with therun_migrationandrevert_migration helpers, and use thetest_up_migration_idempotency/test_down_migration_idempotencyhelpers for idempotency checks.The idempotency helpers delete or insert the
schema_migrationsrow before re-running, so the migration body is genuinely re-executed. Re-running the migrator to the same target only is a no-op and does not test idempotency.Apply each helper only where the migration body supports it: options like
add_column(if_not_exists:)are honoured on PostgreSQL but ignored on MySQL, so such migrations assert idempotency in a database-gated context. Single-database migrations are exercised only there, and migrations with unguarded DDL or no reversible down assert only the steps that apply. Each added step asserts state in the same style as the rest of its spec.Update
spec/migrations/Readme.mdto document the four helpers.Add two custom cops:
UseMigrationHelpersflags rawSequel::Migrator.runcalls in specs that include the shared context, andRequireMigrationSharedContextflags per-migration specs that omit it. Legacy and bigint-step specs are excluded via.rubocop_cc.yml.This change was developed with AI assistance; all code was reviewed and tested by me.
I have reviewed the contributing guide
I have viewed, signed, and submitted the Contributor License Agreement
I have made this pull request to the
mainbranchI have run all the unit tests using
bundle exec rakeI have run CF Acceptance Tests