Skip to content

Commit

Permalink
Merge pull request #132 from doctolib/stop_ruby_2_support
Browse files Browse the repository at this point in the history
stop ruby 2 support
  • Loading branch information
CharlesDelannoy authored Dec 11, 2023
2 parents 3da0ae3 + f0fd295 commit bdf0387
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 123 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/continuous-integration-workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
strategy:
matrix:
postgres: [ 11.7, 12.14, 15.2 ]
ruby: [ 2.7, 3.0, 3.1, 3.2 ]
ruby: [ 3.0, 3.1, 3.2 ]
services:
postgres:
image: postgres:${{ matrix.postgres }}
Expand Down Expand Up @@ -67,9 +67,7 @@ jobs:
- ruby: "3.0"
gemfile: gemfiles/activerecord61.gemfile
- ruby: "3.0"
gemfile: gemfiles/activerecord60.gemfile
- ruby: 2.7
gemfile: gemfiles/activerecord60.gemfile
gemfile: gemfiles/activerecord70.gemfile
env:
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
services:
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.0
NewCops: enable

Layout/MultilineMethodCallIndentation:
Expand Down
29 changes: 21 additions & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,38 @@ PATH
remote: .
specs:
safe-pg-migrations (2.3.1)
activerecord (>= 6.0, < 7.1.x)
activesupport (>= 6.0, < 7.1.x)
activerecord (>= 6.1)
activesupport (>= 6.1)

GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.7.2)
activesupport (= 7.0.7.2)
activerecord (7.0.7.2)
activemodel (= 7.0.7.2)
activesupport (= 7.0.7.2)
activesupport (7.0.7.2)
activemodel (7.1.2)
activesupport (= 7.1.2)
activerecord (7.1.2)
activemodel (= 7.1.2)
activesupport (= 7.1.2)
timeout (>= 0.4.0)
activesupport (7.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.4)
coderay (1.1.3)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
coolline (0.5.0)
unicode_utils (~> 1.4)
drb (2.2.0)
ruby2_keywords
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
Expand All @@ -31,6 +42,7 @@ GEM
minitest (5.18.1)
mocha (2.0.4)
ruby2_keywords (>= 0.0.5)
mutex_m (0.2.0)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
Expand Down Expand Up @@ -64,6 +76,7 @@ GEM
ruby2_keywords (0.0.5)
strong_migrations (1.4.3)
activerecord (>= 5.2)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source 'https://rubygems.org'

gemspec path: '..'

gem 'activerecord', '~> 6.0.0'
gem 'activerecord', '~> 7.0.0'
gem 'bundler'
gem 'minitest', '>= 5'
gem 'mocha'
Expand Down
7 changes: 2 additions & 5 deletions lib/safe-pg-migrations/plugins/blocking_activity_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,19 @@ module BlockingActivityLogger
include Helpers::StatementsHelper

RETRIABLE_SCHEMA_STATEMENTS.each do |method|
define_method method do |*args, &block|
define_method method do |*args, **options, &block|
log_context = lambda do
break unless SafePgMigrations.config.sensitive_logger

options = args.last.is_a?(Hash) ? args.last : {}

Helpers::Logger.say "Executing #{SafePgMigrations.current_migration.name}",
sensitive: true, warn_sensitive_logs: false
Helpers::Logger.say_method_call method, *args, **options, sensitive: true, warn_sensitive_logs: false
end

log_blocking_queries_after_lock(log_context) do
super(*args, &block)
super(*args, **options, &block)
end
end
ruby2_keywords method
end

%i[add_index remove_index].each do |method|
Expand Down
44 changes: 23 additions & 21 deletions lib/safe-pg-migrations/plugins/idempotent_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ module SafePgMigrations
module IdempotentStatements
include Helpers::IndexHelper

