abstract_importer 1.0.0 → 1.1.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: 3f5a316e4e45d1c7a09af7b4930dda62e690d5c8
4
- data.tar.gz: 333effaf5481100cd62f0f7523161747e923d515
3
+ metadata.gz: 390699648f23aa027bf392f0f9095bf4b6801e3b
4
+ data.tar.gz: 5f1d0ccb8c4db21649913fa3914b7f6a4f0ba22c
5
5
  SHA512:
6
- metadata.gz: a49953a3ecee96971314712785578a4b4fb1c09a2461b02448995fe1c3589f471e158c87c6e054aa613386a5ecaa064fd49a03e009d881bc5d29a6702dd23697
7
- data.tar.gz: a18398b96c81a7f8f8ecb469c76fa06b7513f534a2c7d6c9b515479c76209161f08e844bc1b7bbf768881dc8b060730aaf2e7d47b44d44a6248d1087c24887fc
6
+ metadata.gz: 1081c7623c65fcbd0748385d6e30f139dcf0d33703ace0c172e9b3e2d9a07aac3cabff0de5f600dc40dc0de7d28af5de2d0865960252888bb5b101711c8ed252
7
+ data.tar.gz: b59ab4abc7f8c9634e00c24840a01dfe0974f5b7f57e8f34b57129ad10e8d7f0c226972c3323b67107b3aeec6741b2f9e7aaf072ccc8515dbae308c59ca8072b
@@ -0,0 +1,8 @@
1
+ # .travis.yml
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ env:
6
+ - DB=sqlite
7
+ script:
8
+ - bundle exec rake test
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # AbstractImporter
2
2
 
