-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
compatibility.rb
490 lines (411 loc) · 14.9 KB
/
compatibility.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# frozen_string_literal: true
module ActiveRecord
class Migration
module Compatibility # :nodoc: all
def self.find(version)
version = version.to_s
name = "V#{version.tr('.', '_')}"
unless const_defined?(name)
versions = constants.grep(/\AV[0-9_]+\z/).map { |s| s.to_s.delete("V").tr("_", ".").inspect }
raise ArgumentError, "Unknown migration version #{version.inspect}; expected one of #{versions.sort.join(', ')}"
end
const_get(name)
end
# This file exists to ensure that old migrations run the same way they did before a Rails upgrade.
# e.g. if you write a migration on Rails 6.1, then upgrade to Rails 7, the migration should do the same thing to your
# database as it did when you were running Rails 6.1
#
# "Current" is an alias for `ActiveRecord::Migration`, it represents the current Rails version.
# New migration functionality that will never be backward compatible should be added directly to `ActiveRecord::Migration`.
#
# There are classes for each prior Rails version. Each class descends from the *next* Rails version, so:
# 5.2 < 6.0 < 6.1 < 7.0 < 7.1 < 7.2 < 8.0
#
# If you are introducing new migration functionality that should only apply from Rails 7 onward, then you should
# find the class that immediately precedes it (6.1), and override the relevant migration methods to undo your changes.
#
# For example, Rails 6 added a default value for the `precision` option on datetime columns. So in this file, the `V5_2`
# class sets the value of `precision` to `nil` if it's not explicitly provided. This way, the default value will not apply
# for migrations written for 5.2, but will for migrations written for 6.0.
V8_0 = Current
class V7_2 < V8_0
end
class V7_1 < V7_2
end
class V7_0 < V7_1
module LegacyIndexName
private
def legacy_index_name(table_name, options)
if Hash === options
if options[:column]
"index_#{table_name}_on_#{Array(options[:column]) * '_and_'}"
elsif options[:name]
options[:name]
else
raise ArgumentError, "You must specify the index name"
end
else
legacy_index_name(table_name, index_name_options(options))
end
end
def index_name_options(column_names)
if expression_column_name?(column_names)
column_names = column_names.scan(/\w+/).join("_")
end
{ column: column_names }
end
def expression_column_name?(column_name)
column_name.is_a?(String) && /\W/.match?(column_name)
end
end
module TableDefinition
include LegacyIndexName
def column(name, type, **options)
options[:_skip_validate_options] = true
super
end
def change(name, type, **options)
options[:_skip_validate_options] = true
super
end
def index(column_name, **options)
options[:name] = legacy_index_name(name, column_name) if options[:name].nil?
super
end
def references(*args, **options)
options[:_skip_validate_options] = true
super
end
private
def raise_on_if_exist_options(options)
end
end
include LegacyIndexName
def add_column(table_name, column_name, type, **options)
options[:_skip_validate_options] = true
super
end
def add_index(table_name, column_name, **options)
options[:name] = legacy_index_name(table_name, column_name) if options[:name].nil?
super
end
def add_reference(table_name, ref_name, **options)
options[:_skip_validate_options] = true
super
end
alias :add_belongs_to :add_reference
def create_table(table_name, **options)
options[:_uses_legacy_table_name] = true
options[:_skip_validate_options] = true
super
end
def rename_table(table_name, new_name, **options)
options[:_uses_legacy_table_name] = true
options[:_uses_legacy_index_name] = true
super
end
def change_column(table_name, column_name, type, **options)
options[:_skip_validate_options] = true
if connection.adapter_name == "Mysql2" || connection.adapter_name == "Trilogy"
options[:collation] ||= :no_collation
end
super
end
def change_column_null(table_name, column_name, null, default = nil)
super(table_name, column_name, !!null, default)
end
def disable_extension(name, **options)
if connection.adapter_name == "PostgreSQL"
options[:force] = :cascade
end
super
end
def add_foreign_key(from_table, to_table, **options)
if connection.adapter_name == "PostgreSQL" && options[:deferrable] == true
options[:deferrable] = :immediate
end
super
end
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
end
class V6_1 < V7_0
class PostgreSQLCompat
def self.compatible_timestamp_type(type, connection)
if connection.adapter_name == "PostgreSQL"
# For Rails <= 6.1, :datetime was aliased to :timestamp
# See: https://github.com/rails/rails/blob/v6.1.3.2/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L108
# From Rails 7 onwards, you can define what :datetime resolves to (the default is still :timestamp)
# See `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.datetime_type`
type.to_sym == :datetime ? :timestamp : type
else
type
end
end
end
def add_column(table_name, column_name, type, **options)
if type == :datetime
options[:precision] ||= nil
end
type = PostgreSQLCompat.compatible_timestamp_type(type, connection)
super
end
def change_column(table_name, column_name, type, **options)
if type == :datetime
options[:precision] ||= nil
end
type = PostgreSQLCompat.compatible_timestamp_type(type, connection)
super
end
module TableDefinition
def new_column_definition(name, type, **options)
type = PostgreSQLCompat.compatible_timestamp_type(type, @conn)
super
end
def change(name, type, index: nil, **options)
options[:precision] ||= nil
super
end
def column(name, type, index: nil, **options)
options[:precision] ||= nil
super
end
private
def raise_on_if_exist_options(options)
end
end
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
end
class V6_0 < V6_1
class ReferenceDefinition < ConnectionAdapters::ReferenceDefinition
def index_options(table_name)
as_options(index)
end
end
module TableDefinition
def references(*args, **options)
options[:_uses_legacy_reference_index_name] = true
super
end
alias :belongs_to :references
def column(name, type, index: nil, **options)
options[:precision] ||= nil
super
end
private
def raise_on_if_exist_options(options)
end
end
def add_reference(table_name, ref_name, **options)
if connection.adapter_name == "SQLite"
options[:type] = :integer
end
options[:_uses_legacy_reference_index_name] = true
super
end
alias :add_belongs_to :add_reference
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
end
class V5_2 < V6_0
module TableDefinition
def timestamps(**options)
options[:precision] ||= nil
super
end
def column(name, type, index: nil, **options)
options[:precision] ||= nil
super
end
private
def raise_on_if_exist_options(options)
end
def raise_on_duplicate_column(name)
end
end
module CommandRecorder
def invert_transaction(args, &block)
[:transaction, args, block]
end
def invert_change_column_comment(args)
[:change_column_comment, args]
end
def invert_change_table_comment(args)
[:change_table_comment, args]
end
end
def add_timestamps(table_name, **options)
options[:precision] ||= nil
super
end
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
def command_recorder
recorder = super
class << recorder
prepend CommandRecorder
end
recorder
end
end
class V5_1 < V5_2
def change_column(table_name, column_name, type, **options)
if connection.adapter_name == "PostgreSQL"
super(table_name, column_name, type, **options.except(:default, :null, :comment))
connection.change_column_default(table_name, column_name, options[:default]) if options.key?(:default)
connection.change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)
connection.change_column_comment(table_name, column_name, options[:comment]) if options.key?(:comment)
else
super
end
end
def create_table(table_name, **options)
if connection.adapter_name == "Mysql2" || connection.adapter_name == "Trilogy"
super(table_name, options: "ENGINE=InnoDB", **options)
else
super
end
end
end
class V5_0 < V5_1
module TableDefinition
def primary_key(name, type = :primary_key, **options)
type = :integer if type == :primary_key
super
end
def references(*args, **options)
super(*args, type: :integer, **options)
end
alias :belongs_to :references
private
def raise_on_if_exist_options(options)
end
end
def create_table(table_name, **options)
if connection.adapter_name == "PostgreSQL"
if options[:id] == :uuid && !options.key?(:default)
options[:default] = "uuid_generate_v4()"
end
end
unless ["Mysql2", "Trilogy"].include?(connection.adapter_name) && options[:id] == :bigint
if [:integer, :bigint].include?(options[:id]) && !options.key?(:default)
options[:default] = nil
end
end
# Since 5.1 PostgreSQL adapter uses bigserial type for primary
# keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
# serial/int type instead -- the way it used to work before 5.1.
unless options.key?(:id)
options[:id] = :integer
end
super
end
def create_join_table(table_1, table_2, column_options: {}, **options)
column_options.reverse_merge!(type: :integer)
super
end
def add_column(table_name, column_name, type, **options)
if type == :primary_key
type = :integer
options[:primary_key] = true
elsif type == :datetime
options[:precision] ||= nil
end
super
end
def add_reference(table_name, ref_name, **options)
super(table_name, ref_name, type: :integer, **options)
end
alias :add_belongs_to :add_reference
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
end
class V4_2 < V5_0
module TableDefinition
def references(*, **options)
options[:index] ||= false
super
end
alias :belongs_to :references
def timestamps(**options)
options[:null] = true if options[:null].nil?
super
end
private
def raise_on_if_exist_options(options)
end
end
def add_reference(table_name, ref_name, **options)
options[:index] ||= false
super
end
alias :add_belongs_to :add_reference
def add_timestamps(table_name, **options)
options[:null] = true if options[:null].nil?
super
end
def index_exists?(table_name, column_name, **options)
column_names = Array(column_name).map(&:to_s)
options[:name] =
if options[:name].present?
options[:name].to_s
else
connection.index_name(table_name, column: column_names)
end
super
end
def remove_index(table_name, column_name = nil, **options)
options[:name] = index_name_for_remove(table_name, column_name, options)
super
end
private
def compatible_table_definition(t)
class << t
prepend TableDefinition
end
super
end
def index_name_for_remove(table_name, column_name, options)
index_name = connection.index_name(table_name, column_name || options)
unless connection.index_name_exists?(table_name, index_name)
if options.key?(:name)
options_without_column = options.except(:column)
index_name_without_column = connection.index_name(table_name, options_without_column)
if connection.index_name_exists?(table_name, index_name_without_column)
return index_name_without_column
end
end
raise ArgumentError, "Index name '#{index_name}' on table '#{table_name}' does not exist"
end
index_name
end
end
end
end
end