Skip to content

Commit

Permalink
Merge pull request rails#36439 from eileencodes/move-schema-migration…
Browse files Browse the repository at this point in the history
…-to-migration-context

Move SchemaMigration to migration_context
  • Loading branch information
eileencodes authored Jun 14, 2019
2 parents c284771 + 7cc27d7 commit f813119
Show file tree
Hide file tree
Showing 17 changed files with 412 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,8 @@ def foreign_key_options(from_table, to_table, options) # :nodoc:
options
end

def dump_schema_information #:nodoc:
versions = ActiveRecord::SchemaMigration.all_versions
def dump_schema_information # :nodoc:
versions = schema_migration.all_versions
insert_versions_sql(versions) if versions.any?
end

Expand All @@ -1081,7 +1081,7 @@ def assume_migrated_upto_version(version, migrations_paths = nil)
end

version = version.to_i
sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
sm_table = quote_table_name(schema_migration.table_name)

migrated = migration_context.get_all_versions
versions = migration_context.migrations.map(&:version)
Expand Down Expand Up @@ -1454,7 +1454,7 @@ def remove_columns_for_alter(table_name, *column_names)
end

def insert_versions_sql(versions)
sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
sm_table = quote_table_name(schema_migration.table_name)

if versions.is_a?(Array)
sql = +"INSERT INTO #{sm_table} (version) VALUES\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,22 @@ def migrations_paths # :nodoc:
end

def migration_context # :nodoc:
MigrationContext.new(migrations_paths)
MigrationContext.new(migrations_paths, schema_migration)
end

def schema_migration # :nodoc:
@schema_migration ||= begin
conn = self
spec_name = conn.pool.spec.name
name = "#{spec_name}::SchemaMigration"

Class.new(ActiveRecord::SchemaMigration) do
define_singleton_method(:name) { name }
define_singleton_method(:to_s) { name }

self.connection_specification_name = spec_name
end
end
end

class Version
Expand Down
43 changes: 23 additions & 20 deletions activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -884,13 +884,14 @@ def method_missing(method, *arguments, &block)

def copy(destination, sources, options = {})
copied = []
schema_migration = options[:schema_migration] || ActiveRecord::SchemaMigration

FileUtils.mkdir_p(destination) unless File.exist?(destination)

destination_migrations = ActiveRecord::MigrationContext.new(destination).migrations
destination_migrations = ActiveRecord::MigrationContext.new(destination, schema_migration).migrations
last = destination_migrations.last
sources.each do |scope, path|
source_migrations = ActiveRecord::MigrationContext.new(path).migrations
source_migrations = ActiveRecord::MigrationContext.new(path, schema_migration).migrations

source_migrations.each do |migration|
source = File.binread(migration.filename)
Expand Down Expand Up @@ -1012,10 +1013,11 @@ def mtime
end

class MigrationContext #:nodoc:
attr_reader :migrations_paths
attr_reader :migrations_paths, :schema_migration

def initialize(migrations_paths)
def initialize(migrations_paths, schema_migration)
@migrations_paths = migrations_paths
@schema_migration = schema_migration
end

def migrate(target_version = nil, &block)
Expand Down Expand Up @@ -1046,7 +1048,7 @@ def up(target_version = nil)
migrations
end

Migrator.new(:up, selected_migrations, target_version).migrate
Migrator.new(:up, selected_migrations, schema_migration, target_version).migrate
end

def down(target_version = nil)
Expand All @@ -1056,20 +1058,20 @@ def down(target_version = nil)
migrations
end

Migrator.new(:down, selected_migrations, target_version).migrate
Migrator.new(:down, selected_migrations, schema_migration, target_version).migrate
end

def run(direction, target_version)
Migrator.new(direction, migrations, target_version).run
Migrator.new(direction, migrations, schema_migration, target_version).run
end

def open
Migrator.new(:up, migrations, nil)
Migrator.new(:up, migrations, schema_migration)
end

def get_all_versions
if SchemaMigration.table_exists?
SchemaMigration.all_versions.map(&:to_i)
if schema_migration.table_exists?
schema_migration.all_versions
else
[]
end
Expand Down Expand Up @@ -1106,12 +1108,12 @@ def migrations
end

def migrations_status
db_list = ActiveRecord::SchemaMigration.normalized_versions
db_list = schema_migration.normalized_versions

file_list = migration_files.map do |file|
version, name, scope = parse_migration_filename(file)
raise IllegalMigrationNameError.new(file) unless version
version = ActiveRecord::SchemaMigration.normalize_migration_number(version)
version = schema_migration.normalize_migration_number(version)
status = db_list.delete(version) ? "up" : "down"
[status, version, (name + scope).humanize]
end.compact
Expand Down Expand Up @@ -1151,7 +1153,7 @@ def parse_migration_filename(filename)
end

def move(direction, steps)
migrator = Migrator.new(direction, migrations)
migrator = Migrator.new(direction, migrations, schema_migration)

