backhoe 0.11.0 → 0.12.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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +2 -2
- data/Appraisals +0 -9
- data/README.md +1 -0
- data/lib/backhoe/dump.rb +44 -42
- data/lib/backhoe/version.rb +1 -1
- data/lib/backhoe.rb +2 -2
- metadata +3 -5
- data/gemfiles/activerecord_7.2.gemfile +0 -7
- data/gemfiles/activerecord_8.0.gemfile +0 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1c3388209c28de44224013396cb7014a6353f046f3571bbbe14c9583e29c43c6
|
|
4
|
+
data.tar.gz: e19e932a0835eec574edd2d59b5c9b830a65f16acbab692aadad4fd2bea85c57
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 28348bb09ea2d40d4e2df95bb10f518602061b2be76b7301f9cfe43d67a4649259b0871dbd971243ab2887c56cc587498670d39fb4555b66744ad5ee32bc7532
|
|
7
|
+
data.tar.gz: b495b45a9bdcecf5f8d99458b984355360f555ee01a380c0d7b230619e60cf29d56206dbace91141383fc4e2b1cd0aa33ca2b8530947cfe8604886b9b7b4a196
|
data/.github/workflows/ci.yml
CHANGED
|
@@ -5,10 +5,10 @@ jobs:
|
|
|
5
5
|
strategy:
|
|
6
6
|
fail-fast: false
|
|
7
7
|
matrix:
|
|
8
|
-
gemfile: [
|
|
8
|
+
gemfile: [ activerecord_8.1 ]
|
|
9
9
|
ruby: [ 3.3, 3.4, "4.0" ]
|
|
10
10
|
|
|
11
|
-
runs-on: ubuntu-
|
|
11
|
+
runs-on: ubuntu-24.04
|
|
12
12
|
env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
|
|
13
13
|
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
|
|
14
14
|
POSTGRES_HOST_AUTH_METHOD: trust
|
data/Appraisals
CHANGED
data/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Dump and load current ActiveRecord database to and from a file.
|
|
|
10
10
|
Backhoe.dump "data.sql" # dumps db to db/data.sql
|
|
11
11
|
Backhoe.dump "data.sql.gz" # => can also dump a gzipped sql file
|
|
12
12
|
Backhoe.dump "data.sql", skip_tables: [:comments], skip_columns: { users: [:password] } # can skip whole tables or just specific columns
|
|
13
|
+
Backhoe.dump "data.sql", scrub_columns: { users: { email: "CONCAT('user', id, '@example.com')", phone: nil } } # or keep columns but overwrite them with SQL expressions (nil for NULL)
|
|
13
14
|
|
|
14
15
|
# Load
|
|
15
16
|
Backhoe.load "data.sql" # loads db from db/data.sql
|
data/lib/backhoe/dump.rb
CHANGED
|
@@ -1,35 +1,36 @@
|
|
|
1
1
|
require "rake"
|
|
2
|
+
require "shellwords"
|
|
2
3
|
|
|
3
4
|
module Backhoe
|
|
4
|
-
class Dump < Struct.new(:database, :path, :skip_tables, :skip_columns)
|
|
5
|
+
class Dump < Struct.new(:database, :path, :skip_tables, :skip_columns, :scrub_columns)
|
|
5
6
|
include Rake::DSL
|
|
6
7
|
|
|
7
8
|
def initialize *args
|
|
8
9
|
super
|
|
9
10
|
self.skip_tables ||= []
|
|
10
11
|
self.skip_columns ||= {}
|
|
12
|
+
self.scrub_columns ||= {}
|
|
11
13
|
end
|
|
12
14
|
|
|
13
15
|
def call
|
|
14
|
-
if skip_tables.any?
|
|
16
|
+
if skip_tables.any? || skip_columns.any? || scrub_columns.any?
|
|
15
17
|
raise NotImplementedError if database.postgresql?
|
|
16
18
|
end
|
|
17
|
-
if skip_columns.any?
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
self.skip_tables += tables
|
|
21
|
-
dump
|
|
19
|
+
if skip_columns.any? || scrub_columns.any?
|
|
20
|
+
SanitizedDatabase.new(skip_columns, scrub_columns).dump do |tables, trailer|
|
|
21
|
+
dump skip_tables + tables, trailer
|
|
22
22
|
end
|
|
23
23
|
else
|
|
24
|
-
dump
|
|
24
|
+
dump skip_tables
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
private
|
|
29
29
|
|
|
30
|
-
def dump
|
|
30
|
+
def dump skipped, trailer = nil
|
|
31
31
|
if database.mysql?
|
|
32
|
-
|
|
32
|
+
trailer = "; printf '\\n%s' #{Shellwords.escape(trailer)}" if trailer
|
|
33
|
+
bash_sh "{ #{mysqldump} --no-create-db --single-transaction --quick -e #{skip_table_options(skipped)} #{database.to_mysql_options} #{database.name}#{trailer}; } | #{pipe} #{target}"
|
|
33
34
|
elsif database.postgresql?
|
|
34
35
|
bash_sh "#{pg_dump} --column-inserts #{database.name} | #{pipe} #{target}"
|
|
35
36
|
elsif database.sqlite?
|
|
@@ -73,53 +74,54 @@ module Backhoe
|
|
|
73
74
|
path =~ /\.gz\b/ ? "gzip -9f" : "cat"
|
|
74
75
|
end
|
|
75
76
|
|
|
76
|
-
def skip_table_options
|
|
77
|
-
|
|
77
|
+
def skip_table_options skipped
|
|
78
|
+
skipped.map do |table|
|
|
78
79
|
"--ignore-table=#{database.name}.#{table}"
|
|
79
80
|
end.join(" ")
|
|
80
81
|
end
|
|
81
82
|
|
|
82
|
-
|
|
83
|
+
# Copies each table aside, strips the copy, dumps it in place of the original, and
|
|
84
|
+
# renames it back at the end of the dump.
|
|
85
|
+
class SanitizedDatabase
|
|
86
|
+
def initialize skip_columns, scrub_columns
|
|
87
|
+
@skip_columns = skip_columns.to_h { |table, columns| [table.to_s, columns] }
|
|
88
|
+
@scrub_columns = scrub_columns.to_h { |table, columns| [table.to_s, columns] }
|
|
89
|
+
end
|
|
90
|
+
|
|
83
91
|
def dump
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
File.write path, "RENAME TABLE `sanitized_#{table}` TO `#{table}`;", mode: "a"
|
|
89
|
-
end
|
|
92
|
+
tables.each { |table| copy table }
|
|
93
|
+
yield tables, tables.map { |table| "RENAME TABLE `sanitized_#{table}` TO `#{table}`;" }.join("\n")
|
|
94
|
+
ensure
|
|
95
|
+
tables.each { |table| sql "DROP TABLE IF EXISTS `sanitized_#{table}`" }
|
|
90
96
|
end
|
|
91
97
|
|
|
92
98
|
private
|
|
93
99
|
|
|
94
|
-
def
|
|
95
|
-
|
|
100
|
+
def tables
|
|
101
|
+
(@skip_columns.keys + @scrub_columns.keys).uniq
|
|
96
102
|
end
|
|
97
103
|
|
|
98
|
-
def
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
yield
|
|
111
|
-
|
|
112
|
-
config.each do |table, _|
|
|
113
|
-
sql "DROP TABLE `sanitized_#{table}`"
|
|
114
|
-
end
|
|
104
|
+
def copy table
|
|
105
|
+
copy = "sanitized_#{table}"
|
|
106
|
+
sql "DROP TABLE IF EXISTS `#{copy}`"
|
|
107
|
+
sql "CREATE TABLE `#{copy}` LIKE `#{table}`"
|
|
108
|
+
sql "INSERT INTO `#{copy}` SELECT * FROM `#{table}`"
|
|
109
|
+
|
|
110
|
+
scrubs = @scrub_columns.fetch(table, {})
|
|
111
|
+
if scrubs.any?
|
|
112
|
+
sets = scrubs.map { |column, expression| "`#{column}` = #{expression.nil? ? "NULL" : expression}" }
|
|
113
|
+
sql "UPDATE `#{copy}` SET #{sets.join(", ")}"
|
|
115
114
|
end
|
|
116
|
-
end
|
|
117
115
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
116
|
+
skips = @skip_columns.fetch(table, [])
|
|
117
|
+
if skips.any?
|
|
118
|
+
sql "ALTER TABLE `#{copy}` #{skips.map { |column| "DROP `#{column}`" }.join(", ")}"
|
|
121
119
|
end
|
|
122
120
|
end
|
|
121
|
+
|
|
122
|
+
def sql query
|
|
123
|
+
ActiveRecord::Base.connection.execute query
|
|
124
|
+
end
|
|
123
125
|
end
|
|
124
126
|
end
|
|
125
127
|
end
|
data/lib/backhoe/version.rb
CHANGED
data/lib/backhoe.rb
CHANGED
|
@@ -6,8 +6,8 @@ require "active_record"
|
|
|
6
6
|
|
|
7
7
|
module Backhoe
|
|
8
8
|
class << self
|
|
9
|
-
def dump path, skip_tables: [], skip_columns: {}
|
|
10
|
-
Dump.new(Database.new, path, skip_tables, skip_columns).call
|
|
9
|
+
def dump path, skip_tables: [], skip_columns: {}, scrub_columns: {}
|
|
10
|
+
Dump.new(Database.new, path, skip_tables, skip_columns, scrub_columns).call
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
def load path, drop_and_create: false
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: backhoe
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.12.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Micah Geisel
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies:
|
|
12
12
|
- !ruby/object:Gem::Dependency
|
|
13
13
|
name: activerecord
|
|
@@ -169,8 +169,6 @@ files:
|
|
|
169
169
|
- bin/console
|
|
170
170
|
- bin/setup
|
|
171
171
|
- gemfiles/.bundle/config
|
|
172
|
-
- gemfiles/activerecord_7.2.gemfile
|
|
173
|
-
- gemfiles/activerecord_8.0.gemfile
|
|
174
172
|
- gemfiles/activerecord_8.1.gemfile
|
|
175
173
|
- lib/backhoe.rb
|
|
176
174
|
- lib/backhoe/database.rb
|
|
@@ -195,7 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
195
193
|
- !ruby/object:Gem::Version
|
|
196
194
|
version: '0'
|
|
197
195
|
requirements: []
|
|
198
|
-
rubygems_version:
|
|
196
|
+
rubygems_version: 4.0.20
|
|
199
197
|
specification_version: 4
|
|
200
198
|
summary: Dump and load current database to and from a file.
|
|
201
199
|
test_files: []
|