db_subsetter 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/lib/db_subsetter/exporter.rb +30 -11
- data/lib/db_subsetter/filter.rb +2 -0
- data/lib/db_subsetter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99f915db184481361de79ea28709bf1a13737c89
|
4
|
+
data.tar.gz: c916a1d6f148331a4565d4d16c030a26739384c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 594062ff2d049b7142d441cb6a208744fc27282bcc1ff912973f692cf4654614baf7245021001b72dd3cebeb1ad281cd63a0e75db09dc4d48a48f5a59989b1b4
|
7
|
+
data.tar.gz: 58a8e4bc3cbf6cdf19a4d9fade8d7cd645d7008025d7e47754afb0df27cae8c9e29f15c45fbc56955fecd53755faaf9447d1d8f6f60db932d432fa794fe5dd05
|
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'sqlite3'
|
2
|
+
require 'active_record'
|
2
3
|
|
3
4
|
module DbSubsetter
|
4
5
|
class Exporter
|
5
|
-
attr_writer :filter
|
6
|
+
attr_writer :filter, :max_unfiltered_rows, :max_filtered_rows
|
6
7
|
|
7
8
|
def all_tables
|
8
9
|
ActiveRecord::Base.connection.tables
|
@@ -26,15 +27,21 @@ module DbSubsetter
|
|
26
27
|
end
|
27
28
|
end
|
28
29
|
|
29
|
-
def verify_exportability
|
30
|
-
|
31
|
-
|
30
|
+
def verify_exportability(verbose = true)
|
31
|
+
puts "Verifying table exportability ...\n\n" if verbose
|
32
|
+
errors = tables.map{|x| verify_table_exportability(x) }.flatten.compact
|
33
|
+
if errors.count > 0
|
34
|
+
puts errors.join("\n")
|
35
|
+
raise ArgumentError.new "Some tables are not exportable"
|
32
36
|
end
|
37
|
+
puts "\n\n" if verbose
|
33
38
|
end
|
34
39
|
|
35
|
-
def export(filename)
|
36
|
-
|
40
|
+
def export(filename, verbose = true)
|
41
|
+
@verbose = verbose
|
42
|
+
verify_exportability(verbose)
|
37
43
|
|
44
|
+
puts "Exporting data...\n\n" if @verbose
|
38
45
|
@output = SQLite3::Database.new(filename)
|
39
46
|
@output.execute("CREATE TABLE tables (name TEXT, records_exported INTEGER, columns TEXT)")
|
40
47
|
tables.each do |table|
|
@@ -45,8 +52,12 @@ module DbSubsetter
|
|
45
52
|
|
46
53
|
|
47
54
|
private
|
48
|
-
def
|
49
|
-
|
55
|
+
def max_unfiltered_rows
|
56
|
+
@max_unfiltered_rows || 1000
|
57
|
+
end
|
58
|
+
|
59
|
+
def max_filtered_rows
|
60
|
+
@max_filtered_rows || 2000
|
50
61
|
end
|
51
62
|
|
52
63
|
def insert_batch_size
|
@@ -80,8 +91,11 @@ module DbSubsetter
|
|
80
91
|
end
|
81
92
|
|
82
93
|
def verify_table_exportability(table)
|
83
|
-
|
84
|
-
|
94
|
+
puts "Verifying: #{table}" if @verbose
|
95
|
+
errors = []
|
96
|
+
errors << "ERROR: Multiple pages but no primary key on: #{table}" if pages(table) > 1 && order_by(table).blank?
|
97
|
+
errors << "ERROR: Too many rows in: #{table} (#{filtered_row_count(table)})" if( filtered_row_count(table) > max_filtered_rows )
|
98
|
+
errors
|
85
99
|
end
|
86
100
|
|
87
101
|
def cleanup_types(row)
|
@@ -95,10 +109,12 @@ module DbSubsetter
|
|
95
109
|
end
|
96
110
|
|
97
111
|
def export_table(table)
|
112
|
+
print "Exporting: #{table} (#{pages(table)} pages)" if @verbose
|
113
|
+
$stdout.flush if @verbose
|
98
114
|
columns = ActiveRecord::Base.connection.columns(table).map{ |table| table.name }
|
99
115
|
rows_exported = 0
|
100
116
|
@output.execute("CREATE TABLE #{table.underscore} ( data TEXT )")
|
101
|
-
for i in 0..pages(table)
|
117
|
+
for i in 0..(pages(table) - 1)
|
102
118
|
arel_table = query = Arel::Table.new(table, ActiveRecord::Base)
|
103
119
|
query = filter.filter(table, query)
|
104
120
|
# Need to extend this to take more than the first batch_size records
|
@@ -111,7 +127,10 @@ module DbSubsetter
|
|
111
127
|
@output.execute("INSERT INTO #{table.underscore} (data) VALUES #{ Array.new(rows.size){"(?)"}.join(",")}", rows.map{|x| cleanup_types(x)}.map(&:to_json) )
|
112
128
|
rows_exported += rows.size
|
113
129
|
end
|
130
|
+
print "." if @verbose
|
131
|
+
$stdout.flush if @verbose
|
114
132
|
end
|
133
|
+
puts "" if @verbose
|
115
134
|
@output.execute("INSERT INTO tables VALUES (?, ?, ?)", [table, rows_exported, columns.to_json])
|
116
135
|
end
|
117
136
|
end
|
data/lib/db_subsetter/filter.rb
CHANGED
data/lib/db_subsetter/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: db_subsetter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Francis
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-10-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|