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:
parent
c82a1aaf4c
commit
660fee087d
3 changed files with 35 additions and 14 deletions
|
@ -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.
|
* Fix supporting timezone awareness for `tsrange` and `tstzrange` array columns.
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
|
|
|
@ -192,27 +192,25 @@ module ActiveRecord
|
||||||
ActiveRecord::Base.establish_connection(db_config)
|
ActiveRecord::Base.establish_connection(db_config)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
# Skipped when no database
|
database_initialized = ActiveRecord::SchemaMigration.table_exists?
|
||||||
migrate
|
|
||||||
|
|
||||||
if ActiveRecord.dump_schema_after_migration
|
|
||||||
dump_schema(db_config, ActiveRecord.schema_format)
|
|
||||||
end
|
|
||||||
rescue ActiveRecord::NoDatabaseError
|
rescue ActiveRecord::NoDatabaseError
|
||||||
create(db_config)
|
create(db_config)
|
||||||
|
retry
|
||||||
|
end
|
||||||
|
|
||||||
|
unless database_initialized
|
||||||
if File.exist?(schema_dump_path(db_config))
|
if File.exist?(schema_dump_path(db_config))
|
||||||
load_schema(
|
load_schema(
|
||||||
db_config,
|
db_config,
|
||||||
ActiveRecord.schema_format,
|
ActiveRecord.schema_format,
|
||||||
nil
|
nil
|
||||||
)
|
)
|
||||||
else
|
|
||||||
migrate
|
|
||||||
end
|
end
|
||||||
|
|
||||||
seed = true
|
seed = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
migrate
|
||||||
|
dump_schema(db_config) if ActiveRecord.dump_schema_after_migration
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveRecord::Base.establish_connection
|
ActiveRecord::Base.establish_connection
|
||||||
|
|
|
@ -687,35 +687,54 @@ module ApplicationTests
|
||||||
end
|
end
|
||||||
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
|
Dir.chdir(app_path) do
|
||||||
rails "generate", "model", "book", "title:string"
|
rails "generate", "model", "book", "title:string"
|
||||||
output = rails("db:prepare")
|
output = rails("db:prepare")
|
||||||
assert_match(/CreateBooks: migrated/, output)
|
assert_match(/CreateBooks: migrated/, output)
|
||||||
|
assert_match(/create_table "books"/, File.read("db/schema.rb"))
|
||||||
|
|
||||||
output = rails("db:drop")
|
output = rails("db:drop")
|
||||||
assert_match(/Dropped database/, output)
|
assert_match(/Dropped database/, output)
|
||||||
|
|
||||||
rails "generate", "model", "recipe", "title:string"
|
rails "generate", "model", "recipe", "title:string"
|
||||||
output = rails("db:prepare")
|
output = rails("db:prepare")
|
||||||
assert_match(/CreateBooks: migrated/, output)
|
assert_no_match(/CreateBooks: migrated/, output) # loaded from schema
|
||||||
assert_match(/CreateRecipes: migrated/, output)
|
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
|
||||||
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
|
Dir.chdir(app_path) do
|
||||||
rails "generate", "model", "book", "title:string"
|
rails "generate", "model", "book", "title:string"
|
||||||
rails "db:create", "db:migrate"
|
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
|
app_file "config/initializers/disable_dumping_schema.rb", <<-RUBY
|
||||||
Rails.application.config.active_record.dump_schema_after_migration = false
|
Rails.application.config.active_record.dump_schema_after_migration = false
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
rails "db:prepare"
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue