Add config to disable schema dump after migration

* Add a config on Active Record named `dump_schema_after_migration`
* Schema dump doesn't happen if the config is set to false
* Set default value of the config to true
* Set config in generated production environment file to false
* Update configuration guide
* Update CHANGELOG
This commit is contained in:
Emil Soman 2014-02-05 13:02:38 +05:30
parent 90794325cf
commit 8806768e9f
8 changed files with 84 additions and 1 deletions

View File

@ -1,3 +1,11 @@
* Add flag to disable schema dump after migration.
Add a config parameter on Active Record named `dump_schema_after_migration`
which is true by default. Now schema dump does not happen at the
end of migration rake task if `dump_schema_after_migration` is false.
*Emil Soman*
* Make sure transaction state gets reset after a commit operation on the record.
If a new transaction was open inside a callback, the record was loosing track

View File

@ -76,6 +76,15 @@ module ActiveRecord
mattr_accessor :timestamped_migrations, instance_writer: false
self.timestamped_migrations = true
##
# :singleton-method:
# Specify whether schema dump should happen at the end of the
# db:migrate rake task. This is true by default, which is useful for the
# development environment. This should ideally be false in the production
# environment where dumping schema is rarely needed.
mattr_accessor :dump_schema_after_migration, instance_writer: false
self.dump_schema_after_migration = true
# :nodoc:
mattr_accessor :maintain_test_schema, instance_accessor: false

View File

@ -34,7 +34,7 @@ db_namespace = namespace :db do
ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
end
db_namespace['_dump'].invoke
db_namespace['_dump'].invoke if ActiveRecord::Base.dump_schema_after_migration
end
task :_dump do

View File

@ -292,6 +292,12 @@ All these configuration options are delegated to the `I18n` library.
* `config.active_record.maintain_test_schema` is a boolean value which controls whether Active Record should try to keep your test database schema up-to-date with `db/schema.rb` (or `db/structure.sql`) when you run your tests. The default is true.
* `config.active_record.dump_schema_after_migration` is a flag which
controls whether or not schema dump should happen (`db/schema.rb` or
`db/structure.sql`) when you run migrations. This is set to false in
`config/environments/production.rb` which is generated by Rails. The
default value is true if this configuration is not set.
The MySQL adapter adds one additional configuration option:
* `ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns in a MySQL database to be booleans and is true by default.

View File

@ -1,3 +1,10 @@
* Set `dump_schema_after_migration` config values in production.
Set `config.active_record.dump_schema_after_migration` as false
in the generated `config/environments/production.rb` file.
*Emil Soman*
* Added Thor-action for creation of migrations.
Fixes #13588 and #12674.

View File

@ -81,4 +81,9 @@ Rails.application.configure do
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
<%- unless options.skip_active_record? -%>
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false
<%- end -%>
end

View File

@ -781,5 +781,22 @@ module ApplicationTests
assert_not Rails.configuration.respond_to?(:method_missing)
assert Rails.configuration.respond_to?(:method_missing, true)
end
test "config.active_record.dump_schema_after_migration is false on production" do
build_app
ENV["RAILS_ENV"] = "production"
require "#{app_path}/config/environment"
assert_not ActiveRecord::Base.dump_schema_after_migration
end
test "config.active_record.dump_schema_after_migration is true by default on development" do
ENV["RAILS_ENV"] = "development"
require "#{app_path}/config/environment"
assert ActiveRecord::Base.dump_schema_after_migration
end
end
end

View File

@ -153,6 +153,37 @@ module ApplicationTests
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
end
end
test 'schema generation when dump_schema_after_migration is set' do
add_to_config('config.active_record.dump_schema_after_migration = false')
Dir.chdir(app_path) do
`rails generate model book title:string;
bundle exec rake db:migrate`
assert !File.exist?("db/schema.rb")
end
add_to_config('config.active_record.dump_schema_after_migration = true')
Dir.chdir(app_path) do
`rails generate model author name:string;
bundle exec rake db:migrate`
structure_dump = File.read("db/schema.rb")
assert_match(/create_table "authors"/, structure_dump)
end
end
test 'default schema generation after migration' do
Dir.chdir(app_path) do
`rails generate model book title:string;
bundle exec rake db:migrate`
structure_dump = File.read("db/schema.rb")
assert_match(/create_table "books"/, structure_dump)
end
end
end
end
end