提交 253ccbc0 编写于 作者: K kennyj

Abort a rake task when missing db/structure.sql like `db:schema:load` task.

上级 1e5ee397
* No changes.
* Abort a rake task when missing db/structure.sql like `db:schema:load` task.
*kennyj*
Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/activerecord/CHANGELOG.md) for previous changes.
......@@ -249,11 +249,8 @@ db_namespace = namespace :db do
desc 'Load a schema.rb file into the database'
task :load => [:environment, :load_config] do
file = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
if File.exists?(file)
load(file)
else
abort %{#{file} doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.}
end
ActiveRecord::Tasks::DatabaseTasks.check_schema_file(file)
load(file)
end
task :load_if_ruby => ['db:create', :environment] do
......@@ -298,6 +295,7 @@ db_namespace = namespace :db do
# desc "Recreate the databases from the structure.sql file"
task :load => [:environment, :load_config] do
filename = ENV['DB_STRUCTURE'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
ActiveRecord::Tasks::DatabaseTasks.check_schema_file(filename)
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
ActiveRecord::Tasks::DatabaseTasks.structure_load(current_config, filename)
end
......
......@@ -148,6 +148,14 @@ def structure_load(*arguments)
class_for_adapter(configuration['adapter']).new(*arguments).structure_load(filename)
end
def check_schema_file(filename)
unless File.exists?(filename)
message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.}
message << %{ If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded.} if defined?(::Rails)
Kernel.abort message
end
end
def load_seed
if seed_loader
seed_loader.load_seed
......
......@@ -305,4 +305,11 @@ class DatabaseTasksStructureLoadTest < ActiveRecord::TestCase
end
end
end
class DatabaseTasksCheckSchemaFileTest < ActiveRecord::TestCase
def test_check_schema_file
Kernel.expects(:abort).with(regexp_matches(/awesome-file.sql/))
ActiveRecord::Tasks::DatabaseTasks.check_schema_file("awesome-file.sql")
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册