Skip to content

Commit c793cdc

Browse files
committed
Ensure all migration versions use TableDefinition
This is similar to a [previous commit][1] which ensures that versioned migrations always call `super` in `compatible_table_definition`. In this case, these methods are being pulled up to `Current` so that all subclasses will use a `TableDefinition` class and future developers do not have to remember to add all of these methods to new versioned classes when a new one is created. [1]: 16f8bd7
1 parent ec67f71 commit c793cdc

File tree

2 files changed

+33
-77
lines changed

2 files changed

+33
-77
lines changed

activerecord/lib/active_record/migration.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,38 @@ class Migration
559559

560560
# This must be defined before the inherited hook, below
561561
class Current < Migration # :nodoc:
562+
def create_table(table_name, **options)
563+
if block_given?
564+
super { |t| yield compatible_table_definition(t) }
565+
else
566+
super
567+
end
568+
end
569+
570+
def change_table(table_name, **options)
571+
if block_given?
572+
super { |t| yield compatible_table_definition(t) }
573+
else
574+
super
575+
end
576+
end
577+
578+
def create_join_table(table_1, table_2, **options)
579+
if block_given?
580+
super { |t| yield compatible_table_definition(t) }
581+
else
582+
super
583+
end
584+
end
585+
586+
def drop_table(table_name, **options)
587+
if block_given?
588+
super { |t| yield compatible_table_definition(t) }
589+
else
590+
super
591+
end
592+
end
593+
562594
def compatible_table_definition(t)
563595
t
564596
end

activerecord/lib/active_record/migration/compatibility.rb

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,7 @@ def create_table(table_name, **options)
9999
options[:_uses_legacy_table_name] = true
100100
options[:_skip_validate_options] = true
101101

102-
if block_given?
103-
super { |t| yield compatible_table_definition(t) }
104-
else
105-
super
106-
end
107-
end
108-
109-
def change_table(table_name, **options)
110-
if block_given?
111-
super { |t| yield compatible_table_definition(t) }
112-
else
113-
super
114-
end
102+
super
115103
end
116104

117105
def rename_table(table_name, new_name, **options)
@@ -187,22 +175,6 @@ def change_column(table_name, column_name, type, **options)
187175
super
188176
end
189177

190-
def create_table(table_name, **options)
191-
if block_given?
192-
super { |t| yield compatible_table_definition(t) }
193-
else
194-
super
195-
end
196-
end
197-
198-
def change_table(table_name, **options)
199-
if block_given?
200-
super { |t| yield compatible_table_definition(t) }
201-
else
202-
super
203-
end
204-
end
205-
206178
module TableDefinition
207179
def new_column_definition(name, type, **options)
208180
type = PostgreSQLCompat.compatible_timestamp_type(type, @conn)
@@ -257,30 +229,6 @@ def raise_on_if_exist_options(options)
257229
end
258230
end
259231

260-
def create_table(table_name, **options)
261-
if block_given?
262-
super { |t| yield compatible_table_definition(t) }
263-
else
264-
super
265-
end
266-
end
267-
268-
def change_table(table_name, **options)
269-
if block_given?
270-
super { |t| yield compatible_table_definition(t) }
271-
else
272-
super
273-
end
274-
end
275-
276-
def create_join_table(table_1, table_2, **options)
277-
if block_given?
278-
super { |t| yield compatible_table_definition(t) }
279-
else
280-
super
281-
end
282-
end
283-
284232
def add_reference(table_name, ref_name, **options)
285233
if connection.adapter_name == "SQLite"
286234
options[:type] = :integer
@@ -334,30 +282,6 @@ def invert_change_table_comment(args)
334282
end
335283
end
336284

337-
def create_table(table_name, **options)
338-
if block_given?
339-
super { |t| yield compatible_table_definition(t) }
340-
else
341-
super
342-
end
343-
end
344-
345-
def change_table(table_name, **options)
346-
if block_given?
347-
super { |t| yield compatible_table_definition(t) }
348-
else
349-
super
350-
end
351-
end
352-
353-
def create_join_table(table_1, table_2, **options)
354-
if block_given?
355-
super { |t| yield compatible_table_definition(t) }
356-
else
357-
super
358-
end
359-
end
360-
361285
def add_timestamps(table_name, **options)
362286
options[:precision] ||= nil
363287
super

0 commit comments

Comments
 (0)