Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport #30579 #30926

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Fixes #28332.
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ db_namespace = namespace :db do
begin
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
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
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
Expand All @@ -321,7 +321,7 @@ db_namespace = namespace :db do

# desc "Recreate the test database from an existent structure.sql file"
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

# desc "Empty the test database"
Expand Down
21 changes: 11 additions & 10 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,22 @@ def structure_load(*arguments)
class_for_adapter(configuration["adapter"]).new(*arguments).structure_load(filename, structure_load_flags)
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)

check_schema_file(file)
ActiveRecord::Base.establish_connection(configuration)

case format
when :ruby
check_schema_file(file)
ActiveRecord::Base.establish_connection(configuration)
load(file)
when :sql
check_schema_file(file)
structure_load(configuration, file)
else
raise ArgumentError, "unknown format #{format.inspect}"
end
ActiveRecord::InternalMetadata.create_table
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Migrator.current_environment
ActiveRecord::InternalMetadata[:environment] = environment
end

def schema_file(format = ActiveRecord::Base.schema_format)
Expand All @@ -251,8 +251,8 @@ def schema_file(format = ActiveRecord::Base.schema_format)
end

def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
each_current_configuration(environment) { |configuration|
load_schema configuration, format, file
each_current_configuration(environment) { |configuration, configuration_environment|
load_schema configuration, format, file, configuration_environment
}
ActiveRecord::Base.establish_connection(environment.to_sym)
end
Expand Down Expand Up @@ -299,9 +299,10 @@ def each_current_configuration(environment)
environments = [environment]
environments << "test" if environment == "development"

configurations = ActiveRecord::Base.configurations.values_at(*environments)
configurations.compact.each do |configuration|
yield configuration unless configuration["database"].blank?
ActiveRecord::Base.configurations.slice(*environments).each do |configuration_environment, configuration|
next unless configuration["database"]

yield configuration, configuration_environment
end
end

Expand Down
53 changes: 53 additions & 0 deletions railties/test/application/rake/dbs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ def db_structure_dump_and_load(expected_database)
db_structure_dump_and_load database_url_db_name
end

test "db:structure:dump and db:structure:load set ar_internal_metadata" do
require "#{app_path}/config/environment"
db_structure_dump_and_load ActiveRecord::Base.configurations[Rails.env]["database"]

Dir.chdir(app_path) do
assert_equal "test", `bin/rails runner -e test "puts ActiveRecord::InternalMetadata[:environment]"`.strip
assert_equal "development", `bin/rails runner "puts ActiveRecord::InternalMetadata[:environment]"`.strip
end
end

test "db:structure:dump does not dump schema information when no migrations are used" do
Dir.chdir(app_path) do
# create table without migrations
Expand Down Expand Up @@ -306,6 +316,49 @@ def db_test_load_structure
ENV["RACK_ENV"] = @old_rack_env
end
end

test "db:setup sets ar_internal_metadata" do
Dir.chdir(app_path) do
app_file "db/schema.rb", ""
`bin/rails db:setup`

test_environment = lambda { `bin/rails runner -e test "puts ActiveRecord::InternalMetadata[:environment]"`.strip }
development_environment = lambda { `bin/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

`bin/rails db:setup`

assert_equal "test", test_environment.call
assert_equal "development", development_environment.call
end
end

test "db:test:prepare sets test ar_internal_metadata" do
Dir.chdir(app_path) do
app_file "db/schema.rb", ""
`bin/rails db:test:prepare`

test_environment = lambda { `bin/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

`bin/rails db:test:prepare`

assert_equal "test", test_environment.call
end
end
end
end
end