Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use native mysqldump command for 'rake db:structure:dump'.

  • Loading branch information...
commit ccc6910cb034efc6b749e1ae82c748085a671fa9 1 parent 616ba15
@kennyj kennyj authored
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## 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
`ActiveModel::MissingAttributeError` if the attribute being queried for
truthiness was not read from the database, instead of just returning false.
View
28 activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -49,19 +49,17 @@ def collation
end
def structure_dump(filename)
- establish_connection configuration
- File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
+ args = prepare_command_options('mysqldump')
+ args.concat(["--result-file", "#{filename}"])
+ args.concat(["--no-data"])
+ args.concat(["#{configuration['database']}"])
+ Kernel.system(*args)
end
def structure_load(filename)
- args = ['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 = prepare_command_options('mysql')
args.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
+ args.concat(["--database", "#{configuration['database']}"])
Kernel.system(*args)
end
@@ -113,6 +111,18 @@ def root_password
$stdout.print "Please provide the root password for your mysql installation\n>"
$stdin.gets.strip
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['charset']]) if configuration['charset']
+ configuration.slice('host', 'port', 'socket').each do |k, v|
+ args.concat([ "--#{k}", v ]) if v
+ end
+ args
+ end
+
end
end
end
View
17 activerecord/test/cases/tasks/mysql_rake_test.rb
@@ -219,44 +219,31 @@ def test_db_retrieves_collation
class MySQLStructureDumpTest < ActiveRecord::TestCase
def setup
- @connection = stub(:structure_dump => true)
@configuration = {
'adapter' => 'mysql',
'database' => 'test-db'
}
-
- ActiveRecord::Base.stubs(:connection).returns(@connection)
- ActiveRecord::Base.stubs(:establish_connection).returns(true)
end
def test_structure_dump
filename = "awesome-file.sql"
- ActiveRecord::Base.expects(:establish_connection).with(@configuration)
- @connection.expects(:structure_dump)
+ Kernel.expects(:system).with("mysqldump", "--result-file", filename, "--no-data", "test-db")
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
- assert File.exists?(filename)
- ensure
- FileUtils.rm(filename)
end
end
class MySQLStructureLoadTest < ActiveRecord::TestCase
def setup
- @connection = stub
@configuration = {
'adapter' => 'mysql',
'database' => 'test-db'
}
-
- ActiveRecord::Base.stubs(:connection).returns(@connection)
- ActiveRecord::Base.stubs(:establish_connection).returns(true)
- Kernel.stubs(:system)
end
def test_structure_load
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)
end
Please sign in to comment.
Something went wrong with that request. Please try again.