3
+ [![Build Status](https://travis-ci.org/concordia-publishing-house/abstract_importer.png?branch=master)](https://travis-ci.org/concordia-publishing-house/abstract_importer)
4
+
5
+ [![Code Climate](https://codeclimate.com/github/concordia-publishing-house/abstract_importer.png)](https://codeclimate.com/github/concordia-publishing-house/abstract_importer)
6
+
3
7
  AbstractImporter provides services for importing complex data from an arbitrary data source. It:
4
8
 
5
9
  * Preserves relationships between tables that are imported as a set
@@ -4,6 +4,7 @@ require 'abstract_importer/reporter'
4
4
  require 'abstract_importer/collection'
5
5
  require 'abstract_importer/collection_importer'
6
6
  require 'abstract_importer/id_map'
7
+ require 'abstract_importer/summary'
7
8
 
8
9
 
9
10
  module AbstractImporter
@@ -28,12 +29,12 @@ module AbstractImporter
28
29
  @dry_run = options.fetch(:dry_run, false)
29
30
 
30
31
  @id_map = IdMap.new
31
- @summary = {}
32
+ @results = {}
32
33
  @import_plan = self.class.import_plan.to_h
33
34
  @collections = []
34
35
  end
35
36
 
36
- attr_reader :source, :parent, :reporter, :id_map, :summary
37
+ attr_reader :source, :parent, :reporter, :id_map, :results
37
38
 
38
39
  def dry_run?
39
40
  @dry_run
@@ -57,7 +58,7 @@ module AbstractImporter
57
58
 
58
59
  teardown
59
60
  reporter.finish_all(self, ms)
60
- @summary
61
+ results
61
62
  end
62
63
 
63
64
  def setup
@@ -68,7 +69,7 @@ module AbstractImporter
68
69
  end
69
70
 
70
71
  def import_collection(collection)
71
- @summary[collection.name] = CollectionImporter.new(self, collection).perform!
72
+ results[collection.name] = CollectionImporter.new(self, collection).perform!
72
73
  end
73
74
 
74
75
  def teardown
@@ -30,7 +30,7 @@ module AbstractImporter
30
30
  reporter.start_collection(self)
31
31
  prepare!
32
32
 
33
- summary[5] = Benchmark.ms do
33
+ summary.ms = Benchmark.ms do
34
34
  each_new_record &method(:process_record)
35
35
  end
36
36
 
@@ -42,32 +42,10 @@ module AbstractImporter
42
42
 
43
43
 
44
44
  def prepare!
45
- # [total, existing_records, new_records, already_imported, invalid, milliseconds]
46
- @summary = [ 0, 0, 0, 0, 0, 0]
47
- @already_imported = load_already_imported_records!
45
+ @summary = Summary.new
48
46
  @mappings = prepare_mappings!
49
47
  end
50
48
 
51
- def load_already_imported_records!
52
- # We keep a _separate_ list of legacy IDs that
53
- # have already been imported. It would optimal to
54
- # check @id_map to see if a record has been imported;
55
- # but because of a bug with tags, that won't work:
56
- #
57
- # Tags import from three table: Activity, Skill,
58
- # and Training. Those tables yield tags whose
59
- # legacy_ids collide. As a result several tags
60
- # can share the same ID; and tags that would collide
61
- # are [erroneously] not imported.
62
- #
63
- # Fixing this problem would involve changing the
64
- # legacy_id identifier for each tag which would
65
- # break the connection between already-imported tags
66
- # and new imports.
67
- #
68
- id_map[table_name]
69
- end
70
-
71
49
  def prepare_mappings!
72
50
  mappings = []
73
51
  model.reflect_on_all_associations.each do |association|
@@ -117,24 +95,24 @@ module AbstractImporter
117
95
  end
118
96
 
119
97
  def process_record(hash)
120
- summary[0] += 1
98
+ summary.total += 1
121
99
 
122
100
  if already_imported?(hash)
123
- summary[3] += 1
101
+ summary.already_imported += 1
124
102
  return
125
103
  end
126
104
 
127
105
  remap_foreign_keys!(hash)
128
106
 
129
107
  if redundant_record?(hash)
130
- summary[1] += 1
108
+ summary.redundant += 1
131
109
  return
132
110
  end
133
111
 
134
112
  if create_record(hash)
135
- summary[2] += 1
113
+ summary.created += 1
136
114
  else
137
- summary[4] += 1
115
+ summary.invalid += 1
138
116
  end
139
117
  end
140
118
 
@@ -143,7 +121,7 @@ module AbstractImporter
143
121
 
144
122
 
145
123
  def already_imported?(hash)
146
- @already_imported.key? hash[:id]
124
+ id_map.contains? table_name, hash[:id]
147
125
  end
148
126
 
149
127
  def remap_foreign_keys!(hash)
@@ -19,6 +19,10 @@ module AbstractImporter
19
19
  end
20
20
  alias :[] :get
21
21
 
22
+ def contains?(table_name, id)
23
+ @id_map[table_name.to_sym].key?(id)
24
+ end
25
+
22
26
  def <<(record)
23
27
  register(record: record)
24
28
  end
@@ -43,19 +43,9 @@ module AbstractImporter
43
43
  end
44
44
 
45
45
  def finish_collection(collection, summary)
46
- ms = summary[5]
47
- elapsed = distance_of_time(ms)
48
- stat "\n #{summary[0]} #{collection.name} were found"
49
- if summary[0] > 0
50
- stat "#{summary[3]} #{collection.name} were imported previously"
51
- stat "#{summary[1]} #{collection.name} would create duplicates and will not be imported"
52
- stat "#{summary[4]} #{collection.name} were invalid"
53
- stat "#{summary[2]} #{collection.name} were imported"
54
- end
55
- stat "#{elapsed} elapsed" << (summary[0] > 0 ? " (#{(ms / summary[0]).to_i}ms each)" : "")
56
-
57
- print_messages(@notices, "Notices")
58
- print_messages(@errors, "Errors")
46
+ print_summary summary, collection.name
47
+ print_messages @notices, "Notices"
48
+ print_messages @errors, "Errors"
59
49
  end
60
50
 
61
51
 
@@ -115,6 +105,19 @@ module AbstractImporter
115
105
  end
116
106
  end
117
107
 
108
+ def print_summary(summary, plural)
109
+ stat "\n #{summary.total} #{plural} were found"
110
+ if summary.total > 0
111
+ stat "#{summary.already_imported} #{plural} were imported previously"
112
+ stat "#{summary.redundant} #{plural} would create duplicates and will not be imported"
113
+ stat "#{summary.invalid} #{plural} were invalid"
114
+ stat "#{summary.created} #{plural} were imported"
115
+ stat "#{distance_of_time(summary.ms)} elapsed (#{(summary.ms / summary.total).to_i}ms each)"
116
+ else
117
+ stat "#{distance_of_time(summary.ms)} elapsed"
118
+ end
119
+ end
120
+
118
121
  def print_messages(array, caption)
119
122
  return if array.empty?
120
123
  status "\n--#{caption}#{("-"*(78-caption.length))}\n\n"
@@ -0,0 +1,9 @@
1
+ module AbstractImporter
2
+ class Summary < Struct.new(:total, :redundant, :created, :already_imported, :invalid, :ms)
3
+
4
+ def initialize
5
+ super(0,0,0,0,0,0)
6
+ end
7
+
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module AbstractImporter
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: abstract_importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bob Lail
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-14 00:00:00.000000000 Z
11
+ date: 2013-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -172,6 +172,7 @@ extensions: []
172
172
  extra_rdoc_files: []
173
173
  files:
174
174
  - .gitignore
175
+ - .travis.yml
175
176
  - Gemfile
176
177
  - LICENSE.txt
177
178
  - README.md
@@ -185,6 +186,7 @@ files:
185
186
  - lib/abstract_importer/import_options.rb
186
187
  - lib/abstract_importer/import_plan.rb
187
188
  - lib/abstract_importer/reporter.rb
189
+ - lib/abstract_importer/summary.rb
188
190
  - lib/abstract_importer/version.rb
189
191
  - test/callback_test.rb
190
192
  - test/importer_test.rb
@@ -212,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
214
  version: '0'
213
215
  requirements: []
214
216
  rubyforge_project:
215
- rubygems_version: 2.0.2
217
+ rubygems_version: 2.0.3
216
218
  signing_key:
217
219
  specification_version: 4
218
220
  summary: Provides services for the mass-import of complex relational data