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

Add db:prepare rake task. #35768

Merged
merged 6 commits into from Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/railties/databases.rake
Expand Up @@ -222,6 +222,16 @@ db_namespace = namespace :db do
desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]

desc "Runs setup if database does not exist, or runs migrations if it does"
task prepare: :load_config do
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
ActiveRecord::Base.establish_connection(db_config.config)
db_namespace["migrate"].invoke
rescue ActiveRecord::NoDatabaseError
db_namespace["setup"].invoke
end
end

desc "Loads the seed data from db/seeds.rb"
task seed: :load_config do
db_namespace["abort_if_pending_migrations"].invoke
Expand Down
9 changes: 9 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -553,6 +553,15 @@ def db_test_load_structure
end
end
end

test "db:prepare setup the database" do
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
output = rails("db:prepare")

assert_match(/CreateBooks: migrated/, output)
end
end
end
end
end
20 changes: 20 additions & 0 deletions railties/test/application/rake/multi_dbs_test.rb
Expand Up @@ -137,6 +137,21 @@ def db_migrate_status_namespaced(namespace)
end
end

def db_prepare
Dir.chdir(app_path) do
generate_models_for_animals
output = rails("db:prepare")

ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
if db_config.spec_name == "primary"
assert_match(/CreateBooks: migrated/, output)
else
assert_match(/CreateDogs: migrated/, output)
end
end
end
end

def write_models_for_animals
# make a directory for the animals migration
FileUtils.mkdir_p("#{app_path}/db/animals_migrate")
Expand Down Expand Up @@ -226,6 +241,11 @@ def generate_models_for_animals
require "#{app_path}/config/environment"
db_migrate_and_schema_cache_dump_and_schema_cache_clear
end

test "db:prepare works on all databases" do
require "#{app_path}/config/environment"
db_prepare
end
end
end
end