db-clone 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +12 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +9 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +61 -0
  8. data/Rakefile +13 -20
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/db-clone.gemspec +30 -0
  12. data/lib/db/clone.rb +50 -0
  13. data/lib/db/clone/base.rb +59 -0
  14. data/lib/{db_clone → db/clone}/cmd_builder.rb +10 -12
  15. data/lib/db/clone/cmd_prompt.rb +43 -0
  16. data/lib/db/clone/rake_task.rb +17 -0
  17. data/lib/db/clone/version.rb +5 -0
  18. data/screenshot.png +0 -0
  19. metadata +54 -123
  20. data/MIT-LICENSE +0 -20
  21. data/app/assets/javascripts/db_clone/application.js +0 -13
  22. data/app/assets/stylesheets/db_clone/application.css +0 -15
  23. data/app/controllers/db_clone/application_controller.rb +0 -5
  24. data/app/helpers/db_clone/application_helper.rb +0 -4
  25. data/app/views/layouts/db_clone/application.html.erb +0 -14
  26. data/config/routes.rb +0 -2
  27. data/lib/db_clone.rb +0 -27
  28. data/lib/db_clone/db_selections.rb +0 -65
  29. data/lib/db_clone/engine.rb +0 -14
  30. data/lib/db_clone/version.rb +0 -3
  31. data/lib/tasks/db_clone_tasks.rake +0 -6
  32. data/spec/cmd_builder_spec.rb +0 -73
  33. data/spec/db_clone_spec.rb +0 -15
  34. data/spec/db_selections_spec.rb +0 -74
  35. data/spec/dummy/README.rdoc +0 -28
  36. data/spec/dummy/Rakefile +0 -6
  37. data/spec/dummy/app/assets/javascripts/application.js +0 -13
  38. data/spec/dummy/app/assets/stylesheets/application.css +0 -15
  39. data/spec/dummy/app/controllers/application_controller.rb +0 -5
  40. data/spec/dummy/app/helpers/application_helper.rb +0 -2
  41. data/spec/dummy/app/views/layouts/application.html.erb +0 -14
  42. data/spec/dummy/bin/bundle +0 -3
  43. data/spec/dummy/bin/rails +0 -4
  44. data/spec/dummy/bin/rake +0 -4
  45. data/spec/dummy/bin/setup +0 -29
  46. data/spec/dummy/config.ru +0 -4
  47. data/spec/dummy/config/application.rb +0 -31
  48. data/spec/dummy/config/boot.rb +0 -5
  49. data/spec/dummy/config/database.yml +0 -59
  50. data/spec/dummy/config/environment.rb +0 -5
  51. data/spec/dummy/config/environments/development.rb +0 -41
  52. data/spec/dummy/config/environments/production.rb +0 -79
  53. data/spec/dummy/config/environments/test.rb +0 -42
  54. data/spec/dummy/config/initializers/assets.rb +0 -11
  55. data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
  56. data/spec/dummy/config/initializers/cookies_serializer.rb +0 -3
  57. data/spec/dummy/config/initializers/filter_parameter_logging.rb +0 -4
  58. data/spec/dummy/config/initializers/inflections.rb +0 -16
  59. data/spec/dummy/config/initializers/mime_types.rb +0 -4
  60. data/spec/dummy/config/initializers/session_store.rb +0 -3
  61. data/spec/dummy/config/initializers/wrap_parameters.rb +0 -14
  62. data/spec/dummy/config/locales/en.yml +0 -23
  63. data/spec/dummy/config/routes.rb +0 -4
  64. data/spec/dummy/config/secrets.yml +0 -22
  65. data/spec/dummy/log/test.log +0 -40
  66. data/spec/dummy/public/404.html +0 -67
  67. data/spec/dummy/public/422.html +0 -67
  68. data/spec/dummy/public/500.html +0 -66
  69. data/spec/dummy/public/favicon.ico +0 -0
  70. data/spec/rails_helper.rb +0 -59
  71. data/spec/spec_helper.rb +0 -92
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 49f2e655fd5b8142404f997e65a1f2e647c1c970
4
- data.tar.gz: c6157b020d04905514e4de0ba16c69759b7a22c2
3
+ metadata.gz: 858dc634b9bdd7e324b615026b443a6f94848f2d
4
+ data.tar.gz: 3fb6466df18b1e20116d9c95c357e008167a298c
5
5
  SHA512:
