heart_seed 0.1.0 → 0.2.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: 7105f8c5625c2b3850966e762eb226f5f6356a89
4
- data.tar.gz: e8a85e9eb0083fc5bc132218b34d00727e328758
3
+ metadata.gz: 72519e6c42efae07fac452d67d5ae4c97f44be5e
4
+ data.tar.gz: 6ad7ead4918223c3c17b9721021c39be61142452
5
5
  SHA512:
6
- metadata.gz: bf3d33a5bc7a50dc9dd1018749f425340199dad8de49b556c9e8607a0058c4cbe91c63fb46657649b354ce8b27aa39a8d1944f3b728013ce37c92b544a011c16
7
- data.tar.gz: ce8dfb0bb5668ee107b1cb6867be576cc60abb53474414601b84f58c599057c39df01cd3b1444772805de5f174a44aab55e6baaf1f4244c90a0f507b7fe36e07
6
+ metadata.gz: b5f1a13af6dbfb825cae06b8dc60e5ca1d41d49b3cbdd68b1fcd432ee3238e430888c937ef2e3a2ad3a92debbfbf4eefdbd2ce814d00ab3abab6fb11db1cbb37
7
+ data.tar.gz: 0dcd02b8bfc8f44f0a6ae6a7f4e0913a241ba3a58a8e3b0f8fbd8991619f024b06fba9faa2aaac94c291a4ee3e4edd2684c5b75520f76b0a229c2ceea8909288
@@ -1,15 +1,18 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.0
4
3
  - 2.1
5
4
  - 2.2
5
+ - 2.3.3
6
+ - 2.4.1
6
7
  - ruby-head
7
8
  bundler_args: "--jobs=2"
8
9
  cache: bundler
9
10
  before_script:
10
11
  - export CODECLIMATE_REPO_TOKEN=9fe512c81149a06ced2b890ab071422bf553cbaf3253fe6830ad75ce92f37516
11
12
  - export COVERAGE=true
12
- script: bundle exec rspec
13
+ script:
14
+ - bundle exec rspec
15
+ - bundle exec codeclimate-test-reporter
13
16
  branches:
14
17
  only:
15
18
  - master
@@ -20,3 +23,4 @@ notifications:
20
23
  matrix:
21
24
  allow_failures:
22
25
  - rvm: ruby-head
26
+ sudo: false
@@ -1,5 +1,11 @@
1
1
  ## master
2
- [full changelog](https://github.com/sue445/heart_seed/compare/v0.1.0...master)
2
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.2.0...master)
3
+
4
+ ## 0.2.0 (2017/10/01)
5
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.1.0...v0.2.0)
6
+
7
+ * Add :validate parameter
8
+ * https://github.com/sue445/heart_seed/pull/32
3
9
 
4
10
  ## 0.1.0 (2015/05/19)
5
11
  [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.5...v0.1.0)
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in heart_seed.gemspec
4
4
  gemspec
5
+
6
+ if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.2.2")
7
+ gem "activerecord", "< 5.0.0"
8
+ gem "activesupport", "< 5.0.0"
9
+ end
data/README.md CHANGED
@@ -82,8 +82,12 @@ catalogs:
82
82
  ```ruby
83
83
  # Appended by `rake heart_seed:init`
84
84
  HeartSeed::DbSeed.import_all
85
+
85
86
  # If you want to insert by ActiveRecord, replase like this.
86
87
  HeartSeed::DbSeed.import_all(mode: HeartSeed::DbSeed::ACTIVE_RECORD)
88
+
89
+ # If you want to skip model validation in insert, add `validate: false` (default is true)
90
+ HeartSeed::DbSeed.import_all(validate: true)
87
91
  ```
88
92
 
89
93
  ## Specification
@@ -26,20 +26,21 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "roo", ">= 2.0.0"
27
27
 
28
28
  spec.add_development_dependency "bundler"
29
- spec.add_development_dependency "codeclimate-test-reporter"
29
+ spec.add_development_dependency "codeclimate-test-reporter", "~> 1.0.0"
30
30
  spec.add_development_dependency "coveralls"
31
31
  spec.add_development_dependency "database_rewinder", ">= 0.4.2"
32
32
  spec.add_development_dependency "pry"
33
33
  spec.add_development_dependency "pry-nav"
34
34
  spec.add_development_dependency "pry-remote"
35
35
  spec.add_development_dependency "rake"
