提交 f2a44ade 编写于 作者: C Carlos Antonio da Silva

Merge pull request #7564 from kennyj/using_mysqldump

Use mysqldump native commands when rake db:structure:dump. Closes #5547
Fixes database tasks config to use encoding instead of charset.
## Rails 4.0.0 (unreleased) ## ## Rails 4.0.0 (unreleased) ##
* Use native `mysqldump` command instead of `structure_dump` method
when dumping the database structure to a sql file. Fixes #5547.
*kennyj*
* Attribute predicate methods, such as `article.title?`, will now raise * Attribute predicate methods, such as `article.title?`, will now raise
`ActiveModel::MissingAttributeError` if the attribute being queried for `ActiveModel::MissingAttributeError` if the attribute being queried for
truthiness was not read from the database, instead of just returning false. truthiness was not read from the database, instead of just returning false.
......
...@@ -27,7 +27,7 @@ def create ...@@ -27,7 +27,7 @@ def create
rescue error_class => error rescue error_class => error
$stderr.puts error.error $stderr.puts error.error
$stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}" $stderr.puts "Couldn't create database for #{configuration.inspect}, #{creation_options.inspect}"
$stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['charset'] $stderr.puts "(If you set the charset manually, make sure you have a matching collation)" if configuration['encoding']
end end
def drop def drop
...@@ -49,19 +49,17 @@ def collation ...@@ -49,19 +49,17 @@ def collation
end end
def structure_dump(filename) def structure_dump(filename)
establish_connection configuration args = prepare_command_options('mysqldump')
File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump } args.concat(["--result-file", "#{filename}"])
args.concat(["--no-data"])
args.concat(["#{configuration['database']}"])
Kernel.system(*args)
end end
def structure_load(filename) def structure_load(filename)
args = ['mysql'] args = prepare_command_options('mysql')
args.concat(['--user', configuration['username']]) if configuration['username']
args << "--password=#{configuration['password']}" if configuration['password']
args.concat(['--default-character-set', configuration['charset']]) if configuration['charset']
configuration.slice('host', 'port', 'socket', 'database').each do |k, v|
args.concat([ "--#{k}", v ]) if v
end
args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}]) args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
args.concat(["--database", "#{configuration['database']}"])
Kernel.system(*args) Kernel.system(*args)
end end
...@@ -77,7 +75,7 @@ def configuration_without_database ...@@ -77,7 +75,7 @@ def configuration_without_database
def creation_options def creation_options
{ {
charset: (configuration['charset'] || DEFAULT_CHARSET), charset: (configuration['encoding'] || DEFAULT_CHARSET),
collation: (configuration['collation'] || DEFAULT_COLLATION) collation: (configuration['collation'] || DEFAULT_COLLATION)
} }
end end
...@@ -113,6 +111,18 @@ def root_password ...@@ -113,6 +111,18 @@ def root_password
$stdout.print "Please provide the root password for your mysql installation\n>" $stdout.print "Please provide the root password for your mysql installation\n>"
$stdin.gets.strip $stdin.gets.strip
end end
def prepare_command_options(command)
args = [command]
args.concat(['--user', configuration['username']]) if configuration['username']
args << "--password=#{configuration['password']}" if configuration['password']
args.concat(['--default-character-set', configuration['encoding']]) if configuration['encoding']
configuration.slice('host', 'port', 'socket').each do |k, v|
args.concat([ "--#{k}", v ]) if v
end
args
end
end end
end end
end end
...@@ -32,7 +32,7 @@ def test_creates_database_with_given_options ...@@ -32,7 +32,7 @@ def test_creates_database_with_given_options
with('my-app-db', {:charset => 'latin', :collation => 'latin_ci'}) with('my-app-db', {:charset => 'latin', :collation => 'latin_ci'})
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge( ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge(
'charset' => 'latin', 'collation' => 'latin_ci' 'encoding' => 'latin', 'collation' => 'latin_ci'
) )
end end
...@@ -176,7 +176,7 @@ def test_recreates_database_with_the_given_options ...@@ -176,7 +176,7 @@ def test_recreates_database_with_the_given_options
with('test-db', {:charset => 'latin', :collation => 'latin_ci'}) with('test-db', {:charset => 'latin', :collation => 'latin_ci'})
ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge( ActiveRecord::Tasks::DatabaseTasks.purge @configuration.merge(
'charset' => 'latin', 'collation' => 'latin_ci' 'encoding' => 'latin', 'collation' => 'latin_ci'
) )
end end
end end
...@@ -219,44 +219,31 @@ def test_db_retrieves_collation ...@@ -219,44 +219,31 @@ def test_db_retrieves_collation
class MySQLStructureDumpTest < ActiveRecord::TestCase class MySQLStructureDumpTest < ActiveRecord::TestCase
def setup def setup
@connection = stub(:structure_dump => true)
@configuration = { @configuration = {
'adapter' => 'mysql', 'adapter' => 'mysql',
'database' => 'test-db' 'database' => 'test-db'
} }
ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
end end
def test_structure_dump def test_structure_dump
filename = "awesome-file.sql" filename = "awesome-file.sql"
ActiveRecord::Base.expects(:establish_connection).with(@configuration) Kernel.expects(:system).with("mysqldump", "--result-file", filename, "--no-data", "test-db")
@connection.expects(:structure_dump)
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename) ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
assert File.exists?(filename)
ensure
FileUtils.rm(filename)
end end
end end
class MySQLStructureLoadTest < ActiveRecord::TestCase class MySQLStructureLoadTest < ActiveRecord::TestCase
def setup def setup
@connection = stub
@configuration = { @configuration = {
'adapter' => 'mysql', 'adapter' => 'mysql',
'database' => 'test-db' 'database' => 'test-db'
} }
ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
Kernel.stubs(:system)
end end
def test_structure_load def test_structure_load
filename = "awesome-file.sql" filename = "awesome-file.sql"
Kernel.expects(:system).with('mysql', '--database', 'test-db', '--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}) Kernel.expects(:system).with('mysql', '--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}, "--database", "test-db")
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename) ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册