mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Fix bin/rails db:setup
and bin/rails db:test:prepare
create wrong ar_internal_metadata's data for a test database.
Before: ``` $ RAILS_ENV=test rails dbconsole > SELECT * FROM ar_internal_metadata; key|value|created_at|updated_at environment|development|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679 ``` After: ``` $ RAILS_ENV=test rails dbconsole > SELECT * FROM ar_internal_metadata; key|value|created_at|updated_at environment|test|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679 ``` Fixes #26731.
This commit is contained in:
parent
8b52d4cfd2
commit
99b2bf8db3
4 changed files with 72 additions and 9 deletions
|
@ -1,3 +1,26 @@
|
||||||
|
* Fix `bin/rails db:setup` and `bin/rails db:test:prepare` create wrong
|
||||||
|
ar_internal_metadata's data for a test database.
|
||||||
|
|
||||||
|
Before:
|
||||||
|
```
|
||||||
|
$ RAILS_ENV=test rails dbconsole
|
||||||
|
> SELECT * FROM ar_internal_metadata;
|
||||||
|
key|value|created_at|updated_at
|
||||||
|
environment|development|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
|
||||||
|
```
|
||||||
|
|
||||||
|
After:
|
||||||
|
```
|
||||||
|
$ RAILS_ENV=test rails dbconsole
|
||||||
|
> SELECT * FROM ar_internal_metadata;
|
||||||
|
key|value|created_at|updated_at
|
||||||
|
environment|test|2017-09-11 23:14:10.815679|2017-09-11 23:14:10.815679
|
||||||
|
```
|
||||||
|
|
||||||
|
Fixes #26731.
|
||||||
|
|
||||||
|
*bogdanvlviv*
|
||||||
|
|
||||||
* Fix longer sequence name detection for serial columns.
|
* Fix longer sequence name detection for serial columns.
|
||||||
|
|
||||||
Fixes #28332.
|
Fixes #28332.
|
||||||
|
|
|
@ -313,7 +313,7 @@ db_namespace = namespace :db do
|
||||||
begin
|
begin
|
||||||
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
||||||
ActiveRecord::Schema.verbose = false
|
ActiveRecord::Schema.verbose = false
|
||||||
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"]
|
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"], "test"
|
||||||
ensure
|
ensure
|
||||||
if should_reconnect
|
if should_reconnect
|
||||||
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
|
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
|
||||||
|
@ -323,7 +323,7 @@ db_namespace = namespace :db do
|
||||||
|
|
||||||
# desc "Recreate the test database from an existent structure.sql file"
|
# desc "Recreate the test database from an existent structure.sql file"
|
||||||
task load_structure: %w(db:test:purge) do
|
task load_structure: %w(db:test:purge) do
|
||||||
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"]
|
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"], "test"
|
||||||
end
|
end
|
||||||
|
|
||||||
# desc "Empty the test database"
|
# desc "Empty the test database"
|
||||||
|
|
|
@ -225,7 +225,7 @@ module ActiveRecord
|
||||||
class_for_adapter(configuration["adapter"]).new(*arguments).structure_load(filename, structure_load_flags)
|
class_for_adapter(configuration["adapter"]).new(*arguments).structure_load(filename, structure_load_flags)
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
|
def load_schema(configuration, format = ActiveRecord::Base.schema_format, file = nil, environment = env) # :nodoc:
|
||||||
file ||= schema_file(format)
|
file ||= schema_file(format)
|
||||||
|
|
||||||
case format
|
case format
|
||||||
|
@ -240,7 +240,7 @@ module ActiveRecord
|
||||||
raise ArgumentError, "unknown format #{format.inspect}"
|
raise ArgumentError, "unknown format #{format.inspect}"
|
||||||
end
|
end
|
||||||
ActiveRecord::InternalMetadata.create_table
|
ActiveRecord::InternalMetadata.create_table
|
||||||
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
|
ActiveRecord::InternalMetadata[:environment] = environment
|
||||||
end
|
end
|
||||||
|
|
||||||
def schema_file(format = ActiveRecord::Base.schema_format)
|
def schema_file(format = ActiveRecord::Base.schema_format)
|
||||||
|
@ -253,8 +253,8 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
|
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
|
||||||
each_current_configuration(environment) { |configuration|
|
each_current_configuration(environment) { |configuration, configuration_environment|
|
||||||
load_schema configuration, format, file
|
load_schema configuration, format, file, configuration_environment
|
||||||
}
|
}
|
||||||
ActiveRecord::Base.establish_connection(environment.to_sym)
|
ActiveRecord::Base.establish_connection(environment.to_sym)
|
||||||
end
|
end
|
||||||
|
@ -301,9 +301,10 @@ module ActiveRecord
|
||||||
environments = [environment]
|
environments = [environment]
|
||||||
environments << "test" if environment == "development"
|
environments << "test" if environment == "development"
|
||||||
|
|
||||||
configurations = ActiveRecord::Base.configurations.values_at(*environments)
|
ActiveRecord::Base.configurations.slice(*environments).each do |configuration_environment, configuration|
|
||||||
configurations.compact.each do |configuration|
|
next unless configuration["database"]
|
||||||
yield configuration unless configuration["database"].blank?
|
|
||||||
|
yield configuration, configuration_environment
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -298,6 +298,45 @@ module ApplicationTests
|
||||||
ENV["RACK_ENV"] = @old_rack_env
|
ENV["RACK_ENV"] = @old_rack_env
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "db:setup sets ar_internal_metadata" do
|
||||||
|
app_file "db/schema.rb", ""
|
||||||
|
rails "db:setup"
|
||||||
|
|
||||||
|
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
||||||
|
development_environment = lambda { rails("runner", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
||||||
|
|
||||||
|
assert_equal "test", test_environment.call
|
||||||
|
assert_equal "development", development_environment.call
|
||||||
|
|
||||||
|
app_file "db/structure.sql", ""
|
||||||
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
||||||
|
Rails.application.config.active_record.schema_format = :sql
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
rails "db:setup"
|
||||||
|
|
||||||
|
assert_equal "test", test_environment.call
|
||||||
|
assert_equal "development", development_environment.call
|
||||||
|
end
|
||||||
|
|
||||||
|
test "db:test:prepare sets test ar_internal_metadata" do
|
||||||
|
app_file "db/schema.rb", ""
|
||||||
|
rails "db:test:prepare"
|
||||||
|
|
||||||
|
test_environment = lambda { rails("runner", "-e", "test", "puts ActiveRecord::InternalMetadata[:environment]").strip }
|
||||||
|
|
||||||
|
assert_equal "test", test_environment.call
|
||||||
|
|
||||||
|
app_file "db/structure.sql", ""
|
||||||
|
app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
|
||||||
|
Rails.application.config.active_record.schema_format = :sql
|
||||||
|
RUBY
|
||||||
|
|
||||||
|
rails "db:test:prepare"
|
||||||
|
|
||||||
|
assert_equal "test", test_environment.call
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue