db_replicator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +1 -1
- data/Gemfile +4 -0
- data/README.md +4 -1
- data/db_replicator.gemspec +1 -1
- data/lib/db_replicator/exec_command.rb +15 -0
- data/lib/db_replicator/importer.rb +16 -16
- data/lib/db_replicator/version.rb +1 -1
- data/spec/db_replicator/importer_spec.rb +48 -0
- data/spec/spec_helper.rb +3 -0
- metadata +7 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a550c35aaa759054879bed457b8c93c1c0850eed
|
4
|
+
data.tar.gz: e982b43bf8ebfe6a4893f375eee65168d06ccb25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 65a35755a6ef13683f1dd46b63d028b12abbe75593265daa590357194aee7e7cdb7a7e0dfd3db76999df3cf01a8bbd361fb03a1ed87f854a323ae6851e4b3d7e
|
7
|
+
data.tar.gz: d98919eccc50f9702e24f3f1d73b86f8a9afd6a682b4447f600082083e609239cf0091a00535255ff4081cf8f6427370f2593103442980d1883d3c2f03341b83
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -9,13 +9,15 @@ As of now I only suport MySQL. I plan to add more adapters in the future. See TO
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem 'db_replicator', '~> 0.0.
|
12
|
+
gem 'db_replicator', '~> 0.0.2'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
17
17
|
$ rails g db_replicator:install
|
18
18
|
|
19
|
+
Be sure to set the proxy_host to a server that can access your production database `db_replicator.rb` initializers file.
|
20
|
+
|
19
21
|
## Usage
|
20
22
|
|
21
23
|
You can download your production db by running:
|
@@ -27,6 +29,7 @@ You can download your production db by running:
|
|
27
29
|
|
28
30
|
* Allow secure upload of production db to another enviroment. e.g. staging
|
29
31
|
* Add more adapters.
|
32
|
+
* Write better tests!!!!
|
30
33
|
|
31
34
|
|
32
35
|
## Contributing
|
data/db_replicator.gemspec
CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency 'bundler', '~> 1.
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
22
|
spec.add_development_dependency 'rake', '~> 10.0'
|
23
23
|
spec.add_development_dependency 'generator_spec'
|
24
24
|
spec.add_development_dependency 'rspec'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'open3'
|
2
|
+
module ExecCommand
|
3
|
+
def exec_cmd(cmd)
|
4
|
+
Open3.popen2e(cmd) do |_, stdout_err, wait_thr|
|
5
|
+
while line = stdout_err.gets
|
6
|
+
puts line
|
7
|
+
end
|
8
|
+
|
9
|
+
exit_status = wait_thr.value
|
10
|
+
unless exit_status.success?
|
11
|
+
raise "Shell Error From: #{cmd}".colorize(:red)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,11 +1,13 @@
|
|
1
|
+
require 'db_replicator/exec_command'
|
1
2
|
module DbReplicator
|
2
3
|
class Importer
|
3
|
-
|
4
|
+
include ExecCommand
|
5
|
+
attr_accessor :to_db_env, :tmp_db_configs
|
4
6
|
def initialize(to_db_env)
|
5
7
|
@to_db_env = to_db_env
|
6
8
|
@to_db_configs = DbReplicator.db_configs(@to_db_env)
|
7
9
|
if @to_db_configs['adapter'] == 'sqlite3'
|
8
|
-
|
10
|
+
set_tmp_db_configs
|
9
11
|
else
|
10
12
|
@tmp_db_configs = nil
|
11
13
|
end
|
@@ -22,40 +24,38 @@ module DbReplicator
|
|
22
24
|
end
|
23
25
|
else
|
24
26
|
puts "mysql -u root --database=#{@to_db_configs['database']} < #{DbReplicator.dump_file}"
|
25
|
-
|
27
|
+
exec_cmd "mysql -u root --database=#{@to_db_configs['database']} < #{DbReplicator.dump_file}"
|
26
28
|
end
|
27
29
|
end
|
28
30
|
DbReplicator.document_action 'Executing db:migrate to update database Just in case their are pending migrations', 'Migrate complete.' do
|
29
|
-
|
31
|
+
exec_cmd 'bundle exec rake db:migrate'
|
30
32
|
end
|
31
33
|
end
|
32
34
|
|
33
35
|
private
|
34
36
|
|
35
37
|
def convert_sql_dump_and_import
|
36
|
-
DbReplicator.document_action "Creating temp db for transfer #{
|
37
|
-
ActiveRecord::Tasks::DatabaseTasks.create(
|
38
|
+
DbReplicator.document_action "Creating temp db for transfer #{@tmp_db_configs['database']}", 'Create complete' do
|
39
|
+
ActiveRecord::Tasks::DatabaseTasks.create(@tmp_db_configs)
|
38
40
|
end
|
39
|
-
DbReplicator.document_action "Importing data to temp db. DB: #{
|
40
|
-
puts "Executing: mysql -u root
|
41
|
-
|
41
|
+
DbReplicator.document_action "Importing data to temp db. DB: #{@tmp_db_configs['database']}; File: #{DbReplicator.dump_file}", 'Import complete' do
|
42
|
+
puts "Executing: mysql -u root --database=#{@tmp_db_configs['database']} < #{DbReplicator.dump_file}"
|
43
|
+
exec_cmd "mysql -u root --database=#{@tmp_db_configs['database']} < #{DbReplicator.dump_file}"
|
42
44
|
end
|
43
45
|
DbReplicator.document_action 'Starting data transfer', 'Data transfer complete.' do
|
44
|
-
puts "Executing: sequel #{DbReplicator.prod_db_configs['adapter']}://localhost/#{
|
45
|
-
|
46
|
+
puts "Executing: sequel #{DbReplicator.prod_db_configs['adapter']}://localhost/#{@tmp_db_configs['database']}?user=root -C sqlite://#{@to_db_configs['database']}"
|
47
|
+
exec_cmd "sequel #{DbReplicator.prod_db_configs['adapter']}://localhost/#{@tmp_db_configs['database']}?user=root -C sqlite://#{@to_db_configs['database']}"
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
def
|
51
|
+
def set_tmp_db_configs
|
50
52
|
tmp_configs = @to_db_configs.clone
|
51
53
|
tmp_configs['database'] = "#{DbReplicator.prod_db_configs['database']}_db_replicator"
|
52
|
-
|
53
|
-
tmp_configs.reject! { |k, v| %w(password username).include?(k.to_s) || v.nil? }
|
54
|
-
tmp_configs
|
54
|
+
@tmp_db_configs = tmp_configs.reject { |k, v| %w(password username).include?(k.to_s) || v.nil? }
|
55
55
|
end
|
56
56
|
|
57
57
|
def create_fresh_db
|
58
|
-
|
58
|
+
exec_cmd 'bundle exec rake db:drop db:create'
|
59
59
|
end
|
60
60
|
end
|
61
61
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require "active_record/railtie"
|
3
|
+
module DbReplicator
|
4
|
+
describe Importer do
|
5
|
+
before do
|
6
|
+
DbReplicator.stub(:prod_db_configs) { Hash["adapter" => "mysql2", "user" => "user1", "database" => "development", "password" => "123"] }
|
7
|
+
end
|
8
|
+
describe "tmp_db_configs" do
|
9
|
+
before do
|
10
|
+
expect(DbReplicator).to receive(:db_configs).and_return(Hash['adapter', 'sqlite3'])
|
11
|
+
end
|
12
|
+
it 'will set the tmp_db_configs if the development adapter is sqlite3' do
|
13
|
+
importer = Importer.new('development')
|
14
|
+
importer.tmp_db_configs['database'].should == 'development_db_replicator'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
describe "#import_db!" do
|
18
|
+
let(:importer) { Importer.new('development') }
|
19
|
+
context 'convert mysql to sqlite3' do
|
20
|
+
before do
|
21
|
+
DbReplicator.stub(:dump_file) {'dump_file'}
|
22
|
+
expect(DbReplicator).to receive(:db_configs).with('development').and_return(Hash['adapter', 'sqlite3', 'database', 'db/development.sqlite3'])
|
23
|
+
expect(ActiveRecord::Tasks::DatabaseTasks).to receive(:create)
|
24
|
+
expect(importer).to receive(:exec_cmd).with("bundle exec rake db:drop db:create")
|
25
|
+
expect(importer).to receive(:exec_cmd).with("mysql -u root --database=development_db_replicator < #{DbReplicator.dump_file}")
|
26
|
+
expect(importer).to receive(:exec_cmd).with("sequel #{DbReplicator.prod_db_configs['adapter']}://localhost/development_db_replicator?user=root -C sqlite://db/development.sqlite3")
|
27
|
+
expect(importer).to receive(:exec_cmd).with("bundle exec rake db:migrate")
|
28
|
+
end
|
29
|
+
it 'convert the mysql db to sqlite3' do
|
30
|
+
importer.import_db!
|
31
|
+
end
|
32
|
+
end
|
33
|
+
context 'env db is already mysql' do
|
34
|
+
before do
|
35
|
+
DbReplicator.stub(:dump_file) {'dump_file'}
|
36
|
+
expect(DbReplicator).to receive(:db_configs).with('development').and_return(Hash['adapter', 'mysql2', 'database', 'development'])
|
37
|
+
expect(importer).to receive(:exec_cmd).with("bundle exec rake db:drop db:create")
|
38
|
+
expect(importer).to receive(:exec_cmd).with("mysql -u root --database=development < #{DbReplicator.dump_file}")
|
39
|
+
expect(importer).to receive(:exec_cmd).with("bundle exec rake db:migrate")
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'will import the prod db into the mysql db' do
|
43
|
+
importer.import_db!
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_replicator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Eatherly
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,6 +154,7 @@ files:
|
|
154
154
|
- lib/db_replicator.rb
|
155
155
|
- lib/db_replicator/configuration.rb
|
156
156
|
- lib/db_replicator/downloader.rb
|
157
|
+
- lib/db_replicator/exec_command.rb
|
157
158
|
- lib/db_replicator/importer.rb
|
158
159
|
- lib/db_replicator/tasks.rb
|
159
160
|
- lib/db_replicator/version.rb
|
@@ -161,6 +162,7 @@ files:
|
|
161
162
|
- lib/generators/templates/db_replicator.rb
|
162
163
|
- lib/tasks/dbr.rake
|
163
164
|
- spec/db_replicator/configuration_spec.rb
|
165
|
+
- spec/db_replicator/importer_spec.rb
|
164
166
|
- spec/db_replicator_spec.rb
|
165
167
|
- spec/generators/db_replicator/install_generator_spec.rb
|
166
168
|
- spec/generators/tmp/.gitignore
|
@@ -234,6 +236,7 @@ specification_version: 4
|
|
234
236
|
summary: ''
|
235
237
|
test_files:
|
236
238
|
- spec/db_replicator/configuration_spec.rb
|
239
|
+
- spec/db_replicator/importer_spec.rb
|
237
240
|
- spec/db_replicator_spec.rb
|
238
241
|
- spec/generators/db_replicator/install_generator_spec.rb
|
239
242
|
- spec/generators/tmp/.gitignore
|