mass_record 0.0.2 → 0.0.3

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: 5eaa64bad4860bb2f6d3630a437f539b02e94598
4
- data.tar.gz: c46e691401efbe81bd591d98eceda9166edc1f27
3
+ metadata.gz: aa0932cb1a7cca3be43da636721f64a1bf561f99
4
+ data.tar.gz: 5a06ac8edb6dc364b469272afbb74195e174567c
5
5
  SHA512:
6
- metadata.gz: 33bfc72ba9cc48048035faa29702ea843fe50e3dcece44c5017c683fd92876bd665a2878c81c6f5d666ccf6e369c850fea9f6015e54b00d31703112ebf96460e
7
- data.tar.gz: 2c35b274cf2f33e2c5971dadf0d1926679956781a9f192397a48577c1c1cb15ade952f9692f300516d929d85a082c27c95c9953d2feca1c67c133a8819b35bd9
6
+ metadata.gz: 4563ff2538f3580206787a73b93d43d798267099400b48806e32b5a29ea94e80bd4ee3d22c44975e481503d07490cb2d672619dfd07bbfbc5e444f68a57ac326
7
+ data.tar.gz: 784a6ed6e6747e55111289496e0e04169f963095e01f3162b2b8c4b72a77c37cf7c20fb1e19af9fd63dfacbcded5221cb835e042a47f7b5562b5a8c97b046a92
@@ -1,3 +1,3 @@
1
1
  module MassRecord
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/mass_record.rb CHANGED
@@ -51,13 +51,18 @@ module MassRecord
51
51
  }
52
52
 
53
53
  object_array = [object_array] unless object_array.is_a? Array
54
+ return false if object_array.blank?
55
+
54
56
  queue = []
55
57
 
56
58
  object_array.each do |object|
57
- queue << {key[:table] => object.class.table_name, key[:operation] => operation, key[:object] => object} unless object.blank?
59
+ queue << {key[:table] => object.class.name, key[:operation] => operation, key[:object] => object} unless object.blank?
58
60
  end
59
-
60
- File.open(folder[:queued]+"/#{operation.to_s}_#{file_tag}.json",'w'){|f| f.write queue.to_json}
61
+ # begin
62
+ File.open(folder[:queued]+"/#{operation.to_s}_#{file_tag}.json",'w'){|f| f.write queue.to_json}
63
+ # rescue Exception => e
64
+ # pp "#{e.message}\n#{e.backtrace[0..5].pretty_inspect}".red
65
+ # end
61
66
  end
62
67
 
63
68
  def execute_queued_queries key:{
@@ -117,7 +122,7 @@ module MassRecord
117
122
  # close database connection
118
123
 
119
124
  # move to appropriate folder and remove '.processing' from the filename
120
- errors_present = errors.any?{|op,tables| tables.has_key? :run_time or tables.any?{|table,col_sets| !table.blank?}}
125
+ errors_present = errors.any?{|op,tables| tables.has_key? :run_time or tables.any?{|table,col_sets| !col_sets.blank?}}
121
126
  errored_objects = collect_errored_objects found_in:errors, from:json_objects, key:key, synonyms:synonyms if errors_present
122
127
 
123
128
  individual_errors = errors_present ? (query_per_object errored_objects, key:key, synonyms:synonyms) : []
@@ -213,7 +218,8 @@ module MassRecord
213
218
  return {} if from.blank? or for_table.blank?
214
219
  table = for_table
215
220
  hashes = from.select{|o| o[key[:table]] == table}.collect{|x| x[key[:object]]}
216
- model = table.classify.constantize
221
+ model = get_model from:for_table
222
+ connection = model.connection
217
223
  pk = model.primary_key
218
224
 
219
225
  # organize hashes based on whether they exist (based on their primary key(s)) in the table or not
@@ -223,16 +229,16 @@ module MassRecord
223
229
  ids.each do |id|
224
230
  equivalence_clauses = []
225
231
  id.each do |k,v|
226
- equivalence_clauses << "#{ActiveRecord::Base.connection.quote_column_name k} = #{ActiveRecord::Base.connection.quote(ActiveRecord::Base.connection.type_cast(v, model.column_types[k]))}"
232
+ equivalence_clauses << "#{k} = #{connection.quote(connection.type_cast(v, model.column_types[k]))}"
227
233
  end
228
234
  where_clauses << "(#{equivalence_clauses.join ' and '})"
229
235
  end
230
- existing_id_sets = ActiveRecord::Base.connection.execute("SELECT #{pk.join ', '} FROM #{table} WHERE #{where_clauses.join ' OR '}").collect{|x| Hash[x.map.with_index{|x,i| [pk[i],x]}]}
236
+ existing_id_sets = model.find_by_sql("SELECT #{pk.join ', '} FROM #{model.table_name} WHERE #{where_clauses.join ' OR '}").collect{|x| x.attributes} #.collect{|x| Hash[x.map.with_index{|x,i| [pk[i],x]}]}
231
237
  insert_hashes = hashes.reject{|h| existing_id_sets.any?{|set| h == h.merge(set)}}
232
238
  update_hashes = hashes.select{|h| existing_id_sets.any?{|set| h == h.merge(set)}}
233
239
  else
234
240
  ids = hashes.reject{|x| x[pk].blank?}.collect{|x| x[pk]} # should not include null values
235
- existing_ids = ActiveRecord::Base.connection.execute("SELECT #{pk} FROM #{table} WHERE #{pk} in ('#{ids.join "','"}')").collect{|x| x.first.to_s}
241
+ existing_ids = model.find_by_sql("SELECT #{pk} FROM #{model.table_name} WHERE #{pk} in ('#{ids.join "','"}')").collect{|x| x[pk]} # for some reason model.connection.execute returns the count
236
242
  insert_hashes = hashes.reject{|x| existing_ids.include? x[pk].to_s}
237
243
  update_hashes = hashes.select{|x| existing_ids.include? x[pk].to_s}
238
244
  end
@@ -250,7 +256,7 @@ module MassRecord
250
256
  sorted_hashes = sort_save_operations from:json_objects, for_table:table, key:key
251
257
 
252
258
  # perform the appropriate operations
253
- model = table.classify.constantize
259
+ model = get_model from:table
254
260
  errors += update sorted_hashes[:update], into:model unless sorted_hashes[:update].blank?
255
261
  errors += insert sorted_hashes[:insert], into:model unless sorted_hashes[:insert].blank?
256
262
  end
@@ -275,9 +281,19 @@ module MassRecord
275
281
  end
276
282
  end
277
283
 
284
+ def get_model from:nil
285
+ return from if from.is_a? Class
286
+ if from.is_a? String
287
+ model = from.constantize rescue nil
288
+ model = from.classify.constantize rescue nil if model.blank?
289
+ return model
290
+ end
291
+ return nil
292
+ end
293
+
278
294
  def sql_for_insert hash, into:nil
279
295
  return nil if hash.blank? or into.blank?
280
- model = into.is_a?(String) ? into.classify.constantize : into
296
+ model = get_model from:into
281
297
  id_column_name = model.primary_key
282
298
  created_at = model.attribute_alias?("created_at") ? model.attribute_alias("created_at") : "created_at"
283
299
  updated_at = model.attribute_alias?("updated_at") ? model.attribute_alias("updated_at") : "updated_at"
@@ -288,7 +304,8 @@ module MassRecord
288
304
  # assemble an individual query
289
305
  im = Arel::InsertManager.new(ActiveRecord::Base)
290
306
  unless id_column_name.is_a? Array # don't modify the id fields if there are concatenated primary keys
291
- h.delete id_column_name if model.columns.select{|x| x.name == id_column_name}.first.extra == 'auto_increment' or h[id_column_name].blank?
307
+ database_column = model.columns.select{|x| x.name == id_column_name}.first
308
+ h.delete id_column_name if h[id_column_name].blank? or (database_column.methods.include? :extra and database_column.extra == 'auto_increment')
292
309
  end
293
310
  h = convert_to_db_format h, model:model, created_at:created_at, updated_at:updated_at
294
311
  pairs = h.collect do |k,v|
@@ -300,7 +317,7 @@ module MassRecord
300
317
 
301
318
  def sql_for_update hash, into:nil
302
319
  return nil if hash.blank? or into.blank?
303
- model = into.is_a?(String) ? into.classify.constantize : into
320
+ model = get_model from:into
304
321
  id_column_name = model.primary_key
305
322
  created_at = model.attribute_alias?("created_at") ? model.attribute_alias("created_at") : "created_at"
306
323
  updated_at = model.attribute_alias?("updated_at") ? model.attribute_alias("updated_at") : "updated_at"
@@ -327,14 +344,15 @@ module MassRecord
327
344
  begin
328
345
  return false if hashes.blank? or into.blank?
329
346
  hashes = [hashes] unless hashes.is_a? Array
347
+ model = get_model from:into
330
348
 
331
349
  errors = []
332
350
  # create an array of single insert queries
333
351
  hashes.each do |hash|
334
- sql = sql_for_insert hash, into:into
352
+ sql = sql_for_insert hash, into:model
335
353
 
336
354
  begin
337
- query sql
355
+ query sql, connection:model
338
356
  rescue Exception => e
339
357
  puts e.message
340
358
  errors << IndividualError.new(e,table:into,operation:"update",json_object:hash)
@@ -350,21 +368,20 @@ module MassRecord
350
368
  begin
351
369
  return false if hashes.blank? or into.blank?
352
370
  hashes = [hashes] unless hashes.is_a? Array
371
+ model = get_model from:into
353
372
 
354
373
  errors = []
355
374
  # create an array of single insert queries
356
375
  hashes.each do |hash|
357
- sql = sql_for_insert hash, into:into
376
+ sql = sql_for_insert hash, into:model
358
377
 
359
378
  begin
360
- query sql
379
+ query sql, connection:model
361
380
  rescue Exception => e
362
381
  puts e.message
363
382
  errors << IndividualError.new(e,table:into,operation:"insert",json_object:hash)
364
383
  end
365
384
  end
366
-
367
- # reparse the queries and execute them
368
385
  return errors
369
386
  rescue Exception => e
370
387
  return (defined? errors) ? (errors << IndividualError.new(e, table:into, operation:"insert")) : [IndividualError.new( e, table:into, operation:"insert")]
@@ -405,7 +422,7 @@ module MassRecord
405
422
  sorted_hashes = sort_save_operations from:json_objects, for_table:table, key:key
406
423
 
407
424
  # perform the appropriate operations
408
- model = table.classify.constantize
425
+ model = get_model from:table
409
426
  errors[table.to_sym] = {}
410
427
  errors[table.to_sym].merge! mass_update sorted_hashes[:update], into:model unless sorted_hashes[:update].blank?
411
428
  errors[table.to_sym].merge! mass_insert sorted_hashes[:insert], into:model unless sorted_hashes[:insert].blank?
@@ -422,7 +439,7 @@ module MassRecord
422
439
  begin
423
440
  return false if hashes.blank? or into.blank?
424
441
 
425
- model = into.is_a?(String) ? into.classify.constantize : into
442
+ model = get_model from:into
426
443
  id_column_name = model.primary_key
427
444
  created_at = model.attribute_alias?("created_at") ? model.attribute_alias("created_at") : "created_at"
428
445
  updated_at = model.attribute_alias?("updated_at") ? model.attribute_alias("updated_at") : "updated_at"
@@ -492,12 +509,12 @@ module MassRecord
492
509
  set_columns = []
493
510
 
494
511
  set_fragments.each do |column, values|
495
- set_columns << "#{column} = CASE #{model.table_name}.#{id_column_name} #{values.join ' '} END" unless id_column_name.is_a? Array
496
- set_columns << "#{column} = CASE #{values.join ' '} END" if id_column_name.is_a? Array
512
+ set_columns << "#{column} = CASE #{model.table_name}.#{id_column_name} #{values.join ' ' } WHEN 'findabetterwaytodothis' THEN '0' END" unless id_column_name.is_a? Array #TODO: ugly hack, find a better solution
513
+ set_columns << "#{column} = CASE #{values.join ' '} WHEN 1=0 THEN '0' END" if id_column_name.is_a? Array
497
514
  end
498
515
 
499
516
  begin
500
- query "#{update} #{set_columns.join ', '} #{where}"
517
+ query "#{update} #{set_columns.join ', '} #{where}", connection:model
501
518
  rescue Exception => e
502
519
  puts e.message
503
520
  errors[column_set] = e
@@ -535,7 +552,7 @@ module MassRecord
535
552
  return false if hashes.blank? or into.blank?
536
553
 
537
554
  # create an array of single insert queries
538
- model = into.is_a?(String) ? into.classify.constantize : into
555
+ model = get_model from:into
539
556
  concentrated_queries = {}
540
557
 
541
558
  hashes.each do |hash|
@@ -557,7 +574,7 @@ module MassRecord
557
574
  # reparse the queries and execute them
558
575
  concentrated_queries.each do |column_set,clauses|
559
576
  begin
560
- query "#{clauses[:into]} VALUES #{clauses[:values].join(", ")}"
577
+ query "#{clauses[:into]} VALUES #{clauses[:values].join(", ")}", connection:model
561
578
  rescue Exception => e
562
579
  puts e.message
563
580
  errors[column_set] = e
@@ -592,12 +609,16 @@ module MassRecord
592
609
  return json_object
593
610
  end
594
611
 
595
- def query sql
596
- if database_connection.blank?
612
+ def query sql, connection:database_connection
613
+ sql = sql.gsub /`(.*?)`/,'\1' # some queries don't like the "`"s
614
+ if connection.blank? # a blank value was passed in or the cached connection is empty
597
615
  res = ActiveRecord::Base.connection.execute sql
598
616
  ActiveRecord::Base.connection.close
617
+ elsif connection.is_a? Class and connection.ancestors.include? ActiveRecord::Base # an ActiveRecord Class was passed in
618
+ connection.connection.execute sql
619
+ connection.connection.close
599
620
  else
600
- res = database_connection.execute sql
621
+ res = connection.execute sql
601
622
  end
602
623
 
603
624
  return res
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mass_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Hanna
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-21 00:00:00.000000000 Z
11
+ date: 2014-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: colorize
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: sqlite3
29
43
  requirement: !ruby/object:Gem::Requirement