Skip to content
Draft
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
23 changes: 23 additions & 0 deletions db/migrations/20261127140000_add_domain_lookup_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Sequel.migration do
no_transaction # to create and drop indexes concurrently

up do
# MySQL already indexes these foreign key columns.
next unless database_type == :postgres

VCAP::Migration.with_concurrent_timeout(self) do
add_index :domains, :owning_organization_id, name: :domains_owning_org_id_index, if_not_exists: true, concurrently: true
add_index :organizations_private_domains, %i[private_domain_id organization_id],
name: :orgs_pd_domain_org_ids, if_not_exists: true, concurrently: true
end
end

down do
next unless database_type == :postgres

VCAP::Migration.with_concurrent_timeout(self) do
drop_index :domains, nil, name: :domains_owning_org_id_index, if_exists: true, concurrently: true
drop_index :organizations_private_domains, nil, name: :orgs_pd_domain_org_ids, if_exists: true, concurrently: true
end
end
end
20 changes: 20 additions & 0 deletions spec/linters/migration/migration_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module RuboCop
module Cop
module Migration
module MigrationContext
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
23 changes: 23 additions & 0 deletions spec/linters/migration/require_migration_shared_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require_relative 'migration_context'

module RuboCop
module Cop
module Migration
class RequireMigrationSharedContext < Base
include MigrationContext

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
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
40 changes: 26 additions & 14 deletions spec/linters/migration/too_many_migration_runs.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
require_relative 'migration_context'

module RuboCop
module Cop
module Migration
class TooManyMigrationRuns < Base
include MigrationContext

MIGRATION_HELPERS = %i[run_migration revert_migration test_up_migration_idempotency test_down_migration_idempotency].freeze

def on_new_investigation
return unless processed_source.ast

@includes_migration_context = includes_migration_context?
definitions = extract_migrator_definitions
call_count = count_migrator_calls(definitions)

Expand Down Expand Up @@ -38,7 +45,7 @@ def extract_migrator_definitions
end

def extract_migrator_method(node, method_names)
return unless contains_migrator_run?(node)
return unless contains_migration_run?(node)

method_names << node.method_name
end
Expand All @@ -54,12 +61,12 @@ def extract_block_definitions(node, definitions)
when :let, :let!
extract_named_migrator(node, definitions[:let_names])
when :before, :after, :around
definitions[:before_after_blocks].add(node.object_id) if contains_migrator_run?(node)
definitions[:before_after_blocks].add(node.object_id) if contains_migration_run?(node)
end
end

def extract_named_migrator(node, names)
return unless contains_migrator_run?(node)
return unless contains_migration_run?(node)

first_arg = node.send_node.first_argument
names << first_arg.value if first_arg&.sym_type?
Expand All @@ -84,20 +91,25 @@ def migration_call?(node, definitions)
end

if in_before_after_block
# Count direct Migrator.run calls inside before/after/around blocks
migrator_run_call?(node)
# Count migration runs inside before/after/around blocks
migration_run_call?(node)
else
# Count direct calls (not in definitions) or helper invocations
direct_migrator_call?(node) || helper_migration_call?(node, definitions)
# Count direct calls (not in definitions) or local wrapper invocations
direct_migration_call?(node) || local_migration_wrapper_call?(node, definitions)
end
end

def direct_migrator_call?(node)
return false unless migrator_run_call?(node)
def direct_migration_call?(node)
return false unless migration_run_call?(node)

!inside_definition?(node)
end

def migration_run_call?(node)
migrator_run_call?(node) ||
(@includes_migration_context && MIGRATION_HELPERS.include?(node.method_name) && (node.receiver.nil? || node.receiver.self_type?))
end

def migrator_run_call?(node)
return false unless node.method_name == :run

Expand All @@ -113,28 +125,28 @@ def migrator_run_call?(node)
false
end

def helper_migration_call?(node, definitions)
def local_migration_wrapper_call?(node, definitions)
method = node.method_name
definitions[:subject_names].include?(method) ||
definitions[:let_names].include?(method) ||
definitions[:method_names].include?(method)
end

def contains_migrator_run?(node)
node.each_descendant(:send).any? { |send_node| migrator_run_call?(send_node) }
def contains_migration_run?(node)
node.each_descendant(:send).any? { |send_node| migration_run_call?(send_node) }
end

def inside_definition?(node)
node.each_ancestor(:def, :block).any? do |ancestor|
case ancestor.type
when :def
contains_migrator_run?(ancestor)
contains_migration_run?(ancestor)
when :block
next false unless ancestor.send_node

method = ancestor.send_node.method_name
if %i[subject let let!].include?(method)
contains_migrator_run?(ancestor)
contains_migration_run?(ancestor)
else
%i[before after around].include?(method)
end
Expand Down
Loading
Loading