Fix `db:prepare` task to create/migrate test db when dev db already exists

Fixes #44815.
This commit is contained in:
fatkodima 2022-04-01 14:29:01 +03:00
parent b97c308f91
commit dbefed5019
3 changed files with 37 additions and 24 deletions

View File

@ -186,35 +186,36 @@ module ActiveRecord
end
def prepare_all
seed = false
configs_for(env_name: env).each do |db_config|
each_current_configuration(env) do |db_config|
seed = false
ActiveRecord::Base.establish_connection(db_config)
# Skipped when no database
migrate
if ActiveRecord.dump_schema_after_migration
dump_schema(db_config, ActiveRecord.schema_format)
end
rescue ActiveRecord::NoDatabaseError
create_current(db_config.env_name, db_config.name)
if File.exist?(schema_dump_path(db_config))
load_schema(
db_config,
ActiveRecord.schema_format,
nil
)
else
begin
# Skipped when no database
migrate
if ActiveRecord.dump_schema_after_migration
dump_schema(db_config, ActiveRecord.schema_format)
end
rescue ActiveRecord::NoDatabaseError
create(db_config)
if File.exist?(schema_dump_path(db_config))
load_schema(
db_config,
ActiveRecord.schema_format,
nil
)
else
migrate
end
seed = true
end
seed = true
ActiveRecord::Base.establish_connection
load_seed if seed
end
ActiveRecord::Base.establish_connection
load_seed if seed
end
def drop(configuration, *arguments)

View File

@ -702,6 +702,17 @@ module ApplicationTests
end
end
test "db:prepare creates test database if it does not exist" do
Dir.chdir(app_path) do
use_postgresql
rails "db:drop", "db:create"
rails "runner", "ActiveRecord::Base.connection.drop_database(:railties_test)"
output = rails("db:prepare")
assert_match(%r{Created database 'railties_test'}, output)
end
end
test "lazily loaded schema cache isn't read when reading the schema migrations table" do
Dir.chdir(app_path) do
app_file "config/initializers/lazy_load_schema_cache.rb", <<-RUBY

View File

@ -487,11 +487,12 @@ module TestHelpers
default: &default
adapter: postgresql
pool: 5
database: railties_test
development:
<<: *default
database: railties_development
test:
<<: *default
database: railties_test
YAML
end
end