mirror of https://github.com/rails/rails
Merge pull request #35768 from robertomiranda/r/rake-db-prepare
Add db:prepare rake task.
This commit is contained in:
commit
2c4dab11d1
|
@ -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
|
||||
|
|
|
@ -553,6 +553,22 @@ module ApplicationTests
|
|||
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)
|
||||
|
||||
output = rails("db:drop")
|
||||
assert_match(/Dropped database/, output)
|
||||
|
||||
rails "generate", "model", "recipe", "title:string"
|
||||
output = rails("db:prepare")
|
||||
assert_match(/CreateBooks: migrated/, output)
|
||||
assert_match(/CreateRecipes: migrated/, output)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -137,6 +137,21 @@ module ApplicationTests
|
|||
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")
|
||||
|
@ -226,6 +241,11 @@ module ApplicationTests
|
|||
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
|
||||
|
|
Loading…
Reference in New Issue