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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: abfe37e5c43ef24a835955e044c32bef198a37d007372ec45a9ade0a6ea76b67
4
- data.tar.gz: 451090a75bf1b09d2c22f6e8b4a952a2c7fa531e6b11f49e0f3ffcb3d32fbad9
3
+ metadata.gz: 1c3388209c28de44224013396cb7014a6353f046f3571bbbe14c9583e29c43c6
4
+ data.tar.gz: e19e932a0835eec574edd2d59b5c9b830a65f16acbab692aadad4fd2bea85c57
5
5
  SHA512:
6
- metadata.gz: 8be5bb5fdc1dab88f907b60a36e168d6ee0b613ab135e2b2e4b7dad7fab89cbaae2df8dd0422879f7f3a0b3d4f7d9d6f8d1f17d5a5e1b1671d9c3c627b3fc045
7
- data.tar.gz: d8b73ba874184015ff2fc59a79fb011b25b4b0ba7773938cb169fd3da5373e8701f36f62b5477f84dbd188c29a2daa29cc06a64d1f8fb2e575dcc6ffa35820d6
6
+ metadata.gz: 28348bb09ea2d40d4e2df95bb10f518602061b2be76b7301f9cfe43d67a4649259b0871dbd971243ab2887c56cc587498670d39fb4555b66744ad5ee32bc7532
7
+ data.tar.gz: b495b45a9bdcecf5f8d99458b984355360f555ee01a380c0d7b230619e60cf29d56206dbace91141383fc4e2b1cd0aa33ca2b8530947cfe8604886b9b7b4a196
@@ -5,10 +5,10 @@ jobs:
5
5
  strategy:
6
6
  fail-fast: false
7
7
  matrix:
8
- gemfile: [ activerecord_7.2, activerecord_8.0, activerecord_8.1 ]
8
+ gemfile: [ activerecord_8.1 ]
9
9
  ruby: [ 3.3, 3.4, "4.0" ]
10
10
 
11
- runs-on: ubuntu-22.04
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
@@ -1,12 +1,3 @@
1
- appraise "activerecord-7.2" do
2
- gem "activerecord", "~>7.2.0"
3
- end
4
-
5
- appraise "activerecord-8.0" do
6
- gem "activerecord", "~>8.0.0"
7
- end
8
-
9
1
  appraise "activerecord-8.1" do
10
2
  gem "activerecord", "~>8.1.0"
11
3
  end
12
-
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
- raise NotImplementedError if database.postgresql?
19
- SanitizedDatabase.new(skip_columns, path).dump do |tables|
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
- bash_sh "#{mysqldump} --no-create-db --single-transaction --quick -e #{skip_table_options} #{database.to_mysql_options} #{database.name} | #{pipe} #{target}"
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
- skip_tables.map do |table|
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
- class SanitizedDatabase < Struct.new(:config, :path)
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
- with_sanitized_tables do
85
- yield skip_tables
86
- end
87
- skip_tables.each do |table|
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 skip_tables
95
- config.keys
100
+ def tables
101
+ (@skip_columns.keys + @scrub_columns.keys).uniq
96
102
  end
97
103
 
98
- def with_sanitized_tables
99
- ActiveRecord::Base.transaction do
100
- config.each do |table, columns|
101
- sanitized_table = "sanitized_#{table}"
102
- sql <<-SQL
103
- DROP TABLE IF EXISTS `#{sanitized_table}`;
104
- CREATE TABLE `#{sanitized_table}` LIKE `#{table}`;
105
- INSERT INTO `#{sanitized_table}` SELECT * FROM `#{table}`;
106
- ALTER TABLE `#{sanitized_table}` #{columns.map { |column| "DROP `#{column}`" }.join(", ")};
107
- SQL
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
- def sql queries
119
- queries.split(";").select(&:present?).each do |query|
120
- ActiveRecord::Base.connection.execute query
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
@@ -1,3 +1,3 @@
1
1
  module Backhoe
2
- VERSION = "0.11.0"
2
+ VERSION = "0.12.0"
3
3
  end
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.11.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: 2026-02-27 00:00:00.000000000 Z
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: 3.6.2
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: []
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~>7.2.0"
6
-
7
- gemspec path: "../"
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~>8.0.0"
6
-
7
- gemspec path: "../"