Skip to content

Commit 249f71a

Browse files
committed
Raises when ActiveRecord::Migration is inherited directly.
1 parent bc6c5df commit 249f71a

File tree

4 files changed

+35
-54
lines changed

4 files changed

+35
-54
lines changed

Diff for: activerecord/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Raises when `ActiveRecord::Migration` is inherited directly.
2+
3+
*Rafael Mendonça França*
4+
15
* Remove deprecated `original_exception` argument in `ActiveRecord::StatementInvalid#initialize`
26
and `ActiveRecord::StatementInvalid#original_exception`.
37

Diff for: activerecord/lib/active_record/migration.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,10 @@ class Current < Migration # :nodoc:
522522
def self.inherited(subclass) # :nodoc:
523523
super
524524
if subclass.superclass == Migration
525-
subclass.include Compatibility::Legacy
525+
raise StandardError, "Directly inheriting from ActiveRecord::Migration is not supported. " \
526+
"Please specify the Rails release the migration was written for:\n" \
527+
"\n" \
528+
" class #{self.class.name} < ActiveRecord::Migration[4.2]"
526529
end
527530
end
528531

Diff for: activerecord/lib/active_record/migration/compatibility.rb

+21-41
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,27 @@ def self.find(version)
1313

1414
V5_1 = Current
1515

16-
module FourTwoShared
16+
class V5_0 < V5_1
17+
def create_table(table_name, options = {})
18+
if adapter_name == "PostgreSQL"
19+
if options[:id] == :uuid && !options[:default]
20+
options[:default] = "uuid_generate_v4()"
21+
end
22+
end
23+
24+
# Since 5.1 Postgres adapter uses bigserial type for primary
25+
# keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
26+
# serial/int type instead -- the way it used to work before 5.1.
27+
if options[:id].blank?
28+
options[:id] = :integer
29+
options[:auto_increment] = true
30+
end
31+
32+
super
33+
end
34+
end
35+
36+
class V4_2 < V5_0
1737
module TableDefinition
1838
def references(*, **options)
1939
options[:index] ||= false
@@ -101,46 +121,6 @@ def index_name_for_remove(table_name, options = {})
101121
index_name
102122
end
103123
end
104-
105-
class V5_0 < V5_1
106-
def create_table(table_name, options = {})
107-
if adapter_name == "PostgreSQL"
108-
if options[:id] == :uuid && !options[:default]
109-
options[:default] = "uuid_generate_v4()"
110-
end
111-
end
112-
113-
# Since 5.1 Postgres adapter uses bigserial type for primary
114-
# keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
115-
# serial/int type instead -- the way it used to work before 5.1.
116-
if options[:id].blank?
117-
options[:id] = :integer
118-
options[:auto_increment] = true
119-
end
120-
121-
super
122-
end
123-
end
124-
125-
class V4_2 < V5_0
126-
# 4.2 is defined as a module because it needs to be shared with
127-
# Legacy. When the time comes, V5_0 should be defined straight
128-
# in its class.
129-
include FourTwoShared
130-
end
131-
132-
module Legacy
133-
include FourTwoShared
134-
135-
def migrate(*)
136-
ActiveSupport::Deprecation.warn \
137-
"Directly inheriting from ActiveRecord::Migration is deprecated. " \
138-
"Please specify the Rails release the migration was written for:\n" \
139-
"\n" \
140-
" class #{self.class.name} < ActiveRecord::Migration[4.2]"
141-
super
142-
end
143-
end
144124
end
145125
end
146126
end

Diff for: activerecord/test/cases/migration/compatibility_test.rb

+6-12
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def migrate(x)
5555
end
5656

5757
def test_references_does_not_add_index_by_default
58-
migration = Class.new(ActiveRecord::Migration) {
58+
migration = Class.new(ActiveRecord::Migration[4.2]) {
5959
def migrate(x)
6060
create_table :more_testings do |t|
6161
t.references :foo
@@ -73,7 +73,7 @@ def migrate(x)
7373
end
7474

7575
def test_timestamps_have_null_constraints_if_not_present_in_migration_of_create_table
76-
migration = Class.new(ActiveRecord::Migration) {
76+
migration = Class.new(ActiveRecord::Migration[4.2]) {
7777
def migrate(x)
7878
create_table :more_testings do |t|
7979
t.timestamps
@@ -90,7 +90,7 @@ def migrate(x)
9090
end
9191

9292
def test_timestamps_have_null_constraints_if_not_present_in_migration_for_adding_timestamps_to_existing_table
93-
migration = Class.new(ActiveRecord::Migration) {
93+
migration = Class.new(ActiveRecord::Migration[4.2]) {
9494
def migrate(x)
9595
add_timestamps :testings
9696
end
@@ -102,15 +102,9 @@ def migrate(x)
102102
assert connection.columns(:testings).find { |c| c.name == "updated_at" }.null
103103
end
104104

105-
def test_legacy_migrations_get_deprecation_warning_when_run
106-
migration = Class.new(ActiveRecord::Migration) {
107-
def up
108-
add_column :testings, :baz, :string
109-
end
110-
}
111-
112-
assert_deprecated do
113-
migration.migrate :up
105+
def test_legacy_migrations_raises_exception_when_inherited
106+
assert_raises(StandardError) do
107+
Class.new(ActiveRecord::Migration)
114108
end
115109
end
116110
end

0 commit comments

Comments
 (0)