Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .rubocop_cc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ require:
- ./spec/linters/migration/add_constraint_name.rb
- ./spec/linters/migration/include_string_size.rb
- ./spec/linters/migration/no_model_in_specs.rb
- ./spec/linters/migration/require_migration_shared_context.rb
- ./spec/linters/migration/require_primary_key.rb
- ./spec/linters/migration/too_many_migration_runs.rb
- ./spec/linters/migration/use_migration_helpers.rb
- ./spec/linters/match_requires_with_includes.rb
- ./spec/linters/prefer_oj_over_other_json_libraries.rb

Expand Down Expand Up @@ -126,6 +128,21 @@ Style/RaiseArgs:
Migration/TooManyMigrationRuns:
Exclude:
- spec/linters/migration/too_many_migration_runs.rb
Migration/UseMigrationHelpers:
Exclude:
- spec/migrations/helpers/**/*
Migration/RequireMigrationSharedContext:
Include:
- spec/migrations/**/*
Exclude:
- spec/migrations/helpers/**/*
# old migration specs
- !ruby/regexp /spec/migrations/201([0-9]).+_spec\.rb$/
- !ruby/regexp /spec/migrations/202([0-2]).+_spec\.rb$/
# specs not testing a specific migration (no timestamp prefix)
- !ruby/regexp /spec/migrations/[a-z]+.+_spec\.rb$/
# bigint migration step specs use their own 'bigint migration *' contexts by design
- !ruby/regexp /spec/migrations/.+_bigint_migration_.+_spec\.rb$/

#### ENABLED SECTION
Capybara/FindAllFirst:
Expand Down
32 changes: 32 additions & 0 deletions spec/linters/migration/require_migration_shared_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module RuboCop
module Cop
module Migration
class RequireMigrationSharedContext < Base
MSG = "Per-migration specs must include the 'migration' shared context " \
"(include_context 'migration') so they use the shared setup and helpers " \
'(run_migration, revert_migration, test_up_migration_idempotency, ' \
'test_down_migration_idempotency). See spec/migrations/Readme.md for details.'.freeze

def on_new_investigation
return unless processed_source.ast
return if includes_migration_context?

add_offense(processed_source.ast)
end

private

def includes_migration_context?
processed_source.ast.each_descendant(:send).any? { |node| migration_shared_context?(node) }
end

def migration_shared_context?(node)
node.method_name == :include_context &&
node.receiver.nil? &&
node.first_argument&.str_type? &&
node.first_argument.value == 'migration'
end
end
end
end
end
60 changes: 60 additions & 0 deletions spec/linters/migration/require_migration_shared_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/cop_helper'
require 'rubocop/config'
require 'linters/migration/require_migration_shared_context'

RSpec.describe RuboCop::Cop::Migration::RequireMigrationSharedContext do
include CopHelper

subject(:cop) { described_class.new(RuboCop::Config.new({})) }

let(:message) { described_class::MSG }

it 'registers an offense when the migration shared context is missing' do
result = inspect_source(<<~RUBY)
RSpec.describe 'adds a column' do
it 'works' do
expect(true).to be(true)
end
end
RUBY

expect(result.size).to eq(1)
expect(result.map(&:message)).to eq([message])
end

it 'does not register an offense when the context is included via a block' do
result = inspect_source(<<~RUBY)
RSpec.describe 'adds a column' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end
end
RUBY

expect(result.size).to eq(0)
end

it 'does not register an offense for the bare include_context form' do
result = inspect_source(<<~RUBY)
RSpec.describe 'adds a column' do
include_context 'migration'
end
RUBY

expect(result.size).to eq(0)
end

it 'registers an offense when only a different shared context is included' do
result = inspect_source(<<~RUBY)
RSpec.describe 'adds a column' do
include_context 'bigint migration step1' do
let(:migration_filename) { 'x.rb' }
end
end
RUBY

expect(result.size).to eq(1)
end
end
50 changes: 50 additions & 0 deletions spec/linters/migration/use_migration_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module RuboCop
module Cop
module Migration
class UseMigrationHelpers < Base
MSG = 'Do not call Sequel::Migrator.run directly in a migration spec that includes ' \
"the 'migration' shared context. " \
'Use the provided helpers instead: run_migration, revert_migration, ' \
'test_up_migration_idempotency, test_down_migration_idempotency. ' \
'See spec/migrations/Readme.md for details.'.freeze

HOOK_METHODS = %i[before after around].freeze

def on_new_investigation
return unless processed_source.ast
return unless includes_migration_context?

processed_source.ast.each_descendant(:send) do |node|
next unless migrator_run_call?(node)
next if inside_hook_block?(node)

add_offense(node)
end
end

private

def includes_migration_context?
processed_source.ast.each_descendant(:send).any? { |node| migration_shared_context?(node) }
end

def migration_shared_context?(node)
node.method_name == :include_context &&
node.receiver.nil? &&
node.first_argument&.str_type? &&
node.first_argument.value == 'migration'
end

def inside_hook_block?(node)
node.each_ancestor(:block).any? { |block| HOOK_METHODS.include?(block.send_node.method_name) }
end

