db_subsetter 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: 0513f9d6c747b0a4b59915680c0f753f585c7176
4
- data.tar.gz: dd987be888e834ffa1e2f832eed209206eb791e0
3
+ metadata.gz: 99f915db184481361de79ea28709bf1a13737c89
4
+ data.tar.gz: c916a1d6f148331a4565d4d16c030a26739384c6
5
5
  SHA512:
6
- metadata.gz: 4c1bf06867d598238213377d5c8e8c1f47aa9736b8136038e4e49a8f34fa6f113fae065df8b798914824c0329b21baf4763f8e1b09437dafd8e7889af84dea75
7
- data.tar.gz: faa183feff6d80f767310170a9dce02612baaa9816cb07225869da55585c9ab05d75d2af17047c9b7ba8e298ac80d4df5c56be7497ff9718b498a450027ebdee
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
- tables.each do |table|
31
- verify_table_exportability(table)
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
- verify_exportability
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 max_rows
49
- 10000000
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
- raise "ERROR: Multiple pages but no primary key on: #{table}" if pages(table) > 1 && order_by(table).blank?
84
- raise "ERROR: Too many rows in: #{table} (#{filtered_row_count(table)})" if( filtered_row_count(table) > max_rows )
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
@@ -1,3 +1,5 @@
1
+ require 'active_record'
2
+
1
3
  module DbSubsetter
2
4
  class Filter
3
5
  attr_writer :exporter
@@ -1,3 +1,3 @@
1
1
  module DbSubsetter
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-07-15 00:00:00.000000000 Z
11
+ date: 2016-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler