database_exporter 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Database Exporter
2
+
3
+ Experimental project to gain access to system-level database import, export and copy capabilities. It's build to handle Rail's database configuration conventions.
4
+
5
+ # Database Support
6
+
7
+ Current support is for MySQL only.
8
+
9
+ # Installation & Usage
10
+
11
+ To install, add `gem 'database_exporter'` to your Gemfile or do `gem install database_exporter`.
12
+
13
+ Specs are a good example of how to use this library, here's some more:
14
+
15
+ ## Export
16
+ db = DatabaseExporter.new({'adapter' => 'mysql2', 'username' => 'root', 'database' => 'some_database'})
17
+ db.export #=> Native MySQL database dump
18
+
19
+ ## Import
20
+ db = DatabaseExporter.new({'adapter' => 'mysql2', 'username' => 'root', 'database' => 'some_database'})
21
+ db.import('CREATE TABLE IF NOT EXISTS some_new_table;')
22
+
23
+ ## Copy
24
+ # Database to copy from
25
+ db = DatabaseExporter.new({'adapter' => 'mysql2', 'username' => 'root', 'database' => 'source_database'})
26
+
27
+ # Database to copy to
28
+ db.copy({'adapter' => 'mysql2', 'username' => 'root', 'database' => 'destination_database'})
29
+
30
+ # License
31
+
32
+ Database Exporter is released under the MIT license
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ require 'bundler'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "database_exporter"
8
+ s.version = '0.0.2'
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ["Mike Pack"]
11
+ s.email = ["mikepackdev@gmail.com"]
12
+ s.homepage = "http://www.mikepackdev.com"
13
+ s.summary = %q{Access to system-level database import, export and copy capabilities.}
14
+ s.description = %q{Experimental project to gain access to system-level database import, export and copy capabilities. It's build to handle Rail's database configuration conventions.}
15
+
16
+ s.required_rubygems_version = ">= 1.3.6"
17
+
18
+ s.files = %w( README.md database_exporter.gemspec ) + Dir["lib/**/*.rb"]
19
+ s.test_files = Dir["spec/**/*.rb"]
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,25 @@
1
+ class MysqlAdapter
2
+ def initialize(config)
3
+ @config = config
4
+ end
5
+
6
+ def export
7
+ ensure_db_exists
8
+ Kernel.` "mysqldump -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']} #{@config['database']}"
9
+ end
10
+
11
+ def import(sql)
12
+ ensure_db_exists
13
+ Kernel.` "echo \"#{sql}\" | mysql -h #{@config['host']} -u #{@config['username']} #{'-p' + @config['password'] if @config['password']} #{@config['database']}"
14
+ end
15
+
16
+ def copy(new_db_config)
17
+ ensure_db_exists
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
+ end
21
+
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
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'forwardable'
2
+
3
+ class DatabaseExporter
4
+ extend Forwardable
5
+
6
+ def initialize(config)
7
+ @datbase = case config['adapter']
8
+ when /mysql/
9
+ MysqlAdapter.new(config)
10
+ end
11
+ end
12
+
13
+ def_delegators :@database, :export, :import, :copy
14
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe MysqlAdapter do
4
+ context 'with a MySQL database' do
5
+ let(:adapter) { MysqlAdapter.new({"database"=>"some_mysql_db", "host"=>"localhost", "username"=>"root", "password"=>nil}) }
6
+
7
+ describe '#export' do
8
+ it 'ensures the database exists' do
9
+ adapter.should_receive(:ensure_db_exists)
10
+ adapter.export
11
+ end
12
+
13
+ it 'executes the mysqdldump command' do
14
+ Kernel.should_receive(:`).with(/mysqldump/)
15
+ adapter.export
16
+ end
17
+
18
+ it 'returns the database dump' do
19
+ adapter.export.should =~ /Database: some_mysql_db/
20
+ end
21
+ end
22
+
23
+ describe '#import' do
24
+ it 'ensures the database exists' do
25
+ adapter.should_receive(:ensure_db_exists)
26
+ adapter.import('SHOW DATABASES;')
27
+ end
28
+
29
+ it 'executes the mysql command' do
30
+ Kernel.should_receive(:`).with(/mysql\s/)
31
+ adapter.import('SHOW DATABASES;')
32
+ end
33
+
34
+ it 'imports the desired SQL' do
35
+ sql = <<-SQL
36
+ CREATE TABLE my_new_database_table (
37
+ id INT,
38
+ data VARCHAR(100)
39
+ );
40
+ SQL
41
+ adapter.import(sql)
42
+ adapter.export.should =~ /my_new_database_table/
43
+
44
+ # Remove the table so it doesn't disrupt future-run specs
45
+ sql = <<-SQL
46
+ DROP TABLE my_new_database_table;
47
+ SQL
48
+ adapter.import(sql)
49
+ end
50
+ end
51
+
52
+ describe '#copy' do
53
+ it 'executes the mysqldump and pipes to the mysql command' do
54
+ Kernel.should_receive(:`).with(/mysqldump.*\|\s+mysql/)
55
+ adapter.copy({"database"=>"new_mysql_db", "host"=>"localhost", "username"=>"root", "password"=>nil})
56
+ end
57
+
58
+ it 'copies one database to another' do
59
+ # Create a new table to check that it was duplicated to the new database
60
+ adapter.import('CREATE TABLE some_new_database_table ( id INT );')
61
+ adapter.copy({"database"=>"new_mysql_db", "host"=>"localhost", "username"=>"root", "password"=>nil})
62
+
63
+ # Load the new database and check that the table exists
64
+ new_adapter = MysqlAdapter.new({"database"=>"new_mysql_db", "host"=>"localhost", "username"=>"root", "password"=>nil})
65
+ new_adapter.export.should =~ /some_new_database_table/
66
+
67
+ `echo "DROP DATABASE some_mysql_db" | mysql -u root`
68
+ `echo "DROP DATABASE new_mysql_db" | mysql -u root`
69
+ end
70
+ end
71
+
72
+ describe '#ensure_db_exists' do
73
+ it 'creates the database if it does not exist' do
74
+ `mysqldump -u root some_db_that_does_not_exist`
75
+ $?.success?.should be_false
76
+
77
+ adapter.ensure_db_exists('some_db_that_does_not_exist')
78
+
79
+ `mysqldump -u root some_db_that_does_not_exist`
80
+ $?.success?.should be_true
81
+
82
+ `echo "DROP DATABASE some_db_that_does_not_exist" | mysql -u root`
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe DatabaseExporter do
4
+ describe '#initialize' do
5
+ it 'initializes the MysqlAdapter when passed a MySQL config' do
6
+ MysqlAdapter.should_receive(:new).with({'adapter' => 'mysql2'})
7
+ DatabaseExporter.new({'adapter' => 'mysql2'})
8
+ end
9
+ end
10
+
11
+ describe '#export, #import, #copy' do
12
+ it 'all exist (and are delegated to the adapter)' do
13
+ exporter = DatabaseExporter.new({'adapter' => 'mysql2'})
14
+ [:export, :import, :copy].each do |method|
15
+ exporter.respond_to?(method).should be_true
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), '../lib/database_exporter')
2
+ require File.join(File.dirname(__FILE__), '../lib/adapters/mysql_adapter')
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: database_exporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mike Pack
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-18 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Experimental project to gain access to system-level database import,
15
+ export and copy capabilities. It's build to handle Rail's database configuration
16
+ conventions.
17
+ email:
18
+ - mikepackdev@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - README.md
24
+ - database_exporter.gemspec
25
+ - lib/adapters/mysql_adapter.rb
26
+ - lib/database_exporter.rb
27
+ - spec/lib/adapters/mysql_adapter_spec.rb
28
+ - spec/lib/database_exporter_spec.rb
29
+ - spec/spec_helper.rb
30
+ homepage: http://www.mikepackdev.com
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.6
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 1.8.10
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Access to system-level database import, export and copy capabilities.
54
+ test_files:
55
+ - spec/lib/adapters/mysql_adapter_spec.rb
56
+ - spec/lib/database_exporter_spec.rb
57
+ - spec/spec_helper.rb