From 8806768e9f1a2648085f7826d9a0032457182bdb Mon Sep 17 00:00:00 2001 From: Emil Soman Date: Wed, 5 Feb 2014 13:02:38 +0530 Subject: 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 --- activerecord/CHANGELOG.md | 8 ++++++ activerecord/lib/active_record/core.rb | 9 +++++++ .../lib/active_record/railties/databases.rake | 2 +- guides/source/configuring.md | 6 +++++ railties/CHANGELOG.md | 7 +++++ .../templates/config/environments/production.rb.tt | 5 ++++ railties/test/application/configuration_test.rb | 17 ++++++++++++ railties/test/application/rake/migrations_test.rb | 31 ++++++++++++++++++++++ 8 files changed, 84 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index d14b5b27f8..aeb3d328e5 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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 diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d691192cfd..d9aaf8597f 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -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 diff --git a/activerecord/lib/active_record/railties/databases.rake b/activerecord/lib/active_record/railties/databases.rake index 561387a179..9be734e88e 100644 --- a/activerecord/lib/active_record/railties/databases.rake +++ b/activerecord/lib/active_record/railties/databases.rake @@ -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 diff --git a/guides/source/configuring.md b/guides/source/configuring.md index 669086edbe..561a69749a 100644 --- a/guides/source/configuring.md +++ b/guides/source/configuring.md @@ -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. diff --git a/railties/CHANGELOG.md b/railties/CHANGELOG.md index 4ac6a99662..7aeabf735f 100644 --- a/railties/CHANGELOG.md +++ b/railties/CHANGELOG.md @@ -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. diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index d2f041aa27..d9cc60d656 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -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 diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 02d8b2c91d..b814479540 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -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 diff --git a/railties/test/application/rake/migrations_test.rb b/railties/test/application/rake/migrations_test.rb index 33c753868c..b7fd5d02c5 100644 --- a/railties/test/application/rake/migrations_test.rb +++ b/railties/test/application/rake/migrations_test.rb @@ -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 -- cgit v1.2.3