idata 0.1.22 → 0.1.23

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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/ivalidate +27 -15
  3. data/lib/idata/version.rb +1 -1
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e01ecdfe5b0d94ddb4f503305f73177fcbeabc4
4
- data.tar.gz: 0a053ddd16efb8b99f91a967a99a448dcd878e64
3
+ metadata.gz: f09fd4a5e2e638192e76b3ca2b703a78a2c8f53b
4
+ data.tar.gz: c23665e7c90d463a7b9df4fec2d1ef64c794ea9b
5
5
  SHA512:
6
- metadata.gz: 334419ae0fbbfeebf3b3db0b91381007d78e9f4fb162346a6edb7755b0fe57aca5dd059e6cc74ed9d16241087ed9afe1f4c6b8e3d11c05a55faaa6f47a51ee9c
7
- data.tar.gz: f4468fde70bb3ac1ff0218bc6d7d1392e23f3d67837358667460ebd0a54408adc74ee759a4a356a0133fdf9a022dfd9df0ab658b79443bddcacea4b76586d9f1
6
+ metadata.gz: 548ecde59f31bdc4afc5eb3bf674ffc858c19c1bc78f36f42a60a4e07d80e9cc103965fec1353718fba8254222f50dc4e363aa5aa811da47e9471b78f7e29ebe
7
+ data.tar.gz: 24f64401382182e5fb549d962ca7e9bac9ea403ff7ce447b0d353b5ccfe41f88229c6aff54a98ff12212b6d137c7dcba1b134e16838984b9966064585d4957ef
data/bin/ivalidate CHANGED
@@ -25,7 +25,7 @@ $options = {
25
25
  :cross_reference => [],
26
26
  :query => [],
27
27
  :rquery => [],
28
- :unique_by => []
28
+ :consistent_by => []
29
29
  }
30
30
  parser = OptionParser.new("", 24) do |opts|
31
31
  opts.banner = "\nProgram: Data Validator\nAuthor: MCKI\n\n"
@@ -34,10 +34,10 @@ parser = OptionParser.new("", 24) do |opts|
34
34
  $options[:unique] << v
35
35
  end
36
36
 
37
- opts.on("--unique-by FIELD1|FIELD2", "Check if FIELD1 is unique within the scope of FIELD2") do |v|
38
- $options[:unique_by] << v
37
+ opts.on("--consistent-by F1|F2,F3,F4...", "") do |v|
38
+ $options[:consistent_by] << v
39
39
  end
40
-
40
+
41
41
  opts.on("--not-null FIELD", "Check if FIELD is not null or empty") do |v|
42
42
  $options[:not_null] << v
43
43
  end
@@ -164,6 +164,18 @@ ActiveRecord::Base.establish_connection(
164
164
  'timeout' => 15000
165
165
  )
166
166
 
167
+ class String
168
+ def not_null_sql
169
+ a = self.split(/\s*,\s*/)
170
+ sql = a.map{|s|
171
+ "#{s} IS NOT NULL AND length(trim(#{s}::text)) <> 0"
172
+ }.join(" AND ")
173
+
174
+ "(#{sql})"
175
+ end
176
+ end
177
+
178
+
167
179
  puts "\nValidating #{$options[:table]}"
168
180
  puts "------------------------------------------"
169
181
 
@@ -193,11 +205,11 @@ $options[:unique].each do |field|
193
205
  puts "Checking uniqueness: #{field}"
194
206
 
195
207
  uniq_sql = <<-eos
196
- UPDATE #{$options[:table]} SET #{$options[:log_to]} = array_to_string(string_to_array(#{$options[:log_to]}, ' || ') || string_to_array('#{field} is not unique', ' || '), ' || ')
197
- WHERE #{field} IN (
198
- SELECT #{field} FROM #{$options[:table]} GROUP BY #{field}
208
+ UPDATE #{$options[:table]} SET #{$options[:log_to]} = array_to_string(string_to_array(#{$options[:log_to]}, ' || ') || string_to_array('[#{field}] is not unique', ' || '), ' || ')
209
+ WHERE id IN (
210
+ SELECT unnest(array_agg(id)) FROM #{$options[:table]} GROUP BY #{field}
199
211
  HAVING count(*) > 1
200
- ) AND #{field} IS NOT NULL AND length(trim(#{field})) <> 0;
212
+ ) AND #{field.not_null_sql};
201
213
  eos
202
214
 
203
215
  ActiveRecord::Base.connection.execute(uniq_sql)
@@ -207,9 +219,9 @@ $options[:unique].each do |field|
207
219
  end
208
220
 
209
221
  # --------------------------------------------------------------------
210
- # Check unique within scope
222
+ # Check consitent by scope
211
223
  # --------------------------------------------------------------------
212
- $options[:unique_by].each do |fields|
224
+ $options[:consistent_by].each do |fields|
213
225
  begin
214
226
  fields = fields.split(/\s*\|\s*/)
215
227
 
@@ -221,13 +233,14 @@ $options[:unique_by].each do |fields|
221
233
  puts "Checking uniqueness: #{f1} | #{f2}"
222
234
 
223
235
  uniq_sql = <<-eos
224
- UPDATE #{$options[:table]} SET #{$options[:log_to]} = array_to_string(string_to_array(#{$options[:log_to]}, ' || ') || string_to_array('same #{f2} but with different #{f1}', ' || '), ' || ')
225
- WHERE #{f2} IN
236
+ UPDATE #{$options[:table]} SET #{$options[:log_to]} = array_to_string(string_to_array(#{$options[:log_to]}, ' || ') || string_to_array('same [#{f2}] but with different #{f1}', ' || '), ' || ')
237
+ WHERE id IN
226
238
  (
227
- SELECT #{f2} FROM #{$options[:table]}
239
+ SELECT unnest(array_agg(id)) FROM #{$options[:table]}
240
+ WHERE #{f1.not_null_sql} AND #{f2.not_null_sql}
228
241
  GROUP BY #{f2}
229
242
  HAVING COUNT(distinct #{f1}) > 1
230
- ) AND #{f1} IS NOT NULL AND length(trim(#{f1})) <> 0 AND #{f2} IS NOT NULL AND length(trim(#{f2})) <> 0;
243
+ );
231
244
  eos
232
245
 
233
246
  ActiveRecord::Base.connection.execute(uniq_sql)
@@ -236,7 +249,6 @@ $options[:unique_by].each do |fields|
236
249
  end
237
250
  end
238
251
 
239
-
240
252
  # --------------------------------------------------------------------
241
253
  # Check not-null field
242
254
  # --------------------------------------------------------------------
data/lib/idata/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Idata
2
- VERSION = "0.1.22"
2
+ VERSION = "0.1.23"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: idata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.22
4
+ version: 0.1.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nghi Pham
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-24 00:00:00.000000000 Z
11
+ date: 2014-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler