Skip to content

Commit 5730218

Browse files
committed
Use shared-context helpers in migration specs
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.
1 parent 9e087b0 commit 5730218

48 files changed

Lines changed: 687 additions & 151 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.rubocop_cc.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ require:
1212
- ./spec/linters/migration/add_constraint_name.rb
1313
- ./spec/linters/migration/include_string_size.rb
1414
- ./spec/linters/migration/no_model_in_specs.rb
15+
- ./spec/linters/migration/require_migration_shared_context.rb
1516
- ./spec/linters/migration/require_primary_key.rb
1617
- ./spec/linters/migration/too_many_migration_runs.rb
18+
- ./spec/linters/migration/use_migration_helpers.rb
1719
- ./spec/linters/match_requires_with_includes.rb
1820
- ./spec/linters/prefer_oj_over_other_json_libraries.rb
1921

@@ -126,6 +128,21 @@ Style/RaiseArgs:
126128
Migration/TooManyMigrationRuns:
127129
Exclude:
128130
- spec/linters/migration/too_many_migration_runs.rb
131+
Migration/UseMigrationHelpers:
132+
Exclude:
133+
- spec/migrations/helpers/**/*
134+
Migration/RequireMigrationSharedContext:
135+
Include:
136+
- spec/migrations/**/*
137+
Exclude:
138+
- spec/migrations/helpers/**/*
139+
# old migration specs
140+
- !ruby/regexp /spec/migrations/201([0-9]).+_spec\.rb$/
141+
- !ruby/regexp /spec/migrations/202([0-2]).+_spec\.rb$/
142+
# specs not testing a specific migration (no timestamp prefix)
143+
- !ruby/regexp /spec/migrations/[a-z]+.+_spec\.rb$/
144+
# bigint migration step specs use their own 'bigint migration *' contexts by design
145+
- !ruby/regexp /spec/migrations/.+_bigint_migration_.+_spec\.rb$/
129146

130147
#### ENABLED SECTION
131148
Capybara/FindAllFirst:
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module RuboCop
2+
module Cop
3+
module Migration
4+
class RequireMigrationSharedContext < Base
5+
MSG = "Per-migration specs must include the 'migration' shared context " \
6+
"(include_context 'migration') so they use the shared setup and helpers " \
7+
'(run_migration, revert_migration, test_up_migration_idempotency, ' \
8+
'test_down_migration_idempotency). See spec/migrations/Readme.md for details.'.freeze
9+
10+
def on_new_investigation
11+
return unless processed_source.ast
12+
return if includes_migration_context?
13+
14+
add_offense(processed_source.ast)
15+
end
16+
17+
private
18+
19+
def includes_migration_context?
20+
processed_source.ast.each_descendant(:send).any? { |node| migration_shared_context?(node) }
21+
end
22+
23+
def migration_shared_context?(node)
24+
node.method_name == :include_context &&
25+
node.receiver.nil? &&
26+
node.first_argument&.str_type? &&
27+
node.first_argument.value == 'migration'
28+
end
29+
end
30+
end
31+
end
32+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'spec_helper'
2+
require 'rubocop'
3+
require 'rubocop/rspec/cop_helper'
4+
require 'rubocop/config'
5+
require 'linters/migration/require_migration_shared_context'
6+
7+
RSpec.describe RuboCop::Cop::Migration::RequireMigrationSharedContext do
8+
include CopHelper
9+
10+
subject(:cop) { described_class.new(RuboCop::Config.new({})) }
11+
12+
let(:message) { described_class::MSG }
13+
14+
it 'registers an offense when the migration shared context is missing' do
15+
result = inspect_source(<<~RUBY)
16+
RSpec.describe 'adds a column' do
17+
it 'works' do
18+
expect(true).to be(true)
19+
end
20+
end
21+
RUBY
22+
23+
expect(result.size).to eq(1)
24+
expect(result.map(&:message)).to eq([message])
25+
end
26+
27+
it 'does not register an offense when the context is included via a block' do
28+
result = inspect_source(<<~RUBY)
29+
RSpec.describe 'adds a column' do
30+
include_context 'migration' do
31+
let(:migration_filename) { 'x.rb' }
32+
end
33+
end
34+
RUBY
35+
36+
expect(result.size).to eq(0)
37+
end
38+
39+
it 'does not register an offense for the bare include_context form' do
40+
result = inspect_source(<<~RUBY)
41+
RSpec.describe 'adds a column' do
42+
include_context 'migration'
43+
end
44+
RUBY
45+
46+
expect(result.size).to eq(0)
47+
end
48+
49+
it 'registers an offense when only a different shared context is included' do
50+
result = inspect_source(<<~RUBY)
51+
RSpec.describe 'adds a column' do
52+
include_context 'bigint migration step1' do
53+
let(:migration_filename) { 'x.rb' }
54+
end
55+
end
56+
RUBY
57+
58+
expect(result.size).to eq(1)
59+
end
60+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module RuboCop
2+
module Cop
3+
module Migration
4+
class UseMigrationHelpers < Base
5+
MSG = 'Do not call Sequel::Migrator.run directly in a migration spec that includes ' \
6+
"the 'migration' shared context. " \
7+
'Use the provided helpers instead: run_migration, revert_migration, ' \
8+
'test_up_migration_idempotency, test_down_migration_idempotency. ' \
9+
'See spec/migrations/Readme.md for details.'.freeze
10+
11+
HOOK_METHODS = %i[before after around].freeze
12+
13+
def on_new_investigation
14+
return unless processed_source.ast
15+
return unless includes_migration_context?
16+
17+
processed_source.ast.each_descendant(:send) do |node|
18+
next unless migrator_run_call?(node)
19+
next if inside_hook_block?(node)
20+
21+
add_offense(node)
22+
end
23+
end
24+
25+
private
26+
27+
def includes_migration_context?
28+
processed_source.ast.each_descendant(:send).any? { |node| migration_shared_context?(node) }
29+
end
30+
31+
def migration_shared_context?(node)
32+
node.method_name == :include_context &&
33+
node.receiver.nil? &&
34+
node.first_argument&.str_type? &&
35+
node.first_argument.value == 'migration'
36+
end
37+
38+
def inside_hook_block?(node)
39+
node.each_ancestor(:block).any? { |block| HOOK_METHODS.include?(block.send_node.method_name) }
40+
end
41+
42+
def migrator_run_call?(node)
43+
node.method_name == :run &&
44+
node.receiver&.const_type? &&
45+
['Migrator', 'Sequel::Migrator'].include?(node.receiver.const_name)
46+
end
47+
end
48+
end
49+
end
50+
end
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
require 'spec_helper'
2+
require 'rubocop'
3+
require 'rubocop/rspec/cop_helper'
4+
require 'rubocop/config'
5+
require 'linters/migration/use_migration_helpers'
6+
7+
RSpec.describe RuboCop::Cop::Migration::UseMigrationHelpers do
8+
include CopHelper
9+
10+
subject(:cop) { described_class.new(RuboCop::Config.new({})) }
11+
12+
let(:message) { described_class::MSG }
13+
14+
it 'registers an offense for Sequel::Migrator.run inside an example when the context is present' do
15+
result = inspect_source(<<~RUBY)
16+
RSpec.describe 'migration' do
17+
include_context 'migration' do
18+
let(:migration_filename) { 'x.rb' }
19+
end
20+
21+
it 'runs' do
22+
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error
23+
end
24+
end
25+
RUBY
26+
27+
expect(result.size).to eq(1)
28+
expect(result.map(&:message)).to eq([message])
29+
end
30+
31+
it 'registers an offense for an unqualified Migrator.run when the context is present' do
32+
result = inspect_source(<<~RUBY)
33+
RSpec.describe 'migration' do
34+
include_context 'migration' do
35+
let(:migration_filename) { 'x.rb' }
36+
end
37+
38+
it 'runs' do
39+
expect { Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error
40+
end
41+
end
42+
RUBY
43+
44+
expect(result.size).to eq(1)
45+
end
46+
47+
it 'registers one offense per direct call' do
48+
result = inspect_source(<<~RUBY)
49+
RSpec.describe 'migration' do
50+
include_context 'migration' do
51+
let(:migration_filename) { 'x.rb' }
52+
end
53+
54+
it 'runs' do
55+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
56+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1)
57+
end
58+
end
59+
RUBY
60+
61+
expect(result.size).to eq(2)
62+
end
63+
64+
it 'does not register an offense for a call inside a before hook' do
65+
result = inspect_source(<<~RUBY)
66+
RSpec.describe 'migration' do
67+
include_context 'migration' do
68+
let(:migration_filename) { 'x.rb' }
69+
end
70+
71+
before do
72+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
73+
end
74+
end
75+
RUBY
76+
77+
expect(result.size).to eq(0)
78+
end
79+
80+
it 'does not register an offense for a call inside an after hook' do
81+
result = inspect_source(<<~RUBY)
82+
RSpec.describe 'migration' do
83+
include_context 'migration' do
84+
let(:migration_filename) { 'x.rb' }
85+
end
86+
87+
after do
88+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
89+
end
90+
end
91+
RUBY
92+
93+
expect(result.size).to eq(0)
94+
end
95+
96+
it 'does not register an offense for a call inside an around hook' do
97+
result = inspect_source(<<~RUBY)
98+
RSpec.describe 'migration' do
99+
include_context 'migration' do
100+
let(:migration_filename) { 'x.rb' }
101+
end
102+
103+
around do |example|
104+
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
105+
example.run
106+
end
107+
end
108+
RUBY
109+
110+
expect(result.size).to eq(0)
111+
end
112+
113+
it 'does not register an offense when the migration shared context is absent' do
114+
result = inspect_source(<<~RUBY)
115+
RSpec.describe 'migration' do
116+
it 'runs' do
117+
expect { Sequel::Migrator.run(db, migrations_path, target: 1) }.not_to raise_error
118+
end
119+
end
120+
RUBY
121+
122+
expect(result.size).to eq(0)
123+
end
124+
125+
it 'does not register an offense when the helpers are used' do
126+
result = inspect_source(<<~RUBY)
127+
RSpec.describe 'migration' do
128+
include_context 'migration' do
129+
let(:migration_filename) { 'x.rb' }
130+
end
131+
132+
it 'runs' do
133+
expect { run_migration }.not_to raise_error
134+
test_up_migration_idempotency
135+
end
136+
end
137+
RUBY
138+
139+
expect(result.size).to eq(0)
140+
end
141+
end

‎spec/migrations/20230822153000_streamline_annotation_key_prefix_spec.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
anno2 = db[:isolation_segment_annotations].first(key: 'mykey')
1919
anno3 = db[:isolation_segment_annotations].first(key: 'yourkey')
2020

21-
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
21+
expect { run_migration }.not_to raise_error
2222

2323
# Check legacy prefix was converted
2424
anno1_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
@@ -36,6 +36,12 @@
3636
anno3_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-3-guid')
3737
expect(anno2.values).to eq(anno2_after_mig.values)
3838
expect(anno3.values).to eq(anno3_after_mig.values)
39+
40+
# Up migration idempotency
41+
test_up_migration_idempotency
42+
anno1_after_idempotency = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
43+
expect(anno1_after_idempotency[:key_prefix]).to eq 'mylegacyprefix'
44+
expect(anno1_after_idempotency[:key]).to eq 'mykey'
3945
end
4046
end
4147
end

‎spec/migrations/20231016094900_microsecond_timestamp_msql_asg_update_spec.rb‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
end
2727

2828
# Change TIMESTAMP to TIMESTAMP(6)
29-
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true) }.not_to raise_error
29+
expect { run_migration }.not_to raise_error
3030

3131
# the migration shouldn't add accuracy to previously inserted values
3232
t1_post_migration = ds.first(id: 1)
@@ -38,6 +38,25 @@
3838

3939
expect(t2[:last_update].to_datetime.subsec).not_to eq(0)
4040
expect(t2[:last_update].to_datetime.rfc3339(6)).to eq(dt_with_fractional_seconds.rfc3339(6))
41+
42+
# The up/down bodies only change the column type on MySQL, so the re-run and revert
43+
# precision assertions below are only meaningful there.
44+
next unless db.database_type == :mysql
45+
46+
# Up migration idempotency - microsecond precision retained
47+
test_up_migration_idempotency
48+
ds.insert(id: 3, last_update: dt_with_fractional_seconds)
49+
expect(ds.first(id: 3)[:last_update].to_datetime.subsec).not_to eq(0)
50+
51+
# Down migration - precision reverted to seconds
52+
expect { revert_migration }.not_to raise_error
53+
ds.insert(id: 4, last_update: dt_with_fractional_seconds)
54+
expect(ds.first(id: 4)[:last_update].to_datetime.subsec).to eq(0)
55+
56+
# Down migration idempotency - still seconds precision
57+
test_down_migration_idempotency
58+
ds.insert(id: 5, last_update: dt_with_fractional_seconds)
59+
expect(ds.first(id: 5)[:last_update].to_datetime.subsec).to eq(0)
4160
end
4261
end
4362
end

‎spec/migrations/20231113105256_add_service_plan_id_index_spec.rb‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
let(:migration_filename) { '20231113105256_add_service_plan_id_index.rb' }
77
end
88

9-
it 'succeeds' do
10-
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
9+
# The migration body is a no-op on MySQL, so only exercise it on PostgreSQL.
10+
it 'adds and removes the index' do
11+
skip if db.database_type != :postgres
12+
13+
expect(db.indexes(:service_plan_visibilities)).not_to include(:spv_service_plan_id_index)
14+
15+
expect { run_migration }.not_to raise_error
16+
expect(db.indexes(:service_plan_visibilities)).to include(:spv_service_plan_id_index)
17+
18+
expect { revert_migration }.not_to raise_error
19+
expect(db.indexes(:service_plan_visibilities)).not_to include(:spv_service_plan_id_index)
1120
end
1221
end

0 commit comments

Comments
 (0)