1
1
# frozen_string_literal: true
2
2
3
3
module SafePgMigrations
4
- module StatementInsurer
4
+ module StatementInsurer # rubocop:disable Metrics/ModuleLength
5
5
PG_11_VERSION_NUM = 110_000
6
6
7
7
%i[ change_column_null change_column ] . each do |method |
8
8
define_method method do |*args , &block |
9
9
with_setting ( :statement_timeout , SafePgMigrations . config . pg_safe_timeout ) { super ( *args , &block ) }
10
10
end
11
+ ruby2_keywords method
11
12
end
12
13
13
- def add_column ( table_name , column_name , type , **options )
14
+ ruby2_keywords def add_column ( table_name , column_name , type , *args ) # rubocop:disable Metrics/CyclomaticComplexity
15
+ options = args . last . is_a? ( Hash ) ? args . last : { }
14
16
return super if SafePgMigrations . pg_version_num >= PG_11_VERSION_NUM
15
17
16
18
default = options . delete ( :default )
@@ -36,17 +38,21 @@ def add_column(table_name, column_name, type, **options)
36
38
end
37
39
end
38
40
39
- def add_foreign_key ( from_table , to_table , **options )
41
+ ruby2_keywords def add_foreign_key ( from_table , to_table , *args )
42
+ options = args . last . is_a? ( Hash ) ? args . last : { }
40
43
validate_present = options . key? :validate
41
44
options [ :validate ] = false unless validate_present
45
+ with_setting ( :statement_timeout , SafePgMigrations . config . pg_safe_timeout ) do
46
+ super ( from_table , to_table , **options )
47
+ end
42
48
43
- with_setting ( :statement_timeout , SafePgMigrations . config . pg_safe_timeout ) { super }
49
+ return if validate_present
44
50
45
- options_or_to_table = options . slice ( :name , :column ) . presence || to_table
46
- without_statement_timeout { validate_foreign_key from_table , options_or_to_table } unless validate_present
51
+ suboptions = options . slice ( :name , :column )
52
+ without_statement_timeout { validate_foreign_key from_table , suboptions . present? ? nil : to_table , ** suboptions }
47
53
end
48
54
49
- def create_table ( *)
55
+ ruby2_keywords def create_table ( *)
50
56
with_setting ( :statement_timeout , SafePgMigrations . config . pg_safe_timeout ) do
51
57
super do |td |
52
58
yield td if block_given?
@@ -65,17 +71,17 @@ def add_index(table_name, column_name, **options)
65
71
options [ :algorithm ] = :concurrently
66
72
end
67
73
68
- SafePgMigrations . say_method_call ( :add_index , table_name , column_name , options )
74
+ SafePgMigrations . say_method_call ( :add_index , table_name , column_name , ** options )
69
75
70
- without_timeout { super }
76
+ without_timeout { super ( table_name , column_name , ** options ) }
71
77
end
72
78
73
- def remove_index ( table_name , options = { } )
74
- options = { column : options } unless options . is_a? ( Hash )
75
- options [ :algorithm ] = :concurrently
76
- SafePgMigrations . say_method_call ( :remove_index , table_name , options )
79
+ ruby2_keywords def remove_index ( table_name , * args )
80
+ options = args . last . is_a? ( Hash ) ? args . last : { column : args . last }
81
+ options [ :algorithm ] = :concurrently unless options . key? ( :algorithm )
82
+ SafePgMigrations . say_method_call ( :remove_index , table_name , ** options )
77
83
78
- without_timeout { super }
84
+ without_timeout { super ( table_name , ** options ) }
79
85
end
80
86
81
87
def backfill_column_default ( table_name , column_name )
0 commit comments