db_subsetter 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 99f915db184481361de79ea28709bf1a13737c89
4
- data.tar.gz: c916a1d6f148331a4565d4d16c030a26739384c6
3
+ metadata.gz: 3cbd626cd24132c2e8db14c89ecb66c300350886
4
+ data.tar.gz: f7171ef20aded756e45b11a04c5072ab0303e1ec
5
5
  SHA512:
6
- metadata.gz: 594062ff2d049b7142d441cb6a208744fc27282bcc1ff912973f692cf4654614baf7245021001b72dd3cebeb1ad281cd63a0e75db09dc4d48a48f5a59989b1b4
7
- data.tar.gz: 58a8e4bc3cbf6cdf19a4d9fade8d7cd645d7008025d7e47754afb0df27cae8c9e29f15c45fbc56955fecd53755faaf9447d1d8f6f60db932d432fa794fe5dd05
6
+ metadata.gz: dd3d89c74237f6a7c942bfb72561e3eaef426e34c3919f1c0b6651287e76650bebec068eb1b5b81c1dcf2c0d7959c43e1b5682be51ba6f21b62234afab1d4107
7
+ data.tar.gz: d9d8f8a4c5483e12fb9fe10cfee561c656fa67205c3968cc2c2cac6ca281ab7d76d9c8787b13534445434a0939f6477102ff7b2318c9e5e72effc282936280c1
data/db_subsetter.gemspec CHANGED
@@ -27,5 +27,6 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.add_dependency "activerecord", "~> 4.2"
29
29
  spec.add_dependency "sqlite3", "~> 1.3"
30
+ spec.add_dependency "random-word"
30
31
  end
31
32
 
@@ -49,7 +49,13 @@ module DbSubsetter
49
49
  end
50
50
  end
51
51
 
52
+ def add_scrambler(scrambler)
53
+ @scramblers << scrambler
54
+ end
52
55
 
56
+ def initialize
57
+ @scramblers = []
58
+ end
53
59
 
54
60
  private
55
61
  def max_unfiltered_rows
@@ -60,6 +66,7 @@ module DbSubsetter
60
66
  @max_filtered_rows || 2000
61
67
  end
62
68
 
69
+ # this is the batch size we insert into sqlite, which seems to be a reasonable balance of speed and memory usage
63
70
  def insert_batch_size
64
71
  250
65
72
  end
@@ -108,6 +115,13 @@ module DbSubsetter
108
115
  end
109
116
  end
110
117
 
118
+ def scramble_data(table, data)
119
+ @scramblers.each do |scrambler|
120
+ data = scrambler.scramble(table, data)
121
+ end
122
+ data
123
+ end
124
+
111
125
  def export_table(table)
112
126
  print "Exporting: #{table} (#{pages(table)} pages)" if @verbose
113
127
  $stdout.flush if @verbose
@@ -124,7 +138,7 @@ module DbSubsetter
124
138
 
125
139
  records = ActiveRecord::Base.connection.select_rows( sql )
126
140
  records.each_slice(insert_batch_size) do |rows|
127
- @output.execute("INSERT INTO #{table.underscore} (data) VALUES #{ Array.new(rows.size){"(?)"}.join(",")}", rows.map{|x| cleanup_types(x)}.map(&:to_json) )
141
+ @output.execute("INSERT INTO #{table.underscore} (data) VALUES #{ Array.new(rows.size){"(?)"}.join(",")}", rows.map{|x| scramble_data(table, cleanup_types(x))}.map(&:to_json) )
128
142
  rows_exported += rows.size
129
143
  end
130
144
  print "." if @verbose
@@ -0,0 +1,28 @@
1
+ require 'random-word'
2
+
3
+ module DbSubsetter
4
+ class Scrambler
5
+ def scramble(table, row)
6
+ scramble_method = "scramble_#{table.downcase}"
7
+ if self.respond_to? scramble_method
8
+ self.send(scramble_method, row)
9
+ else
10
+ row
11
+ end
12
+ end
13
+
14
+ def initialize
15
+ @column_index_cache = {}
16
+ end
17
+
18
+ protected
19
+ def scramble_column(table, column, row_data, value)
20
+ row_data[column_index(table, column)] = value
21
+ end
22
+
23
+ private
24
+ def column_index(table, column)
25
+ @column_index_cache["#{table}##{column}"] ||= ActiveRecord::Base.connection.columns(table).map{|table| table.name}.index(column.to_s)
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module DbSubsetter
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/db_subsetter.rb CHANGED
@@ -2,6 +2,7 @@ require "db_subsetter/version"
2
2
  require "db_subsetter/filter"
3
3
  require "db_subsetter/exporter"
4
4
  require "db_subsetter/importer"
5
+ require "db_subsetter/scrambler"
5
6
  require "db_subsetter/dialect/generic"
6
7
  require "db_subsetter/dialect/my_sql"
7
8
  require "db_subsetter/dialect/ms_sql"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_subsetter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Francis
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: random-word
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description:
84
98
  email:
85
99
  - joe@lostapathy.com
@@ -103,6 +117,7 @@ files:
103
117
  - lib/db_subsetter/exporter.rb
104
118
  - lib/db_subsetter/filter.rb
105
119
  - lib/db_subsetter/importer.rb
120
+ - lib/db_subsetter/scrambler.rb
106
121
  - lib/db_subsetter/version.rb
107
122
  homepage: https://github.com/lostapathy/db_subsetter
108
123
  licenses: