mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
dump_schema_after_migration
applies migration tasks other than db:migrate
Closes #20743. The task `db:_dump` now only dumps the schema if `ActiveRecord::Base.dump_schema_after_migration` is true. This has effects: - `db:migrate:up` - `db:migrate:down` - `db:forward` - `db:rollback`
This commit is contained in:
parent
a8f250a22c
commit
2183caa24a
3 changed files with 27 additions and 15 deletions
|
@ -1,3 +1,10 @@
|
|||
* `ActiveRecord::Base.dump_schema_after_migration` applies migration tasks
|
||||
other than `db:migrate`. (eg. `db:rollback`, `db:migrate:dup`, ...)
|
||||
|
||||
Fixes #20743.
|
||||
|
||||
*Yves Senn*
|
||||
|
||||
* Add alternate syntax to make `change_column_default` reversible.
|
||||
|
||||
User can pass in `:from` and `:to` to make `change_column_default` command
|
||||
|
|
|
@ -42,19 +42,22 @@ db_namespace = namespace :db do
|
|||
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
|
||||
task :migrate => [:environment, :load_config] do
|
||||
ActiveRecord::Tasks::DatabaseTasks.migrate
|
||||
db_namespace['_dump'].invoke if ActiveRecord::Base.dump_schema_after_migration
|
||||
db_namespace['_dump'].invoke
|
||||
end
|
||||
|
||||
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
|
||||
task :_dump do
|
||||
case ActiveRecord::Base.schema_format
|
||||
when :ruby then db_namespace["schema:dump"].invoke
|
||||
when :sql then db_namespace["structure:dump"].invoke
|
||||
else
|
||||
raise "unknown schema format #{ActiveRecord::Base.schema_format}"
|
||||
if ActiveRecord::Base.dump_schema_after_migration
|
||||
case ActiveRecord::Base.schema_format
|
||||
when :ruby then db_namespace["schema:dump"].invoke
|
||||
when :sql then db_namespace["structure:dump"].invoke
|
||||
else
|
||||
raise "unknown schema format #{ActiveRecord::Base.schema_format}"
|
||||
end
|
||||
# Allow this task to be called as many times as required. An example is the
|
||||
# migrate:redo task, which calls other two internally that depend on this one.
|
||||
db_namespace['_dump'].reenable
|
||||
end
|
||||
# Allow this task to be called as many times as required. An example is the
|
||||
# migrate:redo task, which calls other two internally that depend on this one.
|
||||
db_namespace['_dump'].reenable
|
||||
end
|
||||
|
||||
namespace :migrate do
|
||||
|
|
|
@ -158,20 +158,22 @@ module ApplicationTests
|
|||
add_to_config('config.active_record.dump_schema_after_migration = false')
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
`bin/rails generate model book title:string;
|
||||
bin/rake db:migrate`
|
||||
`bin/rails generate model book title:string`
|
||||
output = `bin/rails generate model author name:string`
|
||||
version = output =~ %r{[^/]+db/migrate/(\d+)_create_authors\.rb} && $1
|
||||
|
||||
assert !File.exist?("db/schema.rb")
|
||||
`bin/rake db:migrate db:rollback db:forward db:migrate:up db:migrate:down VERSION=#{version}`
|
||||
assert !File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
end
|
||||
|
||||
add_to_config('config.active_record.dump_schema_after_migration = true')
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
`bin/rails generate model author name:string;
|
||||
bin/rake db:migrate`
|
||||
`bin/rails generate model reviews book_id:integer`
|
||||
`bin/rake db:migrate`
|
||||
|
||||
structure_dump = File.read("db/schema.rb")
|
||||
assert_match(/create_table "authors"/, structure_dump)
|
||||
assert_match(/create_table "reviews"/, structure_dump)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue