database_exporter 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
data/database_exporter.gemspec
CHANGED
@@ -5,21 +5,21 @@ class MysqlAdapter
|
|
5
5
|
|
6
6
|
def export
|
7
7
|
ensure_db_exists
|
8
|
-
Kernel.` "mysqldump -h #{@config[
|
8
|
+
Kernel.` "mysqldump -h #{@config[:host]} -u #{@config[:username]} #{'-p' + @config[:password] if @config[:password]} #{@config[:database]}"
|
9
9
|
end
|
10
10
|
|
11
11
|
def import(sql)
|
12
12
|
ensure_db_exists
|
13
|
-
Kernel.` "echo \"#{sql}\" | mysql -h #{@config[
|
13
|
+
Kernel.` "echo \"#{sql}\" | mysql -h #{@config[:host]} -u #{@config[:username]} #{'-p' + @config[:password] if @config[:password]} #{@config[:database]}"
|
14
14
|
end
|
15
15
|
|
16
16
|
def copy(new_db_config)
|
17
17
|
ensure_db_exists
|
18
|
-
ensure_db_exists(new_db_config[
|
19
|
-
Kernel.` "mysqldump -h #{@config[
|
18
|
+
ensure_db_exists(new_db_config[:database])
|
19
|
+
Kernel.` "mysqldump -h #{@config[:host]} -u #{@config[:username]} #{'-p' + @config[:password] if @config[:password]} #{@config[:database]} | mysql -h #{new_db_config[:host]} -u #{new_db_config[:username]} #{'-p' + new_db_config[:password] if new_db_config[:password]} #{new_db_config[:database]}"
|
20
20
|
end
|
21
21
|
|
22
|
-
def ensure_db_exists(name = @config[
|
23
|
-
`echo "CREATE DATABASE IF NOT EXISTS #{name}" | mysql -h #{@config[
|
22
|
+
def ensure_db_exists(name = @config[:database])
|
23
|
+
`echo "CREATE DATABASE IF NOT EXISTS #{name}" | mysql -h #{@config[:host]} -u #{@config[:username]} #{'-p' + @config[:password] if @config[:password]}`
|
24
24
|
end
|
25
25
|
end
|
data/lib/database_exporter.rb
CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe MysqlAdapter do
|
4
4
|
context 'with a MySQL database' do
|
5
|
-
let(:adapter) { MysqlAdapter.new({
|
5
|
+
let(:adapter) { MysqlAdapter.new({:database => "some_mysql_db", :host => "localhost", :username => "root", :password => nil}) }
|
6
6
|
|
7
7
|
describe '#export' do
|
8
8
|
it 'ensures the database exists' do
|
@@ -52,16 +52,16 @@ describe MysqlAdapter do
|
|
52
52
|
describe '#copy' do
|
53
53
|
it 'executes the mysqldump and pipes to the mysql command' do
|
54
54
|
Kernel.should_receive(:`).with(/mysqldump.*\|\s+mysql/)
|
55
|
-
adapter.copy({
|
55
|
+
adapter.copy({:database=>"new_mysql_db", :host=>"localhost", :username=>"root", :password=>nil})
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'copies one database to another' do
|
59
59
|
# Create a new table to check that it was duplicated to the new database
|
60
60
|
adapter.import('CREATE TABLE some_new_database_table ( id INT );')
|
61
|
-
adapter.copy({
|
61
|
+
adapter.copy({:database=>"new_mysql_db", :host=>"localhost", :username=>"root", :password=>nil})
|
62
62
|
|
63
63
|
# Load the new database and check that the table exists
|
64
|
-
new_adapter = MysqlAdapter.new({
|
64
|
+
new_adapter = MysqlAdapter.new({:database=>"new_mysql_db", :host=>"localhost", :username=>"root", :password=>nil})
|
65
65
|
new_adapter.export.should =~ /some_new_database_table/
|
66
66
|
|
67
67
|
`echo "DROP DATABASE some_mysql_db" | mysql -u root`
|
@@ -3,14 +3,14 @@ require 'spec_helper'
|
|
3
3
|
describe DatabaseExporter do
|
4
4
|
describe '#initialize' do
|
5
5
|
it 'initializes the MysqlAdapter when passed a MySQL config' do
|
6
|
-
MysqlAdapter.should_receive(:new).with({
|
7
|
-
DatabaseExporter.new({
|
6
|
+
MysqlAdapter.should_receive(:new).with({:adapter => 'mysql2'})
|
7
|
+
DatabaseExporter.new({:adapter => 'mysql2'})
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
11
|
describe '#export, #import, #copy' do
|
12
12
|
it 'all exist (and are delegated to the adapter)' do
|
13
|
-
exporter = DatabaseExporter.new({
|
13
|
+
exporter = DatabaseExporter.new({:adapter => 'mysql2'})
|
14
14
|
[:export, :import, :copy, :ensure_db_exists].each do |method|
|
15
15
|
exporter.respond_to?(method).should be_true
|
16
16
|
end
|