heart_seed 0.0.5 → 0.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: 3eac8531ec734a730680e223a8bb7a58a43dac84
4
- data.tar.gz: d8f9f23f4759928cc439363f198292ccc849d900
3
+ metadata.gz: 7105f8c5625c2b3850966e762eb226f5f6356a89
4
+ data.tar.gz: e8a85e9eb0083fc5bc132218b34d00727e328758
5
5
  SHA512:
6
- metadata.gz: d47173b69ed309859fb0df2e1f28e2ca4e6651c765534bfe2c1fd37b46d057ceae9a6eed19125e836152a6ddd7eaee39b79d3cfda8799849e81f24c9eddfebce
7
- data.tar.gz: 1fc16baceacbb9a16ae967250e84c391d077dd87be4b97ec42d38d8be0979a3cdd40687f289905fedefebb645fe48a18ff090d1f2483e59ce23537634ef39eff
6
+ metadata.gz: bf3d33a5bc7a50dc9dd1018749f425340199dad8de49b556c9e8607a0058c4cbe91c63fb46657649b354ce8b27aa39a8d1944f3b728013ce37c92b544a011c16
7
+ data.tar.gz: ce8dfb0bb5668ee107b1cb6867be576cc60abb53474414601b84f58c599057c39df01cd3b1444772805de5f174a44aab55e6baaf1f4244c90a0f507b7fe36e07
@@ -0,0 +1 @@
1
+ strategy: 'bundler'
@@ -5,6 +5,7 @@ rvm:
5
5
  - 2.2
6
6
  - ruby-head
7
7
  bundler_args: "--jobs=2"
8
+ cache: bundler
8
9
  before_script:
9
10
  - export CODECLIMATE_REPO_TOKEN=9fe512c81149a06ced2b890ab071422bf553cbaf3253fe6830ad75ce92f37516
10
11
  - export COVERAGE=true
@@ -1,5 +1,18 @@
1
1
  ## master