36
- spec.add_development_dependency "rake_shared_context"
36
+ spec.add_development_dependency "rake_shared_context", "0.2.2"
37
37
  spec.add_development_dependency "roo-xls"
38
- spec.add_development_dependency "rspec", "3.0.0"
38
+ spec.add_development_dependency "rspec"
39
39
  spec.add_development_dependency "rspec-collection_matchers"
40
40
  spec.add_development_dependency "rspec-its"
41
41
  spec.add_development_dependency "rspec-parameterized"
42
42
  spec.add_development_dependency "rspec-temp_dir"
43
+ spec.add_development_dependency "simplecov"
43
44
  spec.add_development_dependency "sqlite3"
44
45
  spec.add_development_dependency "yard"
45
46
  end
@@ -8,7 +8,8 @@ module HeartSeed
8
8
  #
9
9
  # @param file_path [String]
10
10
  # @param model_class [Class] require. extends {ActiveRecord::Base}
11
- def self.bulk_insert(file_path: nil, model_class: nil)
11
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
12
+ def self.bulk_insert(file_path: nil, model_class: nil, validate: true)
12
13
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
13
14
  models = fixtures.each_with_object([]) do |fixture, response|
14
15
  response << model_class.new(fixture)
@@ -17,7 +18,7 @@ module HeartSeed
17
18
 
18
19
  model_class.transaction do
19
20
  model_class.delete_all
20
- model_class.import(models)
21
+ model_class.import(models, validate: validate)
21
22
  end
22
23
  end
23
24
 
@@ -25,12 +26,13 @@ module HeartSeed
25
26
  #
26
27
  # @param file_path [String]
27
28
  # @param model_class [Class] require. extends {ActiveRecord::Base}
28
- def self.insert(file_path: nil, model_class: nil)
29
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
30
+ def self.insert(file_path: nil, model_class: nil, validate: true)
29
31
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
30
32
  model_class.transaction do
31
33
  model_class.delete_all
32
34
  fixtures.each do |fixture|
33
- model_class.create!(fixture)
35
+ model_class.new(fixture).save!(validate: validate)
34
36
  end
35
37
  end
36
38
  end
@@ -39,15 +41,17 @@ module HeartSeed
39
41
  #
40
42
  # @param file_path [String]
41
43
  # @param model_class [Class] require. extends {ActiveRecord::Base}
42
- def self.insert_or_update(file_path: nil, model_class: nil)
44
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
45
+ def self.insert_or_update(file_path: nil, model_class: nil, validate: true)
43
46
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
44
47
  model_class.transaction do
45
48
  fixtures.each do |fixture|
46
49
  model = model_class.find_by(id: fixture["id"])
47
50
  if model
48
- model.update!(fixture)
51
+ model.attributes = fixture
52
+ model.save!(validate: validate)
49
53
  else
50
- model_class.create!(fixture)
54
+ model_class.new(fixture).save!(validate: validate)
51
55
  end
52
56
  end
53
57
  end
@@ -66,7 +70,8 @@ module HeartSeed
66
70
  # if `ACTIVE_RECORD`, import with ActiveRecord. (`delete_all` and `create!`)
67
71
  # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
68
72
  # other, using bulk insert. (`delete_all` and BULK INSERT)
69
- def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], mode: ENV["MODE"])
73
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
74
+ def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], mode: ENV["MODE"], validate: true)
70
75
  mode ||= BULK
71
76
  target_table_names = parse_target_table_names(tables: tables, catalogs: catalogs)
72
77
 
@@ -80,7 +85,7 @@ module HeartSeed
80
85
  table_name = File.basename(file_path, '.*')
81
86
 
82
87
  ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
83
- insert_seed(file_path: file_path, table_name: table_name, mode: mode)
88
+ insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
84
89
  ActiveRecord::Migration.say("[INFO] success", true)
85
90
  end
86
91
  end
@@ -96,7 +101,7 @@ module HeartSeed
96
101
  end
97
102
 
98
103
  ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
99
- insert_seed(file_path: file_path, table_name: table_name, mode: mode)
104
+ insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
100
105
  ActiveRecord::Migration.say("[INFO] success", true)
101
106
  end
102
107
  end
@@ -117,12 +122,13 @@ module HeartSeed
117
122
  # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
118
123
  # other, using bulk insert. (`delete_all` and BULK INSERT)
119
124
  # @param shard_names [Array<String>]