def migrator_run_call?(node)
node.method_name == :run &&
node.receiver&.const_type? &&
['Migrator', 'Sequel::Migrator'].include?(node.receiver.const_name)
end
end
end
end
end
141 changes: 141 additions & 0 deletions spec/linters/migration/use_migration_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/cop_helper'
require 'rubocop/config'
require 'linters/migration/use_migration_helpers'

RSpec.describe RuboCop::Cop::Migration::UseMigrationHelpers do
include CopHelper

subject(:cop) { described_class.new(RuboCop::Config.new({})) }

let(:message) { described_class::MSG }

it 'registers an offense for Sequel::Migrator.run inside an example when the context is present' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

it 'runs' do
expect { Sequel::Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error
end
end
RUBY

expect(result.size).to eq(1)
expect(result.map(&:message)).to eq([message])
end

it 'registers an offense for an unqualified Migrator.run when the context is present' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

it 'runs' do
expect { Migrator.run(db, migrations_path, target: current_migration_index) }.not_to raise_error
end
end
RUBY

expect(result.size).to eq(1)
end

it 'registers one offense per direct call' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

it 'runs' do
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
Sequel::Migrator.run(db, migrations_path, target: current_migration_index - 1)
end
end
RUBY

expect(result.size).to eq(2)
end

it 'does not register an offense for a call inside a before hook' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

before do
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
end
end
RUBY

expect(result.size).to eq(0)
end

it 'does not register an offense for a call inside an after hook' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

after do
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
end
end
RUBY

expect(result.size).to eq(0)
end

it 'does not register an offense for a call inside an around hook' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

around do |example|
Sequel::Migrator.run(db, migrations_path, target: current_migration_index)
example.run
end
end
RUBY

expect(result.size).to eq(0)
end

it 'does not register an offense when the migration shared context is absent' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
it 'runs' do
expect { Sequel::Migrator.run(db, migrations_path, target: 1) }.not_to raise_error
end
end
RUBY

expect(result.size).to eq(0)
end

it 'does not register an offense when the helpers are used' do
result = inspect_source(<<~RUBY)
RSpec.describe 'migration' do
include_context 'migration' do
let(:migration_filename) { 'x.rb' }
end

it 'runs' do
expect { run_migration }.not_to raise_error
test_up_migration_idempotency
end
end
RUBY

expect(result.size).to eq(0)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
anno2 = db[:isolation_segment_annotations].first(key: 'mykey')
anno3 = db[:isolation_segment_annotations].first(key: 'yourkey')

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

# Check legacy prefix was converted
anno1_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
Expand All @@ -36,6 +36,12 @@
anno3_after_mig = db[:isolation_segment_annotations].first(guid: 'anno-3-guid')
expect(anno2.values).to eq(anno2_after_mig.values)
expect(anno3.values).to eq(anno3_after_mig.values)

# Up migration idempotency
test_up_migration_idempotency
anno1_after_idempotency = db[:isolation_segment_annotations].first(guid: 'anno-1-guid')
expect(anno1_after_idempotency[:key_prefix]).to eq 'mylegacyprefix'
expect(anno1_after_idempotency[:key]).to eq 'mykey'
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

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

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

expect(t2[:last_update].to_datetime.subsec).not_to eq(0)
expect(t2[:last_update].to_datetime.rfc3339(6)).to eq(dt_with_fractional_seconds.rfc3339(6))

# The up/down bodies only change the column type on MySQL, so the re-run and revert
# precision assertions below are only meaningful there.
next unless db.database_type == :mysql

# Up migration idempotency - microsecond precision retained
test_up_migration_idempotency
ds.insert(id: 3, last_update: dt_with_fractional_seconds)
expect(ds.first(id: 3)[:last_update].to_datetime.subsec).not_to eq(0)

# Down migration - precision reverted to seconds
expect { revert_migration }.not_to raise_error
ds.insert(id: 4, last_update: dt_with_fractional_seconds)
expect(ds.first(id: 4)[:last_update].to_datetime.subsec).to eq(0)

# Down migration idempotency - still seconds precision
test_down_migration_idempotency
ds.insert(id: 5, last_update: dt_with_fractional_seconds)
expect(ds.first(id: 5)[:last_update].to_datetime.subsec).to eq(0)
end
end
end
13 changes: 11 additions & 2 deletions spec/migrations/20231113105256_add_service_plan_id_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
let(:migration_filename) { '20231113105256_add_service_plan_id_index.rb' }
end

it 'succeeds' do
Sequel::Migrator.run(db, migrations_path, target: current_migration_index, allow_missing_migration_files: true)
# The migration body is a no-op on MySQL, so only exercise it on PostgreSQL.
it 'adds and removes the index' do
skip if db.database_type != :postgres

expect(db.indexes(:service_plan_visibilities)).not_to include(:spv_service_plan_id_index)

expect { run_migration }.not_to raise_error
expect(db.indexes(:service_plan_visibilities)).to include(:spv_service_plan_id_index)

expect { revert_migration }.not_to raise_error
expect(db.indexes(:service_plan_visibilities)).not_to include(:spv_service_plan_id_index)
end
end
Loading
Loading