if current_version != 0 && !migrator.current_migration
raise UnknownMigrationVersionError.new(current_version)
Expand All @@ -1170,27 +1172,28 @@ def move(direction, steps)
end
end

class Migrator #:nodoc:
class Migrator # :nodoc:
class << self
attr_accessor :migrations_paths

# For cases where a table doesn't exist like loading from schema cache
def current_version
MigrationContext.new(migrations_paths).current_version
MigrationContext.new(migrations_paths, SchemaMigration).current_version
end
end

self.migrations_paths = ["db/migrate"]

def initialize(direction, migrations, target_version = nil)
def initialize(direction, migrations, schema_migration, target_version = nil)
@direction = direction
@target_version = target_version
@migrated_versions = nil
@migrations = migrations
@schema_migration = schema_migration

validate(@migrations)

ActiveRecord::SchemaMigration.create_table
@schema_migration.create_table
ActiveRecord::InternalMetadata.create_table
end

Expand Down Expand Up @@ -1244,7 +1247,7 @@ def migrated
end

def load_migrated
@migrated_versions = Set.new(Base.connection.migration_context.get_all_versions)
@migrated_versions = Set.new(@schema_migration.all_versions)
end

private
Expand Down Expand Up @@ -1327,10 +1330,10 @@ def validate(migrations)
def record_version_state_after_migrating(version)
if down?
migrated.delete(version)
ActiveRecord::SchemaMigration.delete_by(version: version.to_s)
@schema_migration.delete_by(version: version.to_s)
else
migrated << version
ActiveRecord::SchemaMigration.create!(version: version.to_s)
@schema_migration.create!(version: version.to_s)
end
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def define(info, &block) # :nodoc:
instance_eval(&block)

if info[:version].present?
ActiveRecord::SchemaMigration.create_table
connection.schema_migration.create_table
connection.assume_migrated_upto_version(info[:version])
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/schema_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def normalized_versions
end

def all_versions
order(:version).pluck(:version)
order(:version).pluck(:version).map(&:to_i)
end
end

Expand Down
12 changes: 7 additions & 5 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,10 @@ def drop_current(environment = env)

def truncate_tables(configuration)
ActiveRecord::Base.connected_to(database: { truncation: configuration }) do
table_names = ActiveRecord::Base.connection.tables
conn = ActiveRecord::Base.connection
table_names = conn.tables
table_names -= [
SchemaMigration.table_name,
conn.schema_migration.table_name,
InternalMetadata.table_name
]

Expand Down Expand Up @@ -233,7 +234,7 @@ def migrate
end

def migrate_status
unless ActiveRecord::SchemaMigration.table_exists?
unless ActiveRecord::Base.connection.schema_migration.table_exists?
Kernel.abort "Schema migrations table does not exist yet."
end

