Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DATABASE option for railties:install:migrations #48579

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ end

namespace :railties do
namespace :install do
# desc "Copy missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
# desc "Copy missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2 and database to copy to with DATABASE=database."
task migrations: :'db:load_config' do
to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
railties = {}
Expand All @@ -628,7 +628,16 @@ namespace :railties do
puts "Copied migration #{migration.basename} from #{name}"
end

ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
if ENV["DATABASE"].present? && ENV["DATABASE"] != "primary"
config = ActiveRecord::Base.configurations.configs_for(name: ENV["DATABASE"])
raise "Invalid DATABASE provided" if config.blank?
destination = config.migrations_paths
raise "#{ENV["DATABASE"]} does not have a custom migration path" if destination.blank?
else
destination = ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first
end

ActiveRecord::Migration.copy(destination, railties,
on_skip: on_skip, on_copy: on_copy)
end
end
Expand Down
12 changes: 12 additions & 0 deletions guides/source/engines.md
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,18 @@ If you have multiple engines that need migrations copied over, use
$ bin/rails railties:install:migrations
```

You can specify a custom path in the source engine for the migrations by specifying MIGRATIONS_PATH.

```bash
$ bin/rails railties:install:migrations MIGRATIONS_PATH=db_blourgh
```

If you have multiple databases you can also specify the target database by specifying DATABASE.

```bash
$ bin/rails railties:install:migrations DATABASE=animals
```

This command, when run for the first time, will copy over all the migrations
from the engine. When run the next time, it will only copy over migrations that
haven't been copied over already. The first run for this command will output
Expand Down
11 changes: 11 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
* Add `DATABASE` option to `railties:install:migrations`

This allows you to specify which database the migrations should be copied to
when running `rails railties:install:migrations`.

```bash
$ rails railties:install:migrations DATABASE=animals
```

*Matthew Hirst*

* The new method `config.autoload_lib(ignore:)` provides a simple way to
autoload from `lib`:

Expand Down
64 changes: 61 additions & 3 deletions railties/test/railties/engine_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class EngineTest < ActiveSupport::TestCase
include Rack::Test::Methods

def setup
build_app
build_app({ multi_db: true })

@plugin = engine "bukkits" do |plugin|
plugin.write "lib/bukkits.rb", <<-RUBY
Expand All @@ -32,8 +32,15 @@ def boot_rails
require "#{app_path}/config/environment"
end

def migrations
migration_root = File.expand_path(ActiveRecord::Migrator.migrations_paths.first, app_path)
def migrations(database = nil)
migration_path = if database
config = ActiveRecord::Base.configurations.configs_for(name: database)
config.migrations_paths
else
ActiveRecord::Migrator.migrations_paths.first
end

migration_root = File.expand_path(migration_path, app_path)
sm = ActiveRecord::SchemaMigration::NullSchemaMigration.new
im = ActiveRecord::InternalMetadata::NullInternalMetadata.new
ActiveRecord::MigrationContext.new(migration_root, sm, im).migrations
Expand Down Expand Up @@ -117,6 +124,57 @@ def up
end
end

test "copying migrations to specific database" do
@plugin.write "db/migrate/1_create_users.rb", <<-RUBY
class CreateUsers < ActiveRecord::Migration::Current
end
RUBY

@plugin.write "db/migrate/2_add_last_name_to_users.rb", <<-RUBY
class AddLastNameToUsers < ActiveRecord::Migration::Current
end
RUBY

@plugin.write "db/migrate/3_create_sessions.rb", <<-RUBY
class CreateSessions < ActiveRecord::Migration::Current
end
RUBY

app_file "db/animals_migrate/1_create_sessions.rb", <<-RUBY
class CreateSessions < ActiveRecord::Migration::Current
def up
end
end
RUBY

restrict_frameworks
boot_rails

Dir.chdir(app_path) do
output = `bundle exec rake bukkits:install:migrations DATABASE=animals`

["CreateUsers", "AddLastNameToUsers", "CreateSessions"].each do |migration_name|
assert migrations("animals").detect { |migration| migration.name == migration_name }
end
assert_match(/Copied migration \d+_create_users\.bukkits\.rb from bukkits/, output)
assert_match(/Copied migration \d+_add_last_name_to_users\.bukkits\.rb from bukkits/, output)
assert_match(/NOTE: Migration \d+_create_sessions\.rb from bukkits has been skipped/, output)

migrations_count = Dir["#{app_path}/db/animals_migrate/*.rb"].length

assert_equal migrations("animals").length, migrations_count

output = `bundle exec rake railties:install:migrations DATABASE=animals`.split("\n")

assert_equal migrations_count, Dir["#{app_path}/db/animals_migrate/*.rb"].length

assert_no_match(/\d+_create_users/, output.join("\n"))

bukkits_migration_order = output.index(output.detect { |o| /NOTE: Migration \d+_create_sessions\.rb from bukkits has been skipped/ =~ o })
assert_not_nil bukkits_migration_order, "Expected migration to be skipped"
end
end

test "respects the order of railties when installing migrations" do
@blog = engine "blog" do |plugin|
plugin.write "lib/blog.rb", <<-RUBY
Expand Down