heart_seed 0.0.5 → 0.1.0
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.
- checksums.yaml +4 -4
- data/.tachikoma.yml +1 -0
- data/.travis.yml +1 -0
- data/CHANGELOG.md +14 -1
- data/README.md +21 -0
- data/heart_seed.gemspec +2 -1
- data/lib/heart_seed.rb +5 -0
- data/lib/heart_seed/db_seed.rb +34 -8
- data/lib/heart_seed/version.rb +1 -1
- data/spec/data/articles.xlsx +0 -0
- data/spec/fixtures/other/old_articles.yml +11 -0
- data/spec/heart_seed/db_seed_spec.rb +23 -0
- metadata +21 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7105f8c5625c2b3850966e762eb226f5f6356a89
|
4
|
+
data.tar.gz: e8a85e9eb0083fc5bc132218b34d00727e328758
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf3d33a5bc7a50dc9dd1018749f425340199dad8de49b556c9e8607a0058c4cbe91c63fb46657649b354ce8b27aa39a8d1944f3b728013ce37c92b544a011c16
|
7
|
+
data.tar.gz: ce8dfb0bb5668ee107b1cb6867be576cc60abb53474414601b84f58c599057c39df01cd3b1444772805de5f174a44aab55e6baaf1f4244c90a0f507b7fe36e07
|
data/.tachikoma.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
strategy: 'bundler'
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## master
|
2
|
-
[full changelog](https://github.com/sue445/heart_seed/compare/v0.0
|
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 )
|
data/heart_seed.gemspec
CHANGED
@@ -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", ">=
|
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"
|
data/lib/heart_seed.rb
CHANGED
data/lib/heart_seed/db_seed.rb
CHANGED
@@ -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
|
47
|
-
# if
|
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
|
96
|
-
# if
|
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
|
-
|
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
|
data/lib/heart_seed/version.rb
CHANGED
data/spec/data/articles.xlsx
CHANGED
Binary file
|
@@ -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
|
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-
|
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:
|
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:
|
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
|