125
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
120
126
  def self.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"],
121
- mode: ENV["MODE"] || BULK, shard_names: [])
127
+ mode: ENV["MODE"] || BULK, shard_names: [], validate: true)
122
128
  shard_names.each do |shard_name|
123
129
  ActiveRecord::Migration.say_with_time("import to shard: #{shard_name}") do
124
130
  ActiveRecord::Base.establish_connection(shard_name.to_sym)
125
- import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs, mode: mode)
131
+ import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs, mode: mode, validate: validate)
126
132
  end
127
133
  end
128
134
  end
@@ -159,16 +165,17 @@ module HeartSeed
159
165
  # @param file_path [String] source seed yaml file
160
166
  # @param table_name [String] output destination table
161
167
  # @param mode [String] #{BULK}, #{UPDARE} or #{ACTIVE_RECORD}
162
- def self.insert_seed(file_path: nil, table_name: nil, mode: BULK)
168
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
169
+ def self.insert_seed(file_path: nil, table_name: nil, mode: BULK, validate: true)
163
170
  model_class = table_name.classify.constantize
164
171
  case mode
165
172
  when ACTIVE_RECORD
166
- insert(file_path: file_path, model_class: model_class)
173
+ insert(file_path: file_path, model_class: model_class, validate: validate)
167
174
  when UPDATE
168
- insert_or_update(file_path: file_path, model_class: model_class)
175
+ insert_or_update(file_path: file_path, model_class: model_class, validate: validate)
169
176
  else
170
177
  # default is BULK mode
171
- bulk_insert(file_path: file_path, model_class: model_class)
178
+ bulk_insert(file_path: file_path, model_class: model_class, validate: validate)
172
179
  end
173
180
  end
174
181
  private_class_method :insert_seed
@@ -1,3 +1,3 @@
1
1
  module HeartSeed
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -50,6 +50,39 @@ describe HeartSeed::DbSeed do
50
50
  end
51
51
  end
52
52
 
53
+ context "When validate: false" do
54
+ subject { HeartSeed::DbSeed.send(method, file_path: file_path, model_class: model_class, validate: false) }
55
+
56
+ let(:file_path) { "#{FIXTURE_DIR}/invalid/invalid_comments.yml" }
57
+ let(:model_class) { Comment }
58
+
59
+ describe "#bulk_insert" do
60
+ let(:method) { :bulk_insert }
61
+ it{ expect{ subject }.to change(Comment, :count).by(2) }
62
+ end
63
+
64
+ describe "#insert" do
65
+ let(:method) { :insert }
66
+ it{ expect{ subject }.to change(Comment, :count).by(2) }
67
+ end
68
+
69
+ describe "#insert_or_update" do
70
+ let(:method) { :insert_or_update }
71
+
72
+ context "When insert" do
73
+ it{ expect{ subject }.to change(Comment, :count).by(2) }
74
+ end
75
+
76
+ context "When update" do
77
+ before { HeartSeed::DbSeed.insert(file_path: original_file_path, model_class: model_class) }
78
+
79
+ let(:original_file_path) { "#{FIXTURE_DIR}/comments.yml" }
80
+
81
+ it{ expect{ subject }.to change{ Comment.find(2).article_id }.from(1).to(0) }
82
+ end
83
+ end
84
+ end
85
+
53
86
  describe "#import_all" do
54
87
  subject{ HeartSeed::DbSeed.import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs, mode: mode) }
55
88
 
@@ -18,12 +18,8 @@
18
18
  if ENV["COVERAGE"]
19
19
  require "simplecov"
20
20
  require "coveralls"
21
- require "codeclimate-test-reporter"
22
21
 
23
- SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
24
- CodeClimate::TestReporter::Formatter,
25
- Coveralls::SimpleCov::Formatter
26
- ]
22
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
27
23
  SimpleCov.start
28
24
  end
29
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: heart_seed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - sue445
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
11
+ date: 2017-10-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -84,16 +84,16 @@ dependencies:
84
84
  name: codeclimate-test-reporter
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ">="
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0'
89
+ version: 1.0.0
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ">="
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0'
96
+ version: 1.0.0
97
97
  - !ruby/object:Gem::Dependency
98
98
  name: coveralls
99
99
  requirement: !ruby/object:Gem::Requirement
@@ -182,16 +182,16 @@ dependencies:
182
182
  name: rake_shared_context
