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

@ -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
rescue ActiveRecord::NoDatabaseError
db_namespace["setup"].invoke
else
db_namespace["migrate"].invoke
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
end
end
end
desc "Loads the seed data from db/seeds.rb"

@ -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

@ -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")
@ -175,6 +190,7 @@ def generate_models_for_animals
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 @@ 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