Skip to content

Commit

Permalink
schema rake tasks are specific about the configuration to act on.
Browse files Browse the repository at this point in the history
The rake tasks and the `DatabaseTakss` adapter classes used to
assume a configuration at some places. This forced the rake
tasks to establish a specific connection before calling into
`load_schema`.

After #15394 this started to cause issues because it could
`purge` the wrong database before loading the schema.
  • Loading branch information
senny committed Aug 6, 2014
1 parent 53dba73 commit f15cef6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
22 changes: 14 additions & 8 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
* Deprecate `DatabaseTasks.load_schema` to act on the current connection.
Use `.load_schema_current` instead. In the future `load_schema` will
require the `configuration` to act on as an argument.

*Yves Senn*

* Fixed automatic maintaining test schema to properly handle sql structure
schema format.

Fixes #15394.

*Wojciech Wnętrzak*

* Fix type casting to Decimal from Float with large precision.

*Tomohiro Hashidate*
Expand Down Expand Up @@ -297,14 +310,7 @@

Fixes #8328.

Sean Griffin

* Fixed automatic maintaining test schema to properly handle sql structure
schema format.

Fixes #15394.

*Wojciech Wnętrzak*
*Sean Griffin*

* Pluck now works when selecting columns from different tables with the same
name.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def check_pending!(connection = Base.connection)

def load_schema_if_pending!
if ActiveRecord::Migrator.needs_migration?
ActiveRecord::Tasks::DatabaseTasks.load_schema
ActiveRecord::Tasks::DatabaseTasks.load_schema_current
check_pending!
end
end
Expand Down
14 changes: 4 additions & 10 deletions activerecord/lib/active_record/railties/databases.rake
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ db_namespace = namespace :db do

desc 'Load a schema.rb file into the database'
task :load => [:environment, :load_config] do
ActiveRecord::Tasks::DatabaseTasks.load_schema(:ruby, ENV['SCHEMA'])
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
end

task :load_if_ruby => ['db:create', :environment] do
Expand Down Expand Up @@ -286,7 +286,7 @@ db_namespace = namespace :db do

desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do
ActiveRecord::Tasks::DatabaseTasks.load_schema(:sql, ENV['DB_STRUCTURE'])
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV['DB_STRUCTURE'])
end

task :load_if_sql => ['db:create', :environment] do
Expand Down Expand Up @@ -317,9 +317,8 @@ db_namespace = namespace :db do
task :load_schema => %w(db:test:deprecated db:test:purge) do
begin
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
ActiveRecord::Schema.verbose = false
db_namespace["schema:load"].invoke
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :ruby, ENV['SCHEMA']
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
Expand All @@ -329,12 +328,7 @@ db_namespace = namespace :db do

# desc "Recreate the test database from an existent structure.sql file"
task :load_structure => %w(db:test:deprecated db:test:purge) do
begin
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => ActiveRecord::Base.configurations['test'])
db_namespace["structure:load"].invoke
ensure
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => nil)
end
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :sql, ENV['SCHEMA']
end

# desc "Recreate the test database from a fresh schema"
Expand Down
23 changes: 20 additions & 3 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,39 @@ def structure_load(*arguments)
end

def load_schema(format = ActiveRecord::Base.schema_format, file = nil)
ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc)
This method will act on a specific connection in the future.
To act on the current connection, use `load_schema_current` instead.
MESSAGE
load_schema_current(format, file)
end

# This method is the successor of +load_schema+. We should rename it
# after +load_schema+ went through a deprecation cycle. (Rails > 4.2)
def load_schema_for(configuration, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
case format
when :ruby
file ||= File.join(db_dir, "schema.rb")
check_schema_file(file)
purge(current_config)
purge(configuration)
ActiveRecord::Base.establish_connection(configuration)
load(file)
when :sql
file ||= File.join(db_dir, "structure.sql")
check_schema_file(file)
purge(current_config)
structure_load(current_config, file)
purge(configuration)
structure_load(configuration, file)
else
raise ArgumentError, "unknown format #{format.inspect}"
end
end

def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
each_current_configuration(environment) { |configuration|
load_schema_for configuration, format, file
}
end

def check_schema_file(filename)
unless File.exist?(filename)
message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.}
Expand Down

0 comments on commit f15cef6

Please sign in to comment.