183
183
  requirement: !ruby/object:Gem::Requirement
184
184
  requirements:
185
- - - ">="
185
+ - - '='
186
186
  - !ruby/object:Gem::Version
187
- version: '0'
187
+ version: 0.2.2
188
188
  type: :development
189
189
  prerelease: false
190
190
  version_requirements: !ruby/object:Gem::Requirement
191
191
  requirements:
192
- - - ">="
192
+ - - '='
193
193
  - !ruby/object:Gem::Version
194
- version: '0'
194
+ version: 0.2.2
195
195
  - !ruby/object:Gem::Dependency
196
196
  name: roo-xls
197
197
  requirement: !ruby/object:Gem::Requirement
@@ -210,16 +210,16 @@ dependencies:
210
210
  name: rspec
211
211
  requirement: !ruby/object:Gem::Requirement
212
212
  requirements:
213
- - - '='
213
+ - - ">="
214
214
  - !ruby/object:Gem::Version
215
- version: 3.0.0
215
+ version: '0'
216
216
  type: :development
217
217
  prerelease: false
218
218
  version_requirements: !ruby/object:Gem::Requirement
219
219
  requirements:
220
- - - '='
220
+ - - ">="
221
221
  - !ruby/object:Gem::Version
222
- version: 3.0.0
222
+ version: '0'
223
223
  - !ruby/object:Gem::Dependency
224
224
  name: rspec-collection_matchers
225
225
  requirement: !ruby/object:Gem::Requirement
@@ -276,6 +276,20 @@ dependencies:
276
276
  - - ">="
277
277
  - !ruby/object:Gem::Version
278
278
  version: '0'
279
+ - !ruby/object:Gem::Dependency
280
+ name: simplecov
281
+ requirement: !ruby/object:Gem::Requirement
282
+ requirements:
283
+ - - ">="
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
286
+ type: :development
287
+ prerelease: false
288
+ version_requirements: !ruby/object:Gem::Requirement
289
+ requirements:
290
+ - - ">="
291
+ - !ruby/object:Gem::Version
292
+ version: '0'
279
293
  - !ruby/object:Gem::Dependency
280
294
  name: sqlite3
281
295
  requirement: !ruby/object:Gem::Requirement
@@ -313,9 +327,7 @@ extra_rdoc_files: []
313
327
  files:
314
328
  - ".coveralls.yml"
315
329
  - ".gitignore"
316
- - ".hound.yml"
317
330
  - ".rspec"
318
- - ".tachikoma.yml"
319
331
  - ".travis.yml"
320
332
  - ".yardopts"
321
333
  - CHANGELOG.md
@@ -424,7 +436,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
424
436
  version: '0'
425
437
  requirements: []
426
438
  rubyforge_project:
427
- rubygems_version: 2.4.5
439
+ rubygems_version: 2.6.11
428
440
  signing_key:
429
441
  specification_version: 4
430
442
  summary: seed util (convert excel to yaml and insert yaml to db)
@@ -501,4 +513,3 @@ test_files:
501
513
  - spec/heart_seed_spec.rb
502
514
  - spec/spec_helper.rb
503
515
  - spec/support/shared_contexts/rake_in_app_dir.rb
504
- has_rdoc:
data/.hound.yml DELETED
@@ -1,31 +0,0 @@
1
- Style/LineLength:
2
- Description: 'Limit lines to 130 characters.'
3
- Max: 130
4
- Style/SpaceInsideParens:
5
- Enabled: false
6
- Style/SpaceBeforeBlockBraces:
7
- Enabled: false
8
- StringLiterals:
9
- Enabled: false
10
- Style/TrailingComma:
11
- Enabled: false
12
- Style/BlockComments:
13
- Enabled: false
14
- Style/NilComparison:
15
- Enabled: false
16
- Style/Documentation:
17
- Enabled: false
18
- Style/RegexpLiteral:
19
- Enabled: false
20
- Style/SignalException:
21
- Enabled: false
22
- Style/CaseEquality:
23
- Enabled: false
24
- Style/SpaceInsideBlockBraces:
25
- Enabled: false
26
- Style/SpaceBeforeComma:
27
- Enabled: false
28
- Style/SpaceInsideHashLiteralBraces:
29
- Enabled: false
30
- Style/SingleSpaceBeforeFirstArg:
31
- Enabled: false
@@ -1 +0,0 @@
1
- strategy: 'bundler'