Expand Down Expand Up @@ -328,6 +329,7 @@ def load_schema(configuration, format = ActiveRecord::Base.schema_format, file =
def dump_schema(configuration, format = ActiveRecord::Base.schema_format, spec_name = "primary") # :nodoc:
require "active_record/schema_dumper"
filename = dump_filename(spec_name, format)
connection = ActiveRecord::Base.connection

case format
when :ruby
Expand All @@ -336,9 +338,9 @@ def dump_schema(configuration, format = ActiveRecord::Base.schema_format, spec_n
end
when :sql
structure_dump(configuration, filename)
if ActiveRecord::SchemaMigration.table_exists?
if connection.schema_migration.table_exists?
File.open(filename, "a") do |f|
f.puts ActiveRecord::Base.connection.dump_schema_information
f.puts connection.dump_schema_information
f.print "\n"
end
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/adapters/mysql2/table_options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def migrate(x)
end
end.new

ActiveRecord::Migrator.new(:up, [migration]).migrate
ActiveRecord::Migrator.new(:up, [migration], ActiveRecord::Base.connection.schema_migration).migrate

output = dump_table_schema("mysql_table_options")
options = %r{create_table "mysql_table_options", options: "(?<options>.*)"}.match(output)[:options]
Expand Down Expand Up @@ -112,7 +112,7 @@ def migrate(x)
end
end.new

ActiveRecord::Migrator.new(:up, [migration]).migrate
ActiveRecord::Migrator.new(:up, [migration], ActiveRecord::Base.connection.schema_migration).migrate

assert_match %r{ENGINE=InnoDB}, @log.string
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ def test_enable_extension_migration_ignores_prefix_and_suffix
@connection.disable_extension("hstore")

migrations = [EnableHstore.new(nil, 1)]
ActiveRecord::Migrator.new(:up, migrations).migrate
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::Base.connection.schema_migration).migrate
assert @connection.extension_enabled?("hstore"), "extension hstore should be enabled"
end

def test_disable_extension_migration_ignores_prefix_and_suffix
@connection.enable_extension("hstore")

migrations = [DisableHstore.new(nil, 1)]
ActiveRecord::Migrator.new(:up, migrations).migrate
ActiveRecord::Migrator.new(:up, migrations, ActiveRecord::Base.connection.schema_migration).migrate
assert_not @connection.extension_enabled?("hstore"), "extension hstore should not be enabled"
end
end
8 changes: 4 additions & 4 deletions activerecord/test/cases/adapters/postgresql/uuid_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,14 @@ def migrate(x)
create_table("pg_uuids_4", id: :uuid)
end
end.new
ActiveRecord::Migrator.new(:up, [migration]).migrate
ActiveRecord::Migrator.new(:up, [migration], ActiveRecord::Base.connection.schema_migration).migrate

schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: -> { "uuid_generate_v4\(\)" }/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::SchemaMigration.delete_all
ActiveRecord::Base.connection.schema_migration.delete_all
end
uses_transaction :test_schema_dumper_for_uuid_primary_key_default_in_legacy_migration
end
Expand Down Expand Up @@ -343,14 +343,14 @@ def migrate(x)
create_table("pg_uuids_4", id: :uuid, default: nil)
end
end.new
ActiveRecord::Migrator.new(:up, [migration]).migrate
ActiveRecord::Migrator.new(:up, [migration], ActiveRecord::Base.connection.schema_migration).migrate

schema = dump_table_schema "pg_uuids_4"
assert_match(/\bcreate_table "pg_uuids_4", id: :uuid, default: nil/, schema)
ensure
drop_table "pg_uuids_4"
ActiveRecord::Migration.verbose = @verbose_was
ActiveRecord::SchemaMigration.delete_all
ActiveRecord::Base.connection.schema_migration.delete_all
end
uses_transaction :test_schema_dumper_for_uuid_primary_key_with_default_nil_in_legacy_migration
end
Expand Down
27 changes: 14 additions & 13 deletions activerecord/test/cases/ar_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
@original_verbose = ActiveRecord::Migration.verbose
ActiveRecord::Migration.verbose = false
@connection = ActiveRecord::Base.connection
ActiveRecord::SchemaMigration.drop_table
@schema_migration = @connection.schema_migration
@schema_migration.drop_table
end

teardown do
Expand All @@ -18,21 +19,21 @@ class ActiveRecordSchemaTest < ActiveRecord::TestCase
@connection.drop_table :nep_schema_migrations rescue nil
@connection.drop_table :has_timestamps rescue nil
@connection.drop_table :multiple_indexes rescue nil
ActiveRecord::SchemaMigration.delete_all rescue nil
@schema_migration.delete_all rescue nil
ActiveRecord::Migration.verbose = @original_verbose
end

def test_has_primary_key
old_primary_key_prefix_type = ActiveRecord::Base.primary_key_prefix_type
ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore
assert_equal "version", ActiveRecord::SchemaMigration.primary_key
assert_equal "version", @schema_migration.primary_key

ActiveRecord::SchemaMigration.create_table
assert_difference "ActiveRecord::SchemaMigration.count", 1 do
ActiveRecord::SchemaMigration.create version: 12
@schema_migration.create_table
assert_difference "@schema_migration.count", 1 do
@schema_migration.create version: 12
end
ensure
ActiveRecord::SchemaMigration.drop_table
@schema_migration.drop_table
ActiveRecord::Base.primary_key_prefix_type = old_primary_key_prefix_type
end

Expand All @@ -54,7 +55,7 @@ def test_schema_define
def test_schema_define_with_table_name_prefix
old_table_name_prefix = ActiveRecord::Base.table_name_prefix
ActiveRecord::Base.table_name_prefix = "nep_"
ActiveRecord::SchemaMigration.reset_table_name
@schema_migration.reset_table_name
ActiveRecord::InternalMetadata.reset_table_name
ActiveRecord::Schema.define(version: 7) do
create_table :fruits do |t|
Expand All @@ -67,7 +68,7 @@ def test_schema_define_with_table_name_prefix
assert_equal 7, @connection.migration_context.current_version
ensure
ActiveRecord::Base.table_name_prefix = old_table_name_prefix
ActiveRecord::SchemaMigration.reset_table_name
@schema_migration.reset_table_name
ActiveRecord::InternalMetadata.reset_table_name
end

Expand All @@ -89,10 +90,10 @@ def test_schema_subclass
end

def test_normalize_version
assert_equal "118", ActiveRecord::SchemaMigration.normalize_migration_number("0000118")
assert_equal "002", ActiveRecord::SchemaMigration.normalize_migration_number("2")
assert_equal "017", ActiveRecord::SchemaMigration.normalize_migration_number("0017")
assert_equal "20131219224947", ActiveRecord::SchemaMigration.normalize_migration_number("20131219224947")
assert_equal "118", @schema_migration.normalize_migration_number("0000118")
assert_equal "002", @schema_migration.normalize_migration_number("2")
assert_equal "017", @schema_migration.normalize_migration_number("0017")
assert_equal "20131219224947", @schema_migration.normalize_migration_number("20131219224947")
end

def test_schema_load_with_multiple_indexes_for_column_of_different_names
Expand Down
Loading

0 comments on commit f813119

Please sign in to comment.