ruby2_keywords def add_index(table_name, column_name, *args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_index(table_name, column_name, **options)
index_definition = index_definition(table_name, column_name, **options)

return super unless index_name_exists?(index_definition.table, index_definition.name)
Expand All @@ -15,42 +14,44 @@ module IdempotentStatements
return
end

remove_index(table_name, name: index_definition.name)
remove_index(table_name, column_name, **options)
super
end

ruby2_keywords def add_column(table_name, column_name, type, *)
def add_column(table_name, column_name, type, **options)
if column_exists?(table_name, column_name) && !column_exists?(table_name, column_name, type)
error_message = "/!\\ Column '#{column_name}' already exists in '#{table_name}' with a different type"
raise error_message
end

return super unless column_exists?(table_name, column_name, type)
options_without_default_value_backfill = options.except(:default_value_backfill)

log_message(<<~MESSAGE.squish
/!\\ Column '#{column_name}' already exists in '#{table_name}' with the same type (#{type}).
Skipping statement.
MESSAGE
)
if column_exists?(table_name, column_name, type)
log_message(<<~MESSAGE.squish
/!\\ Column '#{column_name}' already exists in '#{table_name}' with the same type (#{type}).
Skipping statement.
MESSAGE
)
else
super(table_name, column_name, type, **options_without_default_value_backfill)
end
end

ruby2_keywords def remove_column(table_name, column_name, type = nil, *)
def remove_column(table_name, column_name, type = nil, **options)
return super if column_exists?(table_name, column_name)

log_message("/!\\ Column '#{column_name}' not found on table '#{table_name}'. Skipping statement.")
end

ruby2_keywords def remove_index(table_name, *args)
options = args.last.is_a?(Hash) ? args.last : {}
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, options)
def remove_index(table_name, column_name = nil, **options)
index_name = options.key?(:name) ? options[:name].to_s : index_name(table_name, column: column_name)

return super if index_name_exists?(table_name, index_name)

log_message("/!\\ Index '#{index_name}' not found on table '#{table_name}'. Skipping statement.")
end

ruby2_keywords def add_foreign_key(from_table, to_table, *args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_foreign_key(from_table, to_table, **options)
sub_options = options.slice(:name, :column)
return super unless foreign_key_exists?(from_table, sub_options.present? ? nil : to_table, **sub_options)

Expand All @@ -64,13 +65,12 @@ def remove_foreign_key(from_table, to_table = nil, **options)
log_message("/!\\ Foreign key '#{from_table}' -> '#{reference_name}' does not exist. Skipping statement.")
end

ruby2_keywords def create_table(table_name, *args)
options = args.last.is_a?(Hash) ? args.last : {}
def create_table(table_name, **options)
return super if options[:force] || !table_exists?(table_name)

Helpers::Logger.say "/!\\ Table '#{table_name}' already exists.", sub_item: true

td = create_table_definition(table_name, *args)
td = create_table_definition(table_name, **options)

yield td if block_given?

Expand All @@ -82,8 +82,10 @@ def remove_foreign_key(from_table, to_table = nil, **options)
end

def add_check_constraint(table_name, expression, **options)
options_without_validate = options.except(:validate)
constraint_definition = check_constraint_for table_name,
**check_constraint_options(table_name, expression, options)
**check_constraint_options(table_name, expression,
options_without_validate)

return super if constraint_definition.nil?

Expand Down Expand Up @@ -123,7 +125,7 @@ def change_column_default(table_name, column_name, default_or_changes)
)
end

ruby2_keywords def drop_table(table_name, *)
def drop_table(table_name, **options)
return super if table_exists?(table_name)

log_message("/!\\ Table '#{table_name} does not exist. Skipping statement.")
Expand Down
24 changes: 10 additions & 14 deletions lib/safe-pg-migrations/plugins/statement_insurer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def validate_foreign_key(*, **)
without_statement_timeout { super }
end

ruby2_keywords def add_foreign_key(from_table, to_table, *args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_foreign_key(from_table, to_table, **options)
validate_present = options.key?(:validate)
options[:validate] = false unless validate_present

Expand All @@ -43,7 +42,7 @@ def validate_foreign_key(*, **)
validate_foreign_key from_table, sub_options.present? ? nil : to_table, **sub_options
end

ruby2_keywords def create_table(*)
def create_table(*)
super do |td|
yield td if block_given?
td.indexes.map! do |key, index_options|
Expand All @@ -53,9 +52,7 @@ def validate_foreign_key(*, **)
end
end

ruby2_keywords def add_index(table_name, column_name, *args_options)
options = args_options.last.is_a?(Hash) ? args_options.last : {}

def add_index(table_name, column_name, **options)
if options[:algorithm] == :default
options.delete :algorithm
else
Expand All @@ -66,29 +63,28 @@ def validate_foreign_key(*, **)
without_timeout { super(table_name, column_name, **options) }
end

ruby2_keywords def remove_index(table_name, *args)
options = args.last.is_a?(Hash) ? args.last : { column: args.last }
def remove_index(table_name, column_name = nil, **options)
options[:algorithm] = :concurrently unless options.key?(:algorithm)

Helpers::Logger.say_method_call(:remove_index, table_name, **options)
without_timeout { super(table_name, **options) }
Helpers::Logger.say_method_call(:remove_index, table_name, column_name, **options)
without_timeout { super(table_name, column_name, **options) }
end

def remove_column(table_name, column_name, *)
def remove_column(table_name, column_name, type = nil, **options)
foreign_key = foreign_key_for(table_name, column: column_name)

remove_foreign_key(table_name, name: foreign_key.name) if foreign_key
super
end

ruby2_keywords def drop_table(table_name, *args)
def drop_table(table_name, **options)
foreign_keys(table_name).each do |foreign_key|
remove_foreign_key(table_name, name: foreign_key.name)
end

Helpers::Logger.say_method_call :drop_table, table_name, *args
Helpers::Logger.say_method_call :drop_table, table_name, **options

super(table_name, *args)
super
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
module SafePgMigrations
module StatementInsurer
module AddColumn
ruby2_keywords def add_column(table_name, column_name, type, *args)
options = args.last.is_a?(Hash) && args.last
options ||= {}

def add_column(table_name, column_name, type, **options)
return super if should_keep_default_implementation?(**options)

options.delete(:default_value_backfill)

raise <<~ERROR unless backfill_column_default_safe?(table_name)
Table #{table_name} has more than #{SafePgMigrations.config.default_value_backfill_threshold} rows.
Backfilling the default value for column #{column_name} on table #{table_name} would take too long.
Expand Down
5 changes: 2 additions & 3 deletions lib/safe-pg-migrations/plugins/statement_retrier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ module StatementRetrier
include Helpers::StatementsHelper

RETRIABLE_SCHEMA_STATEMENTS.each do |method|
define_method method do |*args, &block|
retry_if_lock_timeout { super(*args, &block) }
define_method method do |*args, **options, &block|
retry_if_lock_timeout { super(*args, **options, &block) }
end
ruby2_keywords method
end

private
Expand Down
17 changes: 7 additions & 10 deletions lib/safe-pg-migrations/plugins/strong_migrations_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,19 @@ def strong_migration_available?
].freeze

SAFE_METHODS.each do |method|
define_method method do |*args|
return super(*args) unless respond_to?(:safety_assured)
define_method method do |*args, **options|
return super(*args, **options) unless respond_to?(:safety_assured)

safety_assured { super(*args) }
safety_assured { super(*args, **options) }
end
ruby2_keywords method
end

ruby2_keywords def add_column(table_name, *args)
return super(table_name, *args) unless respond_to?(:safety_assured)
def add_column(table_name, *args, **options)
return super unless respond_to?(:safety_assured)

options = args.last.is_a?(Hash) ? args.last : {}
return safety_assured { super } if options.fetch(:default_value_backfill, :auto) == :auto

return safety_assured { super(table_name, *args) } if options.fetch(:default_value_backfill, :auto) == :auto

super(table_name, *args)
super
end
end
end
14 changes: 5 additions & 9 deletions lib/safe-pg-migrations/plugins/useless_statements_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,32 @@
module SafePgMigrations
module UselessStatementsLogger
class << self
ruby2_keywords def warn_useless(action, link = nil, *args)
def warn_useless(action, link = nil, *args)
Helpers::Logger.say(
"/!\\ No need to explicitly use #{action}, safe-pg-migrations does it for you", *args
)
Helpers::Logger.say "\t see #{link} for more details", *args if link
end
end

ruby2_keywords def add_index(*args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_index(table_name, column_name, **options)
warn_for_index(**options)
super
end

ruby2_keywords def remove_index(table_name, *args)
options = args.last.is_a?(Hash) ? args.last : {}
def remove_index(table_name, column_name = nil, **options)
warn_for_index(**options) unless options.empty?
super
end

ruby2_keywords def add_foreign_key(*args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_foreign_key(from_table, to_table, **options)
if options[:validate] == false
UselessStatementsLogger.warn_useless '`validate: :false`', 'https://github.com/doctolib/safe-pg-migrations#safe_add_foreign_key'
end
super
end

ruby2_keywords def add_check_constraint(*args)
options = args.last.is_a?(Hash) ? args.last : {}
def add_check_constraint(table_name, expression, **options)
if options[:validate] == false
UselessStatementsLogger.warn_useless '`validate: :false`', 'https://github.com/doctolib/safe-pg-migrations#safe_add_check_constraint'
end
Expand Down
Loading

0 comments on commit bdf0387

Please sign in to comment.