aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authoreileencodes <eileencodes@gmail.com>2018-01-10 10:25:13 -0500
committereileencodes <eileencodes@gmail.com>2018-01-18 08:55:03 -0500
commita2827ec9811b5012e8e366011fd44c8eb53fc714 (patch)
treece158384df40f9cfe128bb659b0372a6e7bb8f07 /railties
parent198e3e3cb784dae54d8d4346667d3fb08c889335 (diff)
downloadrails-a2827ec9811b5012e8e366011fd44c8eb53fc714.tar.gz
rails-a2827ec9811b5012e8e366011fd44c8eb53fc714.tar.bz2
rails-a2827ec9811b5012e8e366011fd44c8eb53fc714.zip
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.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/info.rb2
-rw-r--r--railties/test/application/rake/migrations_test.rb12
-rw-r--r--railties/test/railties/engine_test.rb2
3 files changed, 14 insertions, 2 deletions
diff --git a/railties/lib/rails/info.rb b/railties/lib/rails/info.rb
index d8f361f524..d5c9973c6b 100644
--- a/railties/lib/rails/info.rb
+++ b/railties/lib/rails/info.rb
@@ -99,7 +99,7 @@ module Rails
end
property "Database schema version" do
- ActiveRecord::Migrator.current_version rescue nil
+ ActiveRecord::Base.connection.migration_context.current_version rescue nil
end
end
end
diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb
index 788f9160d6..1b45e0645c 100644
--- a/railties/test/application/rake/migrations_test.rb
+++ b/railties/test/application/rake/migrations_test.rb
@@ -37,6 +37,18 @@ module ApplicationTests
assert_match(/AMigration: reverted/, output)
end
+ test "version outputs current version" do
+ app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
+ class OneMigration < ActiveRecord::Migration::Current
+ end
+ MIGRATION
+
+ rails "db:migrate"
+
+ output = rails("db:version")
+ assert_match(/Current version: 1/, output)
+ end
+
test "migrate with specified VERSION in different formats" do
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
diff --git a/railties/test/railties/engine_test.rb b/railties/test/railties/engine_test.rb
index 339a56c34f..a59c63f343 100644
--- a/railties/test/railties/engine_test.rb
+++ b/railties/test/railties/engine_test.rb
@@ -34,7 +34,7 @@ module RailtiesTest
def migrations
migration_root = File.expand_path(ActiveRecord::Migrator.migrations_paths.first, app_path)
- ActiveRecord::Migrator.migrations(migration_root)
+ ActiveRecord::MigrationContext.new(migration_root).migrations
end
test "serving sprocket's assets" do