Skip to content

Commit

Permalink
Raises when ActiveRecord::Migration is inherited directly.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Dec 29, 2016
1 parent bc6c5df commit 249f71a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 54 deletions.
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Raises when `ActiveRecord::Migration` is inherited directly.

*Rafael Mendonça França*

* Remove deprecated `original_exception` argument in `ActiveRecord::StatementInvalid#initialize`
and `ActiveRecord::StatementInvalid#original_exception`.

Expand Down
5 changes: 4 additions & 1 deletion activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,10 @@ class Current < Migration # :nodoc:
def self.inherited(subclass) # :nodoc:
super
if subclass.superclass == Migration
subclass.include Compatibility::Legacy
raise StandardError, "Directly inheriting from ActiveRecord::Migration is not supported. " \
"Please specify the Rails release the migration was written for:\n" \
"\n" \
" class #{self.class.name} < ActiveRecord::Migration[4.2]"
end
end

Expand Down
62 changes: 21 additions & 41 deletions activerecord/lib/active_record/migration/compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@ def self.find(version)

V5_1 = Current

module FourTwoShared
class V5_0 < V5_1
def create_table(table_name, options = {})
if adapter_name == "PostgreSQL"
if options[:id] == :uuid && !options[:default]
options[:default] = "uuid_generate_v4()"
end
end

# Since 5.1 Postgres 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.
if options[:id].blank?
options[:id] = :integer
options[:auto_increment] = true
end

super
end
end

class V4_2 < V5_0
module TableDefinition
def references(*, **options)
options[:index] ||= false
Expand Down Expand Up @@ -101,46 +121,6 @@ def index_name_for_remove(table_name, options = {})
index_name
end
end

class V5_0 < V5_1
def create_table(table_name, options = {})
if adapter_name == "PostgreSQL"
if options[:id] == :uuid && !options[:default]
options[:default] = "uuid_generate_v4()"
end
end

# Since 5.1 Postgres 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.
if options[:id].blank?
options[:id] = :integer
options[:auto_increment] = true
end

super
end
end

class V4_2 < V5_0
# 4.2 is defined as a module because it needs to be shared with
# Legacy. When the time comes, V5_0 should be defined straight
# in its class.
include FourTwoShared
end

module Legacy
include FourTwoShared

def migrate(*)
ActiveSupport::Deprecation.warn \
"Directly inheriting from ActiveRecord::Migration is deprecated. " \
"Please specify the Rails release the migration was written for:\n" \
"\n" \
" class #{self.class.name} < ActiveRecord::Migration[4.2]"
super
end
end
end
end
end
18 changes: 6 additions & 12 deletions activerecord/test/cases/migration/compatibility_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def migrate(x)
end

def test_references_does_not_add_index_by_default
migration = Class.new(ActiveRecord::Migration) {
migration = Class.new(ActiveRecord::Migration[4.2]) {
def migrate(x)
create_table :more_testings do |t|
t.references :foo
Expand All @@ -73,7 +73,7 @@ def migrate(x)
end

def test_timestamps_have_null_constraints_if_not_present_in_migration_of_create_table
migration = Class.new(ActiveRecord::Migration) {
migration = Class.new(ActiveRecord::Migration[4.2]) {
def migrate(x)
create_table :more_testings do |t|
t.timestamps
Expand All @@ -90,7 +90,7 @@ def migrate(x)
end

def test_timestamps_have_null_constraints_if_not_present_in_migration_for_adding_timestamps_to_existing_table
migration = Class.new(ActiveRecord::Migration) {
migration = Class.new(ActiveRecord::Migration[4.2]) {
def migrate(x)
add_timestamps :testings
end
Expand All @@ -102,15 +102,9 @@ def migrate(x)
assert connection.columns(:testings).find { |c| c.name == "updated_at" }.null
end

def test_legacy_migrations_get_deprecation_warning_when_run
migration = Class.new(ActiveRecord::Migration) {
def up
add_column :testings, :baz, :string
end
}

assert_deprecated do
migration.migrate :up
def test_legacy_migrations_raises_exception_when_inherited
assert_raises(StandardError) do
Class.new(ActiveRecord::Migration)
end
end
end
Expand Down

0 comments on commit 249f71a

Please sign in to comment.