Permalink
Browse files

Use the 'mysql' binary for 'rake db:structure:load'.

The previous implementation had the strange requirement that db/structure.sql contain only CREATE TABLE sql statements, one per table, separated by double newlines. SQLite3 and PostgreSQL database tasks, on the other hand, simply spawn 'sqlite3' and 'psql' binaries to load the file directly. The new implementation follows this and attempts to respect all current MySQL configuration settings.
  • Loading branch information...
1 parent a497bed commit ece23b5fa01301ec006aa42f8b59ec938c35427b @seamusabshere seamusabshere committed Sep 4, 2012
@@ -54,11 +54,15 @@ def structure_dump(filename)
end
def structure_load(filename)
- establish_connection(configuration)
- connection.execute('SET foreign_key_checks = 0')
- IO.read(filename).split("\n\n").each do |table|
- connection.execute(table)
+ 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.concat(['--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}])
+ Kernel.system(*args)
end
private
@@ -251,17 +251,14 @@ def setup
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"
- ActiveRecord::Base.expects(:establish_connection).with(@configuration)
- @connection.expects(:execute).twice
+ Kernel.expects(:system).with('mysql', '--database', 'test-db', '--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1})
- open(filename, 'w') { |f| f.puts("SELECT CURDATE();") }
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
- ensure
- FileUtils.rm(filename)
end
end

0 comments on commit ece23b5

Please sign in to comment.