6
- metadata.gz: 6468a86aafb06f00e78727269d55668c42a2eb68746c99fbaad60476ce0980fdc87c69a3c7be4c965ae5e186b71a8e57905aae24f303c81b9700eaff10b4791d
7
- data.tar.gz: 9274df308d3a9cb27085f5b4ef2c046ddfdbc11e1ef667fd1fd9efb0b1f7b54156ffdec53376773cee053ef29ad7aef8a9cb128a1a3289348cef02f8dc73bc87
6
+ metadata.gz: 9d816644da342037ba4181ea3bba95c95ee96a9ed44282c9603b2005654db29be9506d9333f18bf67e116bb76f014a9a0878b779159a1ac44e8e82efdebeca8a
7
+ data.tar.gz: '0569a2e201f32ef1c2df198ee480578d23aad86bc568338d56db8b43e0bf959ecb292852a4ef01cf595134e90028c958e3183cd705db96982eb5a12d7225adab'
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.3
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ #ruby=ruby-2.3.3
4
+ #ruby-gemset=db-clone
5
+
6
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
7
+
8
+ # Specify your gem's dependencies in db-clone.gemspec
9
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Sean Huber
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ [![Gem Version](https://badge.fury.io/rb/db-clone.svg)](https://badge.fury.io/rb/db-clone)
2
+ [![Build Status](https://travis-ci.org/seanhuber/db-clone.svg?branch=master)](https://travis-ci.org/seanhuber/db-clone)
3
+ [![Coverage Status](https://coveralls.io/repos/github/seanhuber/db-clone/badge.svg?branch=master)](https://coveralls.io/github/seanhuber/db-clone?branch=master)
4
+
5
+ # db-clone
6
+
7
+ The `db-clone` gem provides a rake task for cloning one database into another. It does this by making a system call to `mysqldump` or `pg_dump` (currently only mysql and postgresql are supported) and uses the contents of your application's `database.yml` to determine the command line arguments.
8
+
9
+ ![Screenshot](https://cdn.rawgit.com/seanhuber/db-clone/master/screenshot.png)
10
+
11
+ ## Requirements
12
+
13
+ Ruby >= 2.3
14
+
15
+ db-clone requires `mysqldump` (for MySQL) and/or `pg_dump` (for PostgreSQL).
16
+
17
+ ## Installation
18
+
19
+ Add to `gem 'db-clone', '~> 2.0'` to your `Gemfile` and `bundle install`.
20
+
21
+ ## Usage
22
+
23
+ To clone your `production` database to `development`, run:
24
+
25
+ ```
26
+ rake db:clone
27
+ ```
28
+
29
+ If you have more database blocks (besides `production` and `development`) defined in `config/database.yml`, you can clone between these databases using DbClone's CLI:
30
+
31
+ ```
32
+ rake db:clone[manual]
33
+ ```
34
+
35
+ ## Configuration
36
+
37
+ By default `rake db:clone` will read from `config/database.yml` and use your `production` database as the source and `development` as the destination. You can configure these defaults to something else by adding to your `Rakefile` (or in an initializer if you're using Rails):
38
+
39
+ ```ruby
40
+ Db::Clone.setup do |config|
41
+ # default is 'config/database.yml'
42
+ config.database_yml_path = '/path/to/my_database_config.yml'
43
+
44
+ # default is 'production'
45
+ config.default_source_database = 'my_source_db'
46
+
47
+ # default is 'development'
48
+ config.default_destination_database = 'my_destination_db'
49
+
50
+ # default is [], adds --ignore-table arguments to mysqldump
51
+ config.ignore_tables = ['schema_migrations', 'some_other_table']
52
+ end
53
+ ```
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/seanhuber/db-clone.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile CHANGED
@@ -1,23 +1,16 @@
1
- #!/usr/bin/env rake
2
- begin
3
- require 'bundler/setup'
4
- rescue LoadError
5
- puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
- end
7
-
8
- Bundler::GemHelper.install_tasks
9
-
10
- APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
11
- load 'rails/tasks/engine.rake'
12
-
13
- Bundler::GemHelper.install_tasks
14
-
15
- Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
16
-
17
- require 'rspec/core'
1
+ require 'bundler/gem_tasks'
18
2
  require 'rspec/core/rake_task'
3
+ require 'db/clone'
19
4
 
20
- desc 'Run all specs in spec directory (excluding plugin specs)'
21
- RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
5
+ RSpec::Core::RakeTask.new(:spec)
22
6
 
23
- task default: :spec
7
+ task :default => :spec
8
+
9
+ Db::Clone.setup do |config|
10
+ config.database_yml_path = File.join 'spec', 'sample_database.yml'
11
+ # config.default_source_database = 'production'
12
+ # config.default_destination_database = 'development'
13
+ # config.default_source_database = 'uat2'
14
+ # config.default_destination_database = 'test'
15
+ config.ignore_tables = ['first_ignore', 'second_ignore']
16
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "db/clone"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "db/clone/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "db-clone"
8
+ spec.version = Db::Clone::VERSION
9
+ spec.authors = ["Sean Huber"]
10
+ spec.email = ["seanhuber@seanhuber.com"]
11
+ spec.homepage = 'https://github.com/seanhuber/db-clone'
12
+ spec.summary = 'rake db:clone will clone your production database to development'
13
+ spec.description = 'rake db:clone[manual] provides a CLI for selecting a specific source database to copy to a specific destination database. Supports postgreql and mysql.'
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'colorize', '~> 0.7'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.15'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'coveralls', '~> 0.8'
29
+ spec.add_development_dependency 'awesome_print'
30
+ end
@@ -0,0 +1,50 @@
1
+ require 'colorize'
2
+ require 'yaml'
3
+
4
+ require 'db/clone/cmd_builder.rb'
5
+ require 'db/clone/cmd_prompt.rb'
6
+ require 'db/clone/base.rb'
7
+ require 'db/clone/rake_task.rb'
8
+ require 'db/clone/version'
9
+
10
+ Db::Clone::RakeTask.new.install_tasks
11
+
12
+ module Db
13
+ module Clone
14
+ @@database_yml_path = File.join 'config', 'database.yml'
15
+ def self.database_yml_path
16
+ @@database_yml_path
17
+ end
18
+ def self.database_yml_path=(database_yml_path)
19
+ @@database_yml_path = database_yml_path
20
+ end
21
+
22
+ @@default_source_database = 'production'
23
+ def self.default_source_database
24
+ @@default_source_database
25
+ end
26
+ def self.default_source_database=(default_source_database)
27
+ @@default_source_database = default_source_database
28
+ end
29
+
30
+ @@default_destination_database = 'development'
31
+ def self.default_destination_database
32
+ @@default_destination_database
33
+ end
34
+ def self.default_destination_database=(default_destination_database)
35
+ @@default_destination_database = default_destination_database
36
+ end
37
+
38
+ @@ignore_tables = []
39
+ def self.ignore_tables
40
+ @@ignore_tables
41
+ end
42
+ def self.ignore_tables=(ignore_tables)
43
+ @@ignore_tables = ignore_tables
44
+ end
45
+
46
+ def self.setup
47
+ yield self
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,59 @@
1
+ module Db::Clone
2
+ class Base
3
+ include CmdPrompt
4
+
5
+ def self.clone! invoke_cli
6
+ self.new.clone! invoke_cli
7
+ end
8
+
9
+ def initialize
10
+ @db_yml = YAML.load_file Db::Clone.database_yml_path
11
+ raise ArgumentError.new("#{Db::Clone.database_yml_path} does not have at least 2 database blocks.") unless @db_yml.length >= 2
12
+
13
+ @default_src = Db::Clone.default_source_database
14
+ @default_dest = Db::Clone.default_destination_database
15
+ end
16
+
17
+ def clone! invoke_cli
18
+ src_db, dest_db = invoke_cli ? prompt_for_src_and_dest : default_src_and_dest
19
+
20
+ cmd_builder = CmdBuilder.new src: @db_yml[src_db], dest: @db_yml[dest_db]
21
+
22
+ puts "\n Executing: #{cmd_builder.cmd.light_blue}\n\n"
23
+ exec cmd_builder.cmd
24
+ end
25
+
26
+ private
27
+
28
+ def prompt_for_src_and_dest
29
+ databases = @db_yml.keys.sort
30
+ default_src = databases.include?(@default_src) ? @default_src : databases.first
31
+
32
+ src_db = prompt 'Which is the source database?', default_src, databases.map{|name| [name, name.yellow]}.to_h
33
+ puts "Source database = #{src_db.green}"
34
+
35
+ databases.delete src_db
36
+ default_dest = src_db != @default_dest && databases.include?(@default_dest) ? @default_dest : databases.first
37
+
38
+ dest_db = prompt 'Which is the destination database?', default_dest, databases.map{|name| [name, name.yellow]}.to_h
39
+ puts "Destination database = #{dest_db.green}"
40
+
41
+ if dest_db == @default_src
42
+ proceed = ask_yes_no "#{'WARNING!'.black.on_yellow} You have selected #{@default_src.green} as your destination database, meaning that db-clone will overwrite it with the contents of your #{src_db.green} database. Do you want to proceed anyway?", false
43
+ unless proceed
44
+ puts "\nCloning #{'canceled'.red}."
45
+ abort
46
+ end
47
+ end
48
+
49
+ [src_db, dest_db]
50
+ end
51
+
52
+ def default_src_and_dest
53
+ raise ArgumentError.new("Configured default source database, #{@default_src}, does not exist in #{Db::Clone.database_yml_path}") unless @db_yml[@default_src]
54
+ raise ArgumentError.new("Configured default destination database, #{@default_dest}, does not exist in #{Db::Clone.database_yml_path}") unless @db_yml[@default_dest]
55
+
56
+ [@default_src, @default_dest]
57
+ end
58
+ end
59
+ end
@@ -1,24 +1,22 @@
1
- module DbClone
1
+ module Db::Clone
2
2
  class CmdBuilder
3
3
  VALID_DB_KEYS = ['host', 'port', 'username', 'database', 'password']
4
4
  SUPPORTED_DBS = [:mysql, :postgresql]
5
5
 
6
- def exec!() exec @cmd end
6
+ attr_reader :cmd
7
7
 
8
- def initialize( selections )
9
- raise(ArgumentError, 'Both source and destination must be set') unless selections[:src] && selections[:dest]
10
- raise(ArgumentError, 'Source and destination databases must be of the same type') unless selections[:src]['adapter'] == selections[:dest]['adapter']
8
+ def initialize selections
9
+ raise ArgumentError.new('Both source and destination must be set') unless selections[:src] && selections[:dest]
10
+ raise ArgumentError.new('Source and destination databases must be of the same type') unless selections[:src]['adapter'] == selections[:dest]['adapter']
11
11
  SUPPORTED_DBS.each{|db| @db_type = db if selections[:src]['adapter'].include?(db.to_s)}
12
- raise(ArgumentError, "Unsupported database: #{selections[:src]['adapter']}") unless @db_type
12
+ raise ArgumentError.new("Unsupported database: #{selections[:src]['adapter']}") unless @db_type
13
13
  src_dest = selections.map{|k,v| [k,v.delete_if{|i,j| !VALID_DB_KEYS.include?(i)}]}.to_h
14
- @cmd = send("build_#{@db_type}_cmd", src_dest)
14
+ @cmd = send "build_#{@db_type}_cmd", src_dest
15
15
  end
16
16
 
17
- def get_cmd() @cmd end
18
-
19
17
  private
20
18
 
21
- def build_mysql_cmd( src_dest )
19
+ def build_mysql_cmd src_dest
22
20
  mysqldump_args = [
23
21
  "mysqldump --no-create-db --add-drop-table --lock-tables=false",
24
22
  "--user=#{src_dest[:src]['username']}",
@@ -27,7 +25,7 @@ module DbClone
27
25
  "--port=#{src_dest[:src]['port']}"
28
26
  ]
29
27
 
30
- DbClone.config[:ignore_tables].each{|tbl| mysqldump_args << "--ignore-table=#{src_dest[:src]['database']}.#{tbl}"} if DbClone.config && DbClone.config[:ignore_tables].is_a?(Array)
28
+ Db::Clone.ignore_tables.each{|tbl| mysqldump_args << "--ignore-table=#{src_dest[:src]['database']}.#{tbl}"}
31
29
 
32
30
  (mysqldump_args + [
33
31
  "#{src_dest[:src]['database']}",
@@ -40,7 +38,7 @@ module DbClone
40
38
  ]).join(' ')
41
39
  end
42
40
 
43
- def build_postgresql_cmd( src_dest )
41
+ def build_postgresql_cmd src_dest
44
42
  [
45
43
  "pg_dump --no-password --clean",
46
44
  "--host=#{src_dest[:src]['host']}",
@@ -0,0 +1,43 @@
1
+ module Db::Clone
2
+ module CmdPrompt
3
+ # def ask_for_text instruction, default_text=nil
4
+ # if default_text
5
+ # print "\n#{instruction} [default = \"#{default_text}\"]: "
6
+ # else
7
+ # print "\n#{instruction}: "
8
+ # end
9
+ # text = STDIN.gets.chomp.strip
10
+ # text = default_text if text.blank? && default_text
11
+ # text
12
+ # end
13
+
14
+ def ask_yes_no question, default_to_yes=true
15
+ print "\n#{question} [#{default_to_yes ? 'Yn' : 'yN'}]: "
16
+ answer = STDIN.gets.chomp.strip.downcase
17
+ default_to_yes ? !(answer == 'n') : answer == 'y'
18
+ end
19
+
20
+ def prompt question, default, options={}
21
+ default_num = nil
22
+ numbered_opts = options.map.with_index do |opt, idx|
23
+ default_num = idx+1 if opt[0] == default
24
+ [idx+1, {ret_key: opt[0], label: opt[1]}]
25
+ end.to_h
26
+
27
+ puts "\n"
28
+ numbered_opts.each{|choice_num, opt| puts " [#{choice_num.to_s.white}] #{opt[:label]}" }
29
+
30
+ print "\n#{question} [1-#{options.length}, default=#{default_num}]: "
31
+
32
+ selection = STDIN.gets.chomp.strip.to_i
33
+ selection = default_num if selection.zero?
34
+
35
+ if numbered_opts.has_key? selection
36
+ numbered_opts[selection][:ret_key]
37
+ else
38
+ puts "#{'Invalid selection:'.red} #{selection}"
39
+ prompt question, default, options
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,17 @@
1
+ module Db::Clone
2
+ class RakeTask
3
+ include Rake::DSL if defined? Rake::DSL
4
+
5
+ def install_tasks
6
+ return unless defined? namespace
7
+
8
+ namespace :db do
9
+ desc 'clones a source database to a destination database'
10
+ task :clone, [:manual] => :environment do |t, args|
11
+ invoke_cli = !args[:manual].nil?
12
+ Db::Clone::Base.clone! invoke_cli
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end