1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add test cases for rake db:prepare

This commit is contained in:
Roberto Miranda 2019-04-02 15:11:09 +01:00
parent 8375b8ee6d
commit 098e4d25f1
3 changed files with 39 additions and 6 deletions

View file

@ -222,13 +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 "Setup database if doesnt exist already and run migrations"
desc "Runs setup if database does not exist, or runs migrations if it does"
task prepare: :load_config do
ActiveRecord::Base.connection
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
begin
ActiveRecord::Base.establish_connection(db_config.config)
db_namespace["migrate"].invoke
rescue ActiveRecord::NoDatabaseError
db_namespace["setup"].invoke
else
db_namespace["migrate"].invoke
end
end
end
desc "Loads the seed data from db/seeds.rb"

View file

@ -553,6 +553,15 @@ 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
end
end
end
end
end

View file

@ -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")
@ -175,6 +190,7 @@ module ApplicationTests
test "db:create and db:drop works on all databases for env" do
require "#{app_path}/config/environment"
ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).each do |db_config|
db_create_and_drop db_config.spec_name, db_config.config["database"]
end
@ -226,6 +242,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