mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add option to disable schema dumb per-database
Dumping the schema is on by default for all databases in an application. To turn it off for a specific database use the `schema_dump` option: ```yaml # config/database.yml production: schema_dump: false ``` Co-authored-by: Luis Vasconcellos <vasconcelloslf@gmail.com>
This commit is contained in:
parent
a39098dc85
commit
10ca60a16b
6 changed files with 191 additions and 3 deletions
|
@ -1,3 +1,17 @@
|
|||
* Add option to disable schema dumb per-database
|
||||
|
||||
Dumping the schema is on by default for all databases in an application. To turn it off for a
|
||||
specific database use the `schema_dump` option:
|
||||
|
||||
```yaml
|
||||
# config/database.yml
|
||||
|
||||
production:
|
||||
schema_dump: false
|
||||
```
|
||||
|
||||
*Luis Vasconcellos*, *Eileen M. Uchitelle*
|
||||
|
||||
* Fix `eager_loading?` when ordering with `Hash` syntax
|
||||
|
||||
`eager_loading?` is triggered correctly when using `order` with hash syntax
|
||||
|
|
|
@ -26,6 +26,7 @@ module ActiveRecord
|
|||
# connections.
|
||||
class HashConfig < DatabaseConfig
|
||||
attr_reader :configuration_hash
|
||||
|
||||
def initialize(env_name, name, configuration_hash)
|
||||
super(env_name, name)
|
||||
@configuration_hash = configuration_hash.symbolize_keys.freeze
|
||||
|
@ -103,6 +104,11 @@ module ActiveRecord
|
|||
def schema_cache_path
|
||||
configuration_hash[:schema_cache_path]
|
||||
end
|
||||
|
||||
# Determines whether to dump the schema for a database.
|
||||
def schema_dump
|
||||
configuration_hash.fetch(:schema_dump, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -121,9 +121,13 @@ db_namespace = namespace :db do
|
|||
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
||||
# IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false
|
||||
task name do
|
||||
if ActiveRecord.dump_schema_after_migration
|
||||
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
|
||||
|
||||
if ActiveRecord.dump_schema_after_migration && db_config.schema_dump
|
||||
ActiveRecord::Base.establish_connection(db_config)
|
||||
db_namespace["schema:dump:#{name}"].invoke
|
||||
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:#{name}"].reenable
|
||||
|
@ -433,9 +437,11 @@ db_namespace = namespace :db do
|
|||
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`)"
|
||||
task dump: :load_config do
|
||||
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
|
||||
if db_config.schema_dump
|
||||
ActiveRecord::Base.establish_connection(db_config)
|
||||
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config)
|
||||
end
|
||||
end
|
||||
|
||||
db_namespace["schema:dump"].reenable
|
||||
end
|
||||
|
|
|
@ -102,6 +102,26 @@ module ActiveRecord
|
|||
config = HashConfig.new("default_env", "primary", idle_timeout: "0")
|
||||
assert_nil config.idle_timeout
|
||||
end
|
||||
|
||||
def test_default_schema_dump_value
|
||||
config = HashConfig.new("default_env", "primary", {})
|
||||
assert_equal true, config.schema_dump
|
||||
end
|
||||
|
||||
def test_schema_dump_value_set_to_true
|
||||
config = HashConfig.new("default_env", "primary", { schema_dump: true })
|
||||
assert_equal true, config.schema_dump
|
||||
end
|
||||
|
||||
def test_schema_dump_value_set_to_nil
|
||||
config = HashConfig.new("default_env", "primary", { schema_dump: nil })
|
||||
assert_nil config.schema_dump
|
||||
end
|
||||
|
||||
def test_schema_dump_value_set_to_false
|
||||
config = HashConfig.new("default_env", "primary", { schema_dump: false })
|
||||
assert_equal false, config.schema_dump
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -425,6 +425,42 @@ module ApplicationTests
|
|||
assert_match(/up\s+002\s+Two migration/, output)
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration and schema_dump are true" do
|
||||
add_to_config("config.active_record.dump_schema_after_migration = true")
|
||||
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
adapter: sqlite3
|
||||
database: 'dev_db'
|
||||
schema_dump: true
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate"
|
||||
|
||||
assert File.exist?("db/schema.rb"), "should dump schema when configured to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration is true schema_dump is false" do
|
||||
add_to_config("config.active_record.dump_schema_after_migration = true")
|
||||
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
adapter: sqlite3
|
||||
database: 'dev_db'
|
||||
schema_dump: false
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate"
|
||||
|
||||
assert_not File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration is set" do
|
||||
add_to_config("config.active_record.dump_schema_after_migration = false")
|
||||
|
||||
|
|
|
@ -812,6 +812,112 @@ module ApplicationTests
|
|||
db_create_and_drop_namespace("primary", "db/development.sqlite3")
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration is true schema_dump is false" do
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
primary:
|
||||
adapter: sqlite3
|
||||
database: dev_db
|
||||
schema_dump: false
|
||||
secondary:
|
||||
adapter: sqlite3
|
||||
database: secondary_dev_db
|
||||
schema_dump: false
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate"
|
||||
|
||||
assert_not File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
assert_not File.exist?("db/secondary_schema.rb"), "should not dump schema when configured not to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration is false and schema_dump is true" do
|
||||
add_to_config("config.active_record.dump_schema_after_migration = false")
|
||||
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
primary:
|
||||
adapter: sqlite3
|
||||
database: dev_db
|
||||
secondary:
|
||||
adapter: sqlite3
|
||||
database: secondary_dev_db
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate"
|
||||
|
||||
assert_not File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
assert_not File.exist?("db/secondary_schema.rb"), "should not dump schema when configured not to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation with schema dump only for primary" do
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
primary:
|
||||
adapter: sqlite3
|
||||
database: primary_dev_db
|
||||
secondary:
|
||||
adapter: sqlite3
|
||||
database: secondary_dev_db
|
||||
schema_dump: false
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate:primary", "db:migrate:secondary"
|
||||
|
||||
assert File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
assert_not File.exist?("db/secondary_schema.rb"), "should not dump schema when configured not to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation with schema dump only for secondary" do
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
primary:
|
||||
adapter: sqlite3
|
||||
database: primary_dev_db
|
||||
schema_dump: false
|
||||
secondary:
|
||||
adapter: sqlite3
|
||||
database: secondary_dev_db
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate:primary", "db:migrate:secondary"
|
||||
|
||||
assert_not File.exist?("db/schema.rb"), "should not dump schema when configured not to"
|
||||
assert File.exist?("db/secondary_schema.rb"), "should dump schema when configured to"
|
||||
end
|
||||
end
|
||||
|
||||
test "schema generation when dump_schema_after_migration and schema_dump are true" do
|
||||
app_file "config/database.yml", <<~EOS
|
||||
development:
|
||||
primary:
|
||||
adapter: sqlite3
|
||||
database: dev_db
|
||||
secondary:
|
||||
adapter: sqlite3
|
||||
database: secondary_dev_db
|
||||
EOS
|
||||
|
||||
Dir.chdir(app_path) do
|
||||
rails "generate", "model", "book", "title:string"
|
||||
rails "db:migrate"
|
||||
|
||||
assert File.exist?("db/schema.rb"), "should dump schema when configured to"
|
||||
assert File.exist?("db/secondary_schema.rb"), "should dump schema when configured to"
|
||||
end
|
||||
end
|
||||
|
||||
test "db:create and db:drop don't raise errors when loading YAML containing multiple ERB statements on the same line" do
|
||||
app_file "config/database.yml", <<-YAML
|
||||
development:
|
||||
|
|
Loading…
Reference in a new issue