Skip to content

Commit a2827ec

Browse files
committed
Refactor migration to move migrations paths to connection
Rails has some support for multiple databases but it can be hard to handle migrations with those. The easiest way to implement multiple databases is to contain migrations into their own folder ("db/migrate" for the primary db and "db/seconddb_migrate" for the second db). Without this you would need to write code that allowed you to switch connections in migrations. I can tell you from experience that is not a fun way to implement multiple databases. This refactoring is a pre-requisite for implementing other features related to parallel testing and improved handling for multiple databases. The refactoring here moves the class methods from the `Migrator` class into it's own new class `MigrationContext`. The goal was to move the `migrations_paths` method off of the `Migrator` class and onto the connection. This allows users to do the following in their `database.yml`: ``` development: adapter: mysql2 username: root password: development_seconddb: adapter: mysql2 username: root password: migrations_paths: "db/second_db_migrate" ``` Migrations for the `seconddb` can now be store in the `db/second_db_migrate` directory. Migrations for the primary database are stored in `db/migrate`". The refactoring here drastically reduces the internal API for migrations since we don't need to pass `migrations_paths` around to every single method. Additionally this change does not require any Rails applications to make changes unless they want to use the new public API. All of the class methods from the `Migrator` class were `nodoc`'d except for the `migrations_paths` and `migrations_path` getter/setters respectively.
1 parent 198e3e3 commit a2827ec

File tree

19 files changed

+417
-308
lines changed

19 files changed

+417
-308
lines changed

activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1049,8 +1049,8 @@ def assume_migrated_upto_version(version, migrations_paths)
10491049
sm_table = quote_table_name(ActiveRecord::SchemaMigration.table_name)
10501050

10511051
migrated = ActiveRecord::SchemaMigration.all_versions.map(&:to_i)
1052-
versions = ActiveRecord::Migrator.migration_files(migrations_paths).map do |file|
1053-
ActiveRecord::Migrator.parse_migration_filename(file).first.to_i
1052+
versions = migration_context.migration_files.map do |file|
1053+
migration_context.parse_migration_filename(file).first.to_i
10541054
end
10551055

10561056
unless migrated.include?(version)

activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

+8
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ def initialize(connection, logger = nil, config = {}) # :nodoc:
119119
end
120120
end
121121

122+
def migrations_paths
123+
@config[:migrations_paths] || Migrator.migrations_paths
124+
end
125+
126+
def migration_context
127+
MigrationContext.new(migrations_paths)
128+
end
129+
122130
class Version
123131
include Comparable
124132

activerecord/lib/active_record/connection_adapters/schema_cache.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def encode_with(coder)
2828
coder["columns_hash"] = @columns_hash
2929
coder["primary_keys"] = @primary_keys
3030
coder["data_sources"] = @data_sources
31-
coder["version"] = ActiveRecord::Migrator.current_version
31+
coder["version"] = connection.migration_context.current_version
3232
end
3333

3434
def init_with(coder)
@@ -100,7 +100,7 @@ def clear_data_source_cache!(name)
100100

101101
def marshal_dump
102102
# if we get current version during initialization, it happens stack over flow.
103-
@version = ActiveRecord::Migrator.current_version
103+
@version = connection.migration_context.current_version
104104
[@version, @columns, @columns_hash, @primary_keys, @data_sources]
105105
end
106106

0 commit comments

Comments
 (0)