backhoe 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/Gemfile +1 -0
- data/README.md +3 -4
- data/backhoe.gemspec +3 -0
- data/lib/backhoe.rb +4 -68
- data/lib/backhoe/base.rb +14 -0
- data/lib/backhoe/mysql.rb +96 -0
- data/lib/backhoe/postgresql.rb +18 -0
- data/lib/backhoe/sqlite.rb +15 -0
- data/lib/backhoe/version.rb +1 -1
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56b4732a51c97a122c1ceb8777b8c1063e12465b
|
4
|
+
data.tar.gz: b4e047fc5ceabbe0a6ca030ceea674ba7ad71ff8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cc3c5f7edc01601af3c9c6e7a28b86e8e569fe0ab48ef378c577b73e6287580ec394380fbc629675003b608ace0c8d856634b0db2d1b4ffebcfdec021b4ca52
|
7
|
+
data.tar.gz: 3c39d974916a71d3ad7f725f40d9e23c6829f8a68d558ffb41d04e8ceac520b24e13a818fe0fdf30b41bfee04a32d0114522a223c56bc5910c020e2b3bb7be51
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Backhoe
|
2
|
+
[![Build Status](https://travis-ci.org/botandrose/backhoe.svg)](https://travis-ci.org/botandrose/backhoe)
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
4
|
+
Dump and load current ActiveRecord database to and from a file.
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -32,7 +31,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
31
|
|
33
32
|
## Contributing
|
34
33
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
34
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/backhoe.
|
36
35
|
|
37
36
|
## License
|
38
37
|
|
data/backhoe.gemspec
CHANGED
@@ -21,7 +21,10 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
+
spec.add_dependency "activerecord"
|
25
|
+
|
24
26
|
spec.add_development_dependency "bundler", "~> 1.16"
|
25
27
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
28
|
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
spec.add_development_dependency "mysql2"
|
27
30
|
end
|
data/lib/backhoe.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
require "backhoe/version"
|
2
|
-
require "
|
3
|
-
require "
|
2
|
+
require "backhoe/mysql"
|
3
|
+
require "backhoe/postgresql"
|
4
|
+
require "backhoe/sqlite"
|
5
|
+
require "active_record"
|
4
6
|
|
5
7
|
module Backhoe
|
6
8
|
mattr_accessor(:file_path) { "db/data.sql" }
|
@@ -24,71 +26,5 @@ module Backhoe
|
|
24
26
|
ActiveRecord::Base.configurations[Rails.env || "development"]
|
25
27
|
end
|
26
28
|
end
|
27
|
-
|
28
|
-
class Base < Struct.new(:config, :file_path)
|
29
|
-
include Rake::DSL
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def database
|
34
|
-
config["database"]
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
class Mysql < Base
|
39
|
-
def dump skip_tables: []
|
40
|
-
mysqldump = `which mysqldump`.strip
|
41
|
-
raise RuntimeError, "Cannot find mysqldump." if mysqldump.blank?
|
42
|
-
sh "#{mysqldump} --no-create-db --single-transaction --quick -e #{skip_table_options(skip_tables)} #{mysql_options} > #{file_path}"
|
43
|
-
end
|
44
|
-
|
45
|
-
def load
|
46
|
-
mysql = `which mysql`.strip
|
47
|
-
raise RuntimeError, "Cannot find mysql." if mysql.blank?
|
48
|
-
sh "#{mysql} #{mysql_options} < #{file_path}"
|
49
|
-
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def skip_table_options skip_tables
|
54
|
-
skip_tables.map do |table|
|
55
|
-
"--ignore-table=#{config["database"]}.#{table}"
|
56
|
-
end.join(" ")
|
57
|
-
end
|
58
|
-
|
59
|
-
def mysql_options
|
60
|
-
options = " -u #{config["username"]}"
|
61
|
-
options += " -p'#{config["password"]}'" if config["password"]
|
62
|
-
options += " -h #{config["host"]}" if config["host"]
|
63
|
-
options += " -S #{config["socket"]}" if config["socket"]
|
64
|
-
options += " '#{config["database"]}'"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
Mysql2 = Mysql
|
69
|
-
|
70
|
-
class Sqlite3 < Base
|
71
|
-
def dump **_
|
72
|
-
FileUtils.cp database, file_path
|
73
|
-
end
|
74
|
-
|
75
|
-
def load
|
76
|
-
FileUtils.cp file_path, database
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class Postgresql < Base
|
81
|
-
def dump **_
|
82
|
-
pg_dump = `which pg_dump`.strip
|
83
|
-
raise RuntimeError, "Cannot find pg_dump." if pg_dump.blank?
|
84
|
-
sh "#{pg_dump} -c -f#{file_path} #{database}"
|
85
|
-
end
|
86
|
-
|
87
|
-
def load
|
88
|
-
psql = `which psql`.strip
|
89
|
-
raise RuntimeError, "Cannot find psql." if psql.blank?
|
90
|
-
sh "#{psql} -q -d#{database} -f#{file_path}"
|
91
|
-
end
|
92
|
-
end
|
93
29
|
end
|
94
30
|
|
data/lib/backhoe/base.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
require "backhoe/base"
|
2
|
+
|
3
|
+
module Backhoe
|
4
|
+
class Mysql < Base
|
5
|
+
def dump skip_tables: []
|
6
|
+
mysqldump = `which mysqldump`.strip
|
7
|
+
raise RuntimeError, "Cannot find mysqldump." if mysqldump.blank?
|
8
|
+
sh "#{mysqldump} --no-create-db --single-transaction --quick -e #{skip_table_options(skip_tables)} #{mysql_options} > #{file_path}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def load
|
12
|
+
mysql = `which mysql`.strip
|
13
|
+
raise RuntimeError, "Cannot find mysql." if mysql.blank?
|
14
|
+
sh "#{mysql} #{mysql_options} < #{file_path}"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def skip_table_options skip_tables
|
20
|
+
skip_tables.map do |table|
|
21
|
+
"--ignore-table=#{config["database"]}.#{table}"
|
22
|
+
end.join(" ")
|
23
|
+
end
|
24
|
+
|
25
|
+
def mysql_options
|
26
|
+
options = " -u #{config["username"]}"
|
27
|
+
options += " -p'#{config["password"]}'" if config["password"]
|
28
|
+
options += " -h #{config["host"]}" if config["host"]
|
29
|
+
options += " -S #{config["socket"]}" if config["socket"]
|
30
|
+
options += " '#{config["database"]}'"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module MysqlSkipColumns
|
35
|
+
def dump **options
|
36
|
+
if skip_columns = options.delete(:skip_columns)
|
37
|
+
SanitizedDatabase.new(skip_columns, file_path).dump do |skip_tables|
|
38
|
+
options[:skip_tables] ||= []
|
39
|
+
options[:skip_tables] += skip_tables
|
40
|
+
super options
|
41
|
+
end
|
42
|
+
else
|
43
|
+
super
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
class SanitizedDatabase < Struct.new(:config, :file_path)
|
50
|
+
def dump
|
51
|
+
with_sanitized_tables do
|
52
|
+
yield skip_tables
|
53
|
+
end
|
54
|
+
skip_tables.each do |table|
|
55
|
+
File.write file_path, "RENAME TABLE `sanitized_#{table}` TO `#{table}`;", mode: "a"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def skip_tables
|
62
|
+
config.keys
|
63
|
+
end
|
64
|
+
|
65
|
+
def with_sanitized_tables
|
66
|
+
ActiveRecord::Base.transaction do
|
67
|
+
config.each do |table, columns|
|
68
|
+
sanitized_table = "sanitized_#{table}"
|
69
|
+
sql <<-SQL
|
70
|
+
DROP TABLE IF EXISTS `#{sanitized_table}`;
|
71
|
+
CREATE TABLE `#{sanitized_table}` LIKE `#{table}`;
|
72
|
+
INSERT INTO `#{sanitized_table}` SELECT * FROM `#{table}`;
|
73
|
+
ALTER TABLE `#{sanitized_table}` #{columns.map { |column| "DROP `#{column}`" }.join(", ")};
|
74
|
+
SQL
|
75
|
+
end
|
76
|
+
|
77
|
+
yield
|
78
|
+
|
79
|
+
config.each do |table, _|
|
80
|
+
sql "DROP TABLE `sanitized_#{table}`"
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def sql queries
|
86
|
+
queries.split(";").select(&:present?).each do |query|
|
87
|
+
ActiveRecord::Base.connection.execute query
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
Mysql.prepend MysqlSkipColumns
|
93
|
+
|
94
|
+
Mysql2 = Mysql
|
95
|
+
end
|
96
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "backhoe/base"
|
2
|
+
|
3
|
+
module Backhoe
|
4
|
+
class Postgresql < Base
|
5
|
+
def dump **_
|
6
|
+
pg_dump = `which pg_dump`.strip
|
7
|
+
raise RuntimeError, "Cannot find pg_dump." if pg_dump.blank?
|
8
|
+
sh "#{pg_dump} -c -f#{file_path} #{database}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def load
|
12
|
+
psql = `which psql`.strip
|
13
|
+
raise RuntimeError, "Cannot find psql." if psql.blank?
|
14
|
+
sh "#{psql} -q -d#{database} -f#{file_path}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
data/lib/backhoe/version.rb
CHANGED
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backhoe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-05-
|
11
|
+
date: 2018-05-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +66,20 @@ dependencies:
|
|
52
66
|
- - "~>"
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mysql2
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
55
83
|
description: Dump and load current database to and from a file.
|
56
84
|
email:
|
57
85
|
- micah@botandrose.com
|
@@ -70,6 +98,10 @@ files:
|
|
70
98
|
- bin/console
|
71
99
|
- bin/setup
|
72
100
|
- lib/backhoe.rb
|
101
|
+
- lib/backhoe/base.rb
|
102
|
+
- lib/backhoe/mysql.rb
|
103
|
+
- lib/backhoe/postgresql.rb
|
104
|
+
- lib/backhoe/sqlite.rb
|
73
105
|
- lib/backhoe/version.rb
|
74
106
|
homepage: https://github.com/botandrose/backhoe
|
75
107
|
licenses:
|