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

Allow db:prepare to load schema if database already exists but is empty; also dumps schema after migrations

This commit is contained in:
Ben Sheldon [he/him] 2022-06-26 08:23:00 -07:00
parent c82a1aaf4c
commit 660fee087d
No known key found for this signature in database
GPG key ID: 862102672FFD9973
3 changed files with 35 additions and 14 deletions

View file

@ -1,3 +1,7 @@
* Update `db:prepare` task to load schema when an uninitialized database exists, and dump schema after migrations.
*Ben Sheldon*
* Fix supporting timezone awareness for `tsrange` and `tstzrange` array columns.
```ruby

View file

@ -192,27 +192,25 @@ module ActiveRecord
ActiveRecord::Base.establish_connection(db_config)
begin
# Skipped when no database
migrate
if ActiveRecord.dump_schema_after_migration
dump_schema(db_config, ActiveRecord.schema_format)
end
database_initialized = ActiveRecord::SchemaMigration.table_exists?
rescue ActiveRecord::NoDatabaseError
create(db_config)
retry
end
unless database_initialized
if File.exist?(schema_dump_path(db_config))
load_schema(
db_config,
ActiveRecord.schema_format,
nil
)
else
migrate
end
seed = true
end
migrate
dump_schema(db_config) if ActiveRecord.dump_schema_after_migration
end
ActiveRecord::Base.establish_connection

View file

@ -687,35 +687,54 @@ module ApplicationTests
end
end
test "db:prepare setup the database" do
test "db:prepare loads schema, runs pending migrations, and updates schema" do
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
output = rails("db:prepare")
assert_match(/CreateBooks: migrated/, output)
assert_match(/create_table "books"/, File.read("db/schema.rb"))
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_no_match(/CreateBooks: migrated/, output) # loaded from schema
assert_match(/CreateRecipes: migrated/, output)
schema = File.read("db/schema.rb")
assert_match(/create_table "books"/, schema)
assert_match(/create_table "recipes"/, schema)
tables = rails("runner", "p ActiveRecord::Base.connection.tables.sort").strip
assert_equal('["ar_internal_metadata", "books", "recipes", "schema_migrations"]', tables)
end
end
test "db:prepare does not touch schema when dumping is disabled" do
test "db:prepare loads schema when database exists but is empty" do
rails "generate", "model", "book", "title:string"
rails("db:prepare", "db:drop", "db:create")
output = rails("db:prepare")
assert_no_match(/CreateBooks: migrated/, output)
tables = rails("runner", "p ActiveRecord::Base.connection.tables.sort").strip
assert_equal('["ar_internal_metadata", "books", "schema_migrations"]', tables)
end
test "db:prepare does not dump schema when dumping is disabled" do
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:create", "db:migrate"
app_file "db/schema.rb", "Not touched"
app_file "db/schema.rb", "# Not touched"
app_file "config/initializers/disable_dumping_schema.rb", <<-RUBY
Rails.application.config.active_record.dump_schema_after_migration = false
RUBY
rails "db:prepare"
assert_equal("Not touched", File.read("db/schema.rb").strip)
assert_equal("# Not touched", File.read("db/schema.rb").strip)
end
end