2
- [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.5...master)
2
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.1.0...master)
3
+
4
+ ## 0.1.0 (2015/05/19)
5
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.5...v0.1.0)
6
+
7
+ * Add Insert Mode `update`
8
+ * https://github.com/sue445/heart_seed/pull/28
9
+ * Upgrade to roo v2.0.0 and roo-xls v1.0.0
10
+ * https://github.com/sue445/heart_seed/pull/29
11
+
12
+ ### [Note] Tweak License!
13
+ * If you want to use xls support, read [License](https://github.com/sue445/heart_seed#license) and
14
+ add [roo-xls](https://github.com/roo-rb/roo-xls) to your Gemfile
15
+ * https://github.com/sue445/heart_seed#xls-support
3
16
 
4
17
  ## 0.0.5 (2015/02/09)
5
18
  [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.4...v0.0.5)
data/README.md CHANGED
@@ -29,6 +29,11 @@ Or install it yourself as:
29
29
 
30
30
  $ gem install heart_seed
31
31
 
32
+ ### xls support
33
+ If you want to use `.xls` file (NOT `.xlsx` file), `gem install` (or write to Gemfile) [roo-xls](https://github.com/roo-rb/roo-xls) too
34
+
35
+ **[And see License!](#license)**
36
+
32
37
  ## Usage
33
38
 
34
39
  1. `bundle exec rake heart_seed:init`
@@ -154,6 +159,22 @@ SHARD_NAMES = %W(
154
159
  HeartSeed::DbSeed.import_all_with_shards(shard_names: SHARD_NAMES)
155
160
  ```
156
161
 
162
+ ### Insert Mode
163
+ ```
164
+ MODE=(bulk|active_record|update) bundle exec rake db:seed
165
+ ```
166
+
167
+ * `bulk`(default): using bulk insert. (`delete_all` and BULK INSERT)
168
+ * `active_record`: import with ActiveRecord. (`delete_all` and `create!`)
169
+ * `update`: import with ActiveRecord. (if exists same record, `update!`, otherwise `create!`)
170
+
171
+ ## License
172
+ While heart_seed is licensed under the MIT license, please note that the 'spreadsheet' gem is released under the GPLv3 license.
173
+
174
+ * https://github.com/roo-rb/roo#additional-libraries
175
+ * https://github.com/roo-rb/roo-xls#license
176
+ * https://github.com/zdavatz/spreadsheet/blob/master/LICENSE.txt
177
+
157
178
  ## Contributing
158
179
 
159
180
  1. Fork it ( https://github.com/sue445/heart_seed/fork )
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.add_dependency "activerecord", ">= 3.0.0"
24
24
  spec.add_dependency "activerecord-import"
25
25
  spec.add_dependency "activesupport", ">= 3.0.0"
26
- spec.add_dependency "roo", ">= 1.13.2"
26
+ spec.add_dependency "roo", ">= 2.0.0"
27
27
 
28
28
  spec.add_development_dependency "bundler"
29
29
  spec.add_development_dependency "codeclimate-test-reporter"
@@ -34,6 +34,7 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "pry-remote"
35
35
  spec.add_development_dependency "rake"
36
36
  spec.add_development_dependency "rake_shared_context"
37
+ spec.add_development_dependency "roo-xls"
37
38
  spec.add_development_dependency "rspec", "3.0.0"
38
39
  spec.add_development_dependency "rspec-collection_matchers"
39
40
  spec.add_development_dependency "rspec-its"
@@ -4,6 +4,11 @@ require "active_record"
4
4
  require "yaml"
5
5
  require "activerecord-import"
6
6
 
7
+ begin
8
+ require "roo-xls"
9
+ rescue LoadError
10
+ end
11
+
7
12
  module HeartSeed
8
13
  autoload :Converter , "heart_seed/converter"
9
14
  autoload :DbSeed , "heart_seed/db_seed"
@@ -2,6 +2,8 @@ module HeartSeed
2
2
  module DbSeed
3
3
  BULK = "bulk"
4
4
  ACTIVE_RECORD = "active_record"
5
+ UPDATE = "update"
6
+
5
7
  # delete all records and bulk insert from seed yaml
6
8
  #
7
9
  # @param file_path [String]
@@ -33,6 +35,24 @@ module HeartSeed
33
35
  end
34
36
  end
35
37
 
38
+ # insert records. if same record exists, updated
39
+ #
40
+ # @param file_path [String]
41
+ # @param model_class [Class] require. extends {ActiveRecord::Base}
42
+ def self.insert_or_update(file_path: nil, model_class: nil)
43
+ fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
44
+ model_class.transaction do
45
+ fixtures.each do |fixture|
46
+ model = model_class.find_by(id: fixture["id"])
47
+ if model
48
+ model.update!(fixture)
49
+ else
50
+ model_class.create!(fixture)
51
+ end
52
+ end
53
+ end
54
+ end
55
+
36
56
  # import all seed yaml to table
37
57
  #
38
58
  # @param seed_dir [String]
@@ -42,9 +62,10 @@ module HeartSeed
42
62
  # @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names.
43
63
  # if empty, import all seed yaml.
44
64
  # if not empty, import only these tables in catalogs.
45
- # @param insert_mode [String] const ACTIVE_RECORD or other string.
46
- # if empty or not ACTIVE_RECORD, using bulk insert.
47
- # if ACTIVE_RECORD, import with ActiveRecord.
65
+ # @param insert_mode [String] const `ACTIVE_RECORD` or `UPDATE` other string.
66
+ # if `ACTIVE_RECORD`, import with ActiveRecord. (`delete_all` and `create!`)
67
+ # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
68
+ # other, using bulk insert. (`delete_all` and BULK INSERT)
48
69
  def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], mode: ENV["MODE"])
49
70
  mode ||= BULK
50
71
  target_table_names = parse_target_table_names(tables: tables, catalogs: catalogs)
@@ -91,9 +112,10 @@ module HeartSeed
91
112
  # @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names.
92
113
  # if empty, import all seed yaml.
93
114
  # if not empty, import only these tables in catalogs.
94
- # @param insert_mode [String] const ACTIVE_RECORD or other string.
95
- # if empty or not ACTIVE_RECORD, using bulk insert.
96
- # if ACTIVE_RECORD, import with ActiveRecord.
115
+ # @param insert_mode [String] const `ACTIVE_RECORD` or `UPDATE` other string.
116
+ # if `ACTIVE_RECORD`, import with ActiveRecord. (`delete_all` and `create!`)
117
+ # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
118
+ # other, using bulk insert. (`delete_all` and BULK INSERT)
97
119
  # @param shard_names [Array<String>]
98
120
  def self.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"],
99
121
  mode: ENV["MODE"] || BULK, shard_names: [])
@@ -136,12 +158,16 @@ module HeartSeed
136
158
  # insert yaml file to table
137
159
  # @param file_path [String] source seed yaml file
138
160
  # @param table_name [String] output destination table
139
- # @param mode [String] #{BULK} or #{ACTIVE_RECORD}
161
+ # @param mode [String] #{BULK}, #{UPDARE} or #{ACTIVE_RECORD}
140
162
  def self.insert_seed(file_path: nil, table_name: nil, mode: BULK)
141
163
  model_class = table_name.classify.constantize
142
- if mode == ACTIVE_RECORD
164
+ case mode
165
+ when ACTIVE_RECORD
143
166
  insert(file_path: file_path, model_class: model_class)
167
+ when UPDATE
168
+ insert_or_update(file_path: file_path, model_class: model_class)
144
169
  else
170
+ # default is BULK mode
145
171
  bulk_insert(file_path: file_path, model_class: model_class)
146
172
  end
147
173
  end
@@ -1,3 +1,3 @@
1
1
  module HeartSeed
2
- VERSION = "0.0.5"
2
+ VERSION = "0.1.0"
3
3
  end
Binary file
@@ -0,0 +1,11 @@
1
+ ---
2
+ articles_1:
3
+ id: 1
4
+ title: old title1
5
+ description: old description1
6
+ created_at: '2014-06-01 12:10:00 +0900'
7
+ articles_3:
8
+ id: 3
9
+ title: title3
10
+ description: description3
11
+ created_at: '2014-06-02 12:10:00 +0900'
@@ -27,6 +27,29 @@ describe HeartSeed::DbSeed do
27
27
  end
28
28
  end
29
29
 
30
+ describe "#insert_or_update" do
31
+ subject { HeartSeed::DbSeed.insert_or_update(file_path: file_path, model_class: model_class) }
32
+
33
+ let(:file_path){ "#{FIXTURE_DIR}/articles.yml" }
34
+ let(:model_class){ Article }
35
+
36
+ context "When not exists same data" do
37
+ it{ expect{ subject }.to change(Article, :count).by(2) }
38
+ end
39
+
40
+ context "When exists same data" do
41
+ before do
42
+ HeartSeed::DbSeed.insert(file_path: old_file_path, model_class: model_class)
43
+ end
44
+
45
+ let(:old_file_path){ "#{FIXTURE_DIR}/other/old_articles.yml" }
46
+
47
+ it{ expect{ subject }.to change(Article, :count).from(2).to(3) }
48
+ it{ expect{ subject }.to change{ Article.find(1).title }.from("old title1").to("title1") }
49
+ it{ expect{ subject }.to change{ Article.find(1).description }.from("old description1").to("description1") }
50
+ end
51
+ end
52
+
30
53
  describe "#import_all" do
31
54
  subject{ HeartSeed::DbSeed.import_all(seed_dir: seed_dir, tables: tables, catalogs: catalogs, mode: mode) }
32
55
 
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.0.5
4
+ version: 0.1.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-02-08 00:00:00.000000000 Z
11
+ date: 2015-05-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 1.13.2
61
+ version: 2.0.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: 1.13.2
68
+ version: 2.0.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -192,6 +192,20 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: roo-xls
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
195
209
  - !ruby/object:Gem::Dependency
196
210
  name: rspec
197
211
  requirement: !ruby/object:Gem::Requirement
@@ -301,6 +315,7 @@ files:
301
315
  - ".gitignore"
302
316
  - ".hound.yml"
303
317
  - ".rspec"
318
+ - ".tachikoma.yml"
304
319
  - ".travis.yml"
305
320
  - ".yardopts"
306
321
  - CHANGELOG.md
@@ -381,6 +396,7 @@ files:
381
396
  - spec/fixtures/comments.yml
382
397
  - spec/fixtures/invalid/invalid_comments.yml
383
398
  - spec/fixtures/likes.yml
399
+ - spec/fixtures/other/old_articles.yml
384
400
  - spec/fixtures/shard_articles.yml
385
401
  - spec/heart_seed/converter_spec.rb
386
402
  - spec/heart_seed/db_seed_spec.rb
@@ -477,6 +493,7 @@ test_files:
477
493
  - spec/fixtures/comments.yml
478
494
  - spec/fixtures/invalid/invalid_comments.yml
479
495
  - spec/fixtures/likes.yml
496
+ - spec/fixtures/other/old_articles.yml
480
497
  - spec/fixtures/shard_articles.yml
481
498
  - spec/heart_seed/converter_spec.rb
482
499
  - spec/heart_seed/db_seed_spec.rb