Skip to content

Commit

Permalink
Merge pull request #45178 from eileencodes/allow-setting-db-with-post…
Browse files Browse the repository at this point in the history
…gres

Allow setting db with use_postgresql tests
  • Loading branch information
eileencodes committed May 26, 2022
1 parent 19118d5 commit e3fc675
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
22 changes: 5 additions & 17 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -672,20 +672,6 @@ def db_fixtures_load(expected_database)
end
end

test "db:prepare setup the database even if schema does not exist" do
Dir.chdir(app_path) do
use_postgresql(multi_db: true) # bug doesn't exist with sqlite3
output = rails("db:drop")
assert_match(/Dropped database/, output)

rails "generate", "model", "recipe", "title:string"
output = rails("db:prepare")
assert_match(/CreateRecipes: migrated/, output)
end
ensure
rails "db:drop" rescue nil
end

test "db:prepare does not touch schema when dumping is disabled" do
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
Expand All @@ -704,13 +690,15 @@ def db_fixtures_load(expected_database)

test "db:prepare creates test database if it does not exist" do
Dir.chdir(app_path) do
use_postgresql
use_postgresql(database_name: "railties_db")
rails "db:drop", "db:create"
rails "runner", "ActiveRecord::Base.connection.drop_database(:railties_test)"
rails "runner", "ActiveRecord::Base.connection.drop_database(:railties_db_test)"

output = rails("db:prepare")
assert_match(%r{Created database 'railties_test'}, output)
assert_match(%r{Created database 'railties_db_test'}, output)
end
ensure
rails "db:drop" rescue nil
end

test "lazily loaded schema cache isn't read when reading the schema migrations table" do
Expand Down
14 changes: 14 additions & 0 deletions railties/test/application/rake/multi_dbs_test.rb
Expand Up @@ -753,6 +753,20 @@ class TwoMigration < ActiveRecord::Migration::Current
db_migrate_and_schema_cache_dump
end

test "db:prepare setup the database even if schema does not exist" do
Dir.chdir(app_path) do
use_postgresql(multi_db: true) # bug doesn't exist with sqlite3
output = rails("db:drop")
assert_match(/Dropped database/, output)

rails "generate", "model", "recipe", "title:string"
output = rails("db:prepare")
assert_match(/CreateRecipes: migrated/, output)
end
ensure
rails "db:drop" rescue nil
end

# Note that schema cache loader depends on the connection and
# does not work for all connections.
test "schema_cache is loaded on primary db in multi-db app" do
Expand Down
10 changes: 5 additions & 5 deletions railties/test/isolation/abstract_unit.rb
Expand Up @@ -458,7 +458,7 @@ def use_frameworks(arr)
$:.reject! { |path| path =~ %r'/(#{to_remove.join('|')})/' }
end

def use_postgresql(multi_db: false)
def use_postgresql(multi_db: false, database_name: "railties_#{Process.pid}")
if multi_db
File.open("#{app_path}/config/database.yml", "w") do |f|
f.puts <<-YAML
Expand All @@ -468,10 +468,10 @@ def use_postgresql(multi_db: false)
development:
primary:
<<: *default
database: railties_test
database: #{database_name}_test
animals:
<<: *default
database: railties_animals_test
database: #{database_name}_animals_test
migrations_paths: db/animals_migrate
YAML
end
Expand All @@ -483,10 +483,10 @@ def use_postgresql(multi_db: false)
pool: 5
development:
<<: *default
database: railties_development
database: #{database_name}_development
test:
<<: *default
database: railties_test
database: #{database_name}_test
YAML
end
end
Expand Down

0 comments on commit e3fc675

Please sign in to comment.