mirror of https://github.com/rails/rails
Merge pull request #44834 from ghiculescu/schema-dump-env
`rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
This commit is contained in:
commit
ca6d15ba17
|
@ -1,3 +1,15 @@
|
|||
* `rails db:schema:{dump,load}` now checks `ENV["SCHEMA_FORMAT"]` before config
|
||||
|
||||
Since `rails db:structure:{dump,load}` was deprecated there wasn't a simple
|
||||
way to dump a schema to both SQL and Ruby formats. You can now do this with
|
||||
an environment variable. For example:
|
||||
|
||||
```
|
||||
SCHEMA_FORMAT=sql rake db:schema:dump
|
||||
```
|
||||
|
||||
*Alex Ghiculescu*
|
||||
|
||||
* Fixed MariaDB default function support.
|
||||
|
||||
Defaults would be written wrong in "db/schema.rb" and not work correctly
|
||||
|
|
|
@ -452,30 +452,32 @@ db_namespace = namespace :db do
|
|||
end
|
||||
|
||||
namespace :schema do
|
||||
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`)"
|
||||
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `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)
|
||||
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
||||
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
|
||||
end
|
||||
end
|
||||
|
||||
db_namespace["schema:dump"].reenable
|
||||
end
|
||||
|
||||
desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the database"
|
||||
desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the database"
|
||||
task load: [:load_config, :check_protected_environments] do
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(ActiveRecord.schema_format, ENV["SCHEMA"])
|
||||
end
|
||||
|
||||
namespace :dump do
|
||||
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
||||
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) for #{name} database"
|
||||
desc "Creates a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) for #{name} database"
|
||||
task name => :load_config do
|
||||
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
|
||||
ActiveRecord::Base.establish_connection(db_config)
|
||||
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config)
|
||||
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
||||
ActiveRecord::Tasks::DatabaseTasks.dump_schema(db_config, schema_format)
|
||||
db_namespace["schema:dump:#{name}"].reenable
|
||||
end
|
||||
end
|
||||
|
@ -483,11 +485,12 @@ db_namespace = namespace :db do
|
|||
|
||||
namespace :load do
|
||||
ActiveRecord::Tasks::DatabaseTasks.for_each(databases) do |name|
|
||||
desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `config.active_record.schema_format`) into the #{name} database"
|
||||
desc "Loads a database schema file (either db/schema.rb or db/structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`) into the #{name} database"
|
||||
task name => [:load_config, :check_protected_environments] do
|
||||
original_db_config = ActiveRecord::Base.connection_db_config
|
||||
db_config = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env, name: name)
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config)
|
||||
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
||||
ensure
|
||||
ActiveRecord::Base.establish_connection(original_db_config) if original_db_config
|
||||
end
|
||||
|
@ -545,12 +548,13 @@ db_namespace = namespace :db do
|
|||
db_namespace["test:load_schema"].invoke
|
||||
end
|
||||
|
||||
# desc "Recreate the test database from an existent schema file (schema.rb or structure.sql, depending on `config.active_record.schema_format`)"
|
||||
# desc "Recreate the test database from an existent schema file (schema.rb or structure.sql, depending on `ENV['SCHEMA_FORMAT']` or `config.active_record.schema_format`)"
|
||||
task load_schema: %w(db:test:purge) do
|
||||
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
||||
ActiveRecord::Schema.verbose = false
|
||||
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |db_config|
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config)
|
||||
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
||||
end
|
||||
ensure
|
||||
if should_reconnect
|
||||
|
@ -586,7 +590,8 @@ db_namespace = namespace :db do
|
|||
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
||||
ActiveRecord::Schema.verbose = false
|
||||
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "test", name: name)
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config)
|
||||
schema_format = ENV.fetch("SCHEMA_FORMAT", ActiveRecord.schema_format).to_sym
|
||||
ActiveRecord::Tasks::DatabaseTasks.load_schema(db_config, schema_format)
|
||||
ensure
|
||||
if should_reconnect
|
||||
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym)
|
||||
|
|
|
@ -352,6 +352,15 @@ module ApplicationTests
|
|||
end
|
||||
end
|
||||
|
||||
def db_schema_sql_dump
|
||||
Dir.chdir(app_path) do
|
||||
args = ["generate", "model", "book", "title:string"]
|
||||
rails args
|
||||
rails "db:migrate", "db:schema:dump"
|
||||
assert_match(/CREATE TABLE/, File.read("db/structure.sql"))
|
||||
end
|
||||
end
|
||||
|
||||
test "db:schema:dump without database_url" do
|
||||
db_schema_dump
|
||||
end
|
||||
|
@ -361,7 +370,29 @@ module ApplicationTests
|
|||
db_schema_dump
|
||||
end
|
||||
|
||||
def db_schema_cache_dump(filename = "db/schema_cache.yml")
|
||||
test "db:schema:dump with env as ruby" do
|
||||
add_to_config "config.active_record.schema_format = :sql"
|
||||
|
||||
old_env = ENV["SCHEMA_FORMAT"]
|
||||
ENV["SCHEMA_FORMAT"] = "ruby"
|
||||
|
||||
db_schema_dump
|
||||
ensure
|
||||
ENV["SCHEMA_FORMAT"] = old_env
|
||||
end
|
||||
|
||||
test "db:schema:dump with env as sql" do
|
||||
add_to_config "config.active_record.schema_format = :ruby"
|
||||
|
||||
old_env = ENV["SCHEMA_FORMAT"]
|
||||
ENV["SCHEMA_FORMAT"] = "sql"
|
||||
|
||||
db_schema_sql_dump
|
||||
ensure
|
||||
ENV["SCHEMA_FORMAT"] = old_env
|
||||
end
|
||||
|
||||
def db_schema_cache_dump
|
||||
Dir.chdir(app_path) do
|
||||
rails "db:schema:cache:dump"
|
||||
|
||||
|
@ -398,7 +429,7 @@ module ApplicationTests
|
|||
end
|
||||
|
||||
db_schema_dump
|
||||
db_schema_cache_dump("db/special_schema_cache.yml")
|
||||
db_schema_cache_dump
|
||||
end
|
||||
|
||||
test "db:schema:cache:dump custom env" do
|
||||
|
@ -407,7 +438,7 @@ module ApplicationTests
|
|||
ENV["SCHEMA_CACHE"] = filename
|
||||
|
||||
db_schema_dump
|
||||
db_schema_cache_dump(filename)
|
||||
db_schema_cache_dump
|
||||
ensure
|
||||
ENV["SCHEMA_CACHE"] = @old_schema_cache_env
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue