heart_seed 0.0.4 → 0.2.1

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
- SHA1:
3
- metadata.gz: cf94922ae1a0a622269943b4598ac461fabb7f11
4
- data.tar.gz: 84bb1c58325c03d3472656e763a27b892428b6f0
2
+ SHA256:
3
+ metadata.gz: 83bf236073c81560457a5ac1a4c70ac8da4346526af3a4553a2c28e9c081b983
4
+ data.tar.gz: a3b7e7cf5f9d0c7b1a464f02fe4aaf1147b1dc7790b4ef7d169396bfb8a126e9
5
5
  SHA512:
6
- metadata.gz: 53da2afdc2b19b6cd23a2d299b0540cf221738f5ca09e36cecf06f955e73d9049971b9ed0e4d4277f8227fcfb26fd75175c207262a14214a71c2607b37cf5f00
7
- data.tar.gz: 22f75929ad85617f9516b714fe6c2672fdd1cce5de61e52186272438e5ce6d4ceff36b339d8bb0e36cc6c9cdf1548207cbf1b0ba4e23a8a145221fd1e1be96d8
6
+ metadata.gz: f2447389cd0d58e6e1f79ed1a2f14f8b58e619507d8c8a2e0a319de8fcc89811dd78f05e4643f0a8070b0d573a710ebbdc402e2ea53c88b15e1f163abd614fbe
7
+ data.tar.gz: 9eacb3fd0a51cb5f9633ac57715ff6aa4d750b3c93fe800b25540faf7ea4f107e66c575968ed1eede66bf6eca94ee1043f0b5a700fd6722fc911310296105f15
@@ -0,0 +1,112 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+ pull_request:
8
+ types:
9
+ - opened
10
+ - synchronize
11
+ - reopened
12
+ schedule:
13
+ - cron: "0 10 * * 5" # JST 19:00 (Fri)
14
+
15
+ env:
16
+ CI: "true"
17
+
18
+ jobs:
19
+ test:
20
+ runs-on: ubuntu-latest
21
+
22
+ container: ${{ matrix.ruby }}
23
+
24
+ strategy:
25
+ fail-fast: false
26
+
27
+ matrix:
28
+ ruby:
29
+ - ruby:2.2
30
+ - ruby:2.3
31
+ - ruby:2.4
32
+ - ruby:2.5
33
+ - ruby:2.6
34
+ - ruby:2.7
35
+ - ruby:3.0
36
+ - rubylang/ruby:master-nightly-bionic
37
+ include:
38
+ - ruby: rubylang/ruby:master-nightly-bionic
39
+ allow_failures: "true"
40
+
41
+ steps:
42
+ - uses: actions/checkout@v2
43
+
44
+ - run: apt-get update && apt-get install -y libsqlite3-dev
45
+
46
+ - name: Cache vendor/bundle
47
+ uses: actions/cache@v1
48
+ id: cache_gem
49
+ with:
50
+ path: vendor/bundle
51
+ key: v1-gem-${{ runner.os }}-${{ matrix.ruby }}-${{ github.sha }}
52
+ restore-keys: |
53
+ v1-gem-${{ runner.os }}-${{ matrix.ruby }}-
54
+ continue-on-error: ${{ matrix.allow_failures == 'true' }}
55
+
56
+ - name: bundle update
57
+ run: |
58
+ set -xe
59
+ bundle config path vendor/bundle
60
+ bundle update --jobs $(nproc) --retry 3
61
+ continue-on-error: ${{ matrix.allow_failures == 'true' }}
62
+
63
+ - name: Setup Code Climate Test Reporter
64
+ uses: aktions/codeclimate-test-reporter@v1
65
+ with:
66
+ codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }}
67
+ command: before-build
68
+ if: matrix.ruby >= 'ruby:2.4'
69
+ continue-on-error: true
70
+
71
+ - name: Run test
72
+ run: |
73
+ set -xe
74
+ bundle exec rspec
75
+ continue-on-error: ${{ matrix.allow_failures == 'true' }}
76
+
77
+ - name: Teardown Code Climate Test Reporter
78
+ uses: aktions/codeclimate-test-reporter@v1
79
+ with:
80
+ codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }}
81
+ command: after-build
82
+ if: matrix.ruby >= 'ruby:2.4' && always()
83
+ continue-on-error: true
84
+
85
+ - name: Slack Notification (not success)
86
+ uses: lazy-actions/slatify@master
87
+ if: "! success()"
88
+ continue-on-error: true
89
+ with:
90
+ job_name: ${{ format('*build* ({0})', matrix.ruby) }}
91
+ type: ${{ job.status }}
92
+ icon_emoji: ":octocat:"
93
+ url: ${{ secrets.SLACK_WEBHOOK }}
94
+ token: ${{ secrets.GITHUB_TOKEN }}
95
+
96
+ notify:
97
+ needs:
98
+ - test
99
+
100
+ runs-on: ubuntu-latest
101
+
102
+ steps:
103
+ - name: Slack Notification (success)
104
+ uses: lazy-actions/slatify@master
105
+ if: always()
106
+ continue-on-error: true
107
+ with:
108
+ job_name: '*build*'
109
+ type: ${{ job.status }}
110
+ icon_emoji: ":octocat:"
111
+ url: ${{ secrets.SLACK_WEBHOOK }}
112
+ token: ${{ secrets.GITHUB_TOKEN }}
data/CHANGELOG.md CHANGED
@@ -1,5 +1,39 @@
1
1
  ## master
2
- [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.4...master)
2
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.2.1...master)
3
+
4
+ ## 0.2.1 (2021/11/20)
5
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.2.0...v0.2.1)
6
+
7
+ * Enable MFA requirement for gem releasing
8
+ * https://github.com/sue445/heart_seed/pull/57
9
+
10
+ ## 0.2.0 (2017/10/01)
11
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.1.0...v0.2.0)
12
+
13
+ * Add :validate parameter
14
+ * https://github.com/sue445/heart_seed/pull/32
15
+
16
+ ## 0.1.0 (2015/05/19)
17
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.5...v0.1.0)
18
+
19
+ * Add Insert Mode `update`
20
+ * https://github.com/sue445/heart_seed/pull/28
21
+ * Upgrade to roo v2.0.0 and roo-xls v1.0.0
22
+ * https://github.com/sue445/heart_seed/pull/29
23
+
24
+ ### [Note] Tweak License!
25
+ * If you want to use xls support, read [License](https://github.com/sue445/heart_seed#license) and
26
+ add [roo-xls](https://github.com/roo-rb/roo-xls) to your Gemfile
27
+ * https://github.com/sue445/heart_seed#xls-support
28
+
29
+ ## 0.0.5 (2015/02/09)
30
+ [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.4...v0.0.5)
31
+
32
+ * Stop db:seed When invalid fixture
33
+ * https://github.com/sue445/heart_seed/issues/22
34
+ * https://github.com/sue445/heart_seed/issues/23
35
+ * execute seed do follow the order in catalog file
36
+ * https://github.com/sue445/heart_seed/issues/24
3
37
 
4
38
  ## 0.0.4 (2015/01/16)
5
39
  [full changelog](https://github.com/sue445/heart_seed/compare/v0.0.3...v0.0.4)
data/Gemfile CHANGED
@@ -2,3 +2,33 @@ 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
+ # NOTE: rails 5+ requires Ruby 2.2.2+
8
+ gem "activerecord", "< 5.0.0"
9
+ gem "activesupport", "< 5.0.0"
10
+
11
+ # https://github.com/rails/rails/blob/v4.2.11.1/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L5
12
+ gem "sqlite3", "~> 1.3.6"
13
+
14
+ elsif Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.5.0")
15
+ # NOTE: rails 6+ requires Ruby 2.5.0+
16
+ gem "activerecord", "< 6.0.0"
17
+ gem "activesupport", "< 6.0.0"
18
+
19
+ # https://github.com/rails/rails/blob/v5.2.3/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
20
+ gem "sqlite3", "~> 1.3.6"
21
+
22
+ else
23
+ # FIXME: Support activerecord 6.1+
24
+ gem "activerecord", "< 6.1.0"
25
+ gem "activesupport", "< 6.1.0"
26
+
27
+ # https://github.com/rails/rails/blob/v6.0.0/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb#L13
28
+ gem "sqlite3", "~> 1.4"
29
+ end
30
+
31
+ if Gem::Version.create(RUBY_VERSION) < Gem::Version.create("2.5.0")
32
+ # NOTE: unparser v0.3.0+ requires Ruby 2.5+
33
+ gem "unparser", "< 0.3.0"
34
+ end
data/README.md CHANGED
@@ -3,14 +3,11 @@
3
3
  seed util (convert excel to yaml and insert yaml to db)
4
4
 
5
5
  [![Gem Version](https://badge.fury.io/rb/heart_seed.svg)](http://badge.fury.io/rb/heart_seed)
6
- [![Build Status](https://travis-ci.org/sue445/heart_seed.svg)](https://travis-ci.org/sue445/heart_seed)
6
+ [![Build Status](https://github.com/sue445/heart_seed/workflows/test/badge.svg?branch=master)](https://github.com/sue445/heart_seed/actions?query=workflow%3Atest)
7
7
  [![Code Climate](https://codeclimate.com/github/sue445/heart_seed.png)](https://codeclimate.com/github/sue445/heart_seed)
8
8
  [![Coverage Status](https://img.shields.io/coveralls/sue445/heart_seed.svg)](https://coveralls.io/r/sue445/heart_seed?branch=master)
9
- [![Dependency Status](https://gemnasium.com/sue445/heart_seed.svg)](https://gemnasium.com/sue445/heart_seed)
10
9
  [![Inline docs](http://inch-ci.org/github/sue445/heart_seed.svg?branch=master)](http://inch-ci.org/github/sue445/heart_seed)
11
10
 
12
- [![Stories in Ready](https://badge.waffle.io/sue445/heart_seed.png?label=ready&title=Ready)](https://waffle.io/sue445/heart_seed)
13
-
14
11
  ## Require
15
12
  * ruby 2.0+
16
13
  * [Ruby on Rails](http://rubyonrails.org/) , [Padrino](http://www.padrinorb.com/) or other ruby app
@@ -29,20 +26,25 @@ Or install it yourself as:
29
26
 
30
27
  $ gem install heart_seed
31
28
 
29
+ ### xls support
30
+ 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
31
+
32
+ **[And see License!](#license)**
33
+
32
34
  ## Usage
33
35
 
34
36
  1. `bundle exec rake heart_seed:init`
35
- * create `config/heart_seed.yml`, `db/xls`, `db/seeds`
36
- * append to `db/seeds.rb`
37
+ * create `config/heart_seed.yml`, `db/xls`, `db/seeds`
38
+ * append to `db/seeds.rb`
37
39
  2. Create xls
38
- * sheet name (xls,xlsx) = table name (DB)
39
- * example https://github.com/sue445/heart_seed/tree/master/spec/dummy/db/xls
40
+ * sheet name (xls,xlsx) = table name (DB)
41
+ * example https://github.com/sue445/heart_seed/tree/master/spec/dummy/db/xls
40
42
  3. `bundle exec rake heart_seed:xls`
41
- * Generate yml to `db/seeds`
42
- * If you want to specify files: `FILES=comments_and_likes.xls SHEETS=comments,likes bundle exec rake heart_seed:xls`
43
+ * Generate yml to `db/seeds`
44
+ * If you want to specify files: `FILES=comments_and_likes.xls SHEETS=comments,likes bundle exec rake heart_seed:xls`
43
45
  4. `bundle exec rake db:seed` or `bundle exec rake heart_seed:db:seed`
44
- * Import yml to db
45
- * Exists `TABLES`, `CATALOGS` options
46
+ * Import yml to db
47
+ * Exists `TABLES`, `CATALOGS` options
46
48
 
47
49
  examples
48
50
 
@@ -77,8 +79,12 @@ catalogs:
77
79
  ```ruby
78
80
  # Appended by `rake heart_seed:init`
79
81
  HeartSeed::DbSeed.import_all
82
+
80
83
  # If you want to insert by ActiveRecord, replase like this.
81
84
  HeartSeed::DbSeed.import_all(mode: HeartSeed::DbSeed::ACTIVE_RECORD)
85
+
86
+ # If you want to skip model validation in insert, add `validate: false` (default is true)
87
+ HeartSeed::DbSeed.import_all(validate: true)
82
88
  ```
83
89
 
84
90
  ## Specification
@@ -154,6 +160,22 @@ SHARD_NAMES = %W(
154
160
  HeartSeed::DbSeed.import_all_with_shards(shard_names: SHARD_NAMES)
155
161
  ```
156
162
 
163
+ ### Insert Mode
164
+ ```
165
+ MODE=(bulk|active_record|update) bundle exec rake db:seed
166
+ ```
167
+
168
+ * `bulk`(default): using bulk insert. (`delete_all` and BULK INSERT)
169
+ * `active_record`: import with ActiveRecord. (`delete_all` and `create!`)
170
+ * `update`: import with ActiveRecord. (if exists same record, `update!`, otherwise `create!`)
171
+
172
+ ## License
173
+ While heart_seed is licensed under the MIT license, please note that the 'spreadsheet' gem is released under the GPLv3 license.
174
+
175
+ * https://github.com/roo-rb/roo#additional-libraries
176
+ * https://github.com/roo-rb/roo-xls#license
177
+ * https://github.com/zdavatz/spreadsheet/blob/master/LICENSE.txt
178
+
157
179
  ## Contributing
158
180
 
159
181
  1. Fork it ( https://github.com/sue445/heart_seed/fork )
data/heart_seed.gemspec CHANGED
@@ -13,6 +13,11 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = "https://github.com/sue445/heart_seed"
14
14
  spec.license = "MIT"
15
15
 
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/master/CHANGELOG.md"
19
+ spec.metadata["rubygems_mfa_required"] = "true"
20
+
16
21
  spec.required_ruby_version = '>= 2.0.0'
17
22
 
18
23
  spec.files = `git ls-files -z`.split("\x0")
@@ -23,23 +28,20 @@ Gem::Specification.new do |spec|
23
28
  spec.add_dependency "activerecord", ">= 3.0.0"
24
29
  spec.add_dependency "activerecord-import"
25
30
  spec.add_dependency "activesupport", ">= 3.0.0"
26
- spec.add_dependency "roo", ">= 1.13.2"
31
+ spec.add_dependency "roo", ">= 2.0.0"
27
32
 
28
- spec.add_development_dependency "bundler", "~> 1.6"
29
- spec.add_development_dependency "codeclimate-test-reporter"
33
+ spec.add_development_dependency "bundler"
30
34
  spec.add_development_dependency "coveralls"
31
- spec.add_development_dependency "database_rewinder", "> 0.2.0"
32
- spec.add_development_dependency "pry"
33
- spec.add_development_dependency "pry-nav"
34
- spec.add_development_dependency "pry-remote"
35
+ spec.add_development_dependency "database_rewinder", ">= 0.4.2"
35
36
  spec.add_development_dependency "rake"
36
- spec.add_development_dependency "rake_shared_context"
37
- spec.add_development_dependency "rspec", "3.0.0"
37
+ spec.add_development_dependency "rake_shared_context", "0.2.2"
38
+ spec.add_development_dependency "roo-xls"
39
+ spec.add_development_dependency "rspec"
38
40
  spec.add_development_dependency "rspec-collection_matchers"
39
41
  spec.add_development_dependency "rspec-its"
40
42
  spec.add_development_dependency "rspec-parameterized"
41
43
  spec.add_development_dependency "rspec-temp_dir"
44
+ spec.add_development_dependency "simplecov"
42
45
  spec.add_development_dependency "sqlite3"
43
46
  spec.add_development_dependency "yard"
44
47
  end
45
-
@@ -2,11 +2,14 @@ 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]
8
10
  # @param model_class [Class] require. extends {ActiveRecord::Base}
9
- 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)
10
13
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
11
14
  models = fixtures.each_with_object([]) do |fixture, response|
12
15
  response << model_class.new(fixture)
@@ -15,7 +18,7 @@ module HeartSeed
15
18
 
16
19
  model_class.transaction do
17
20
  model_class.delete_all
18
- model_class.import(models)
21
+ model_class.import(models, validate: validate)
19
22
  end
20
23
  end
21
24
 
@@ -23,12 +26,33 @@ module HeartSeed
23
26
  #
24
27
  # @param file_path [String]
25
28
  # @param model_class [Class] require. extends {ActiveRecord::Base}
26
- 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)
27
31
  fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
28
32
  model_class.transaction do
29
33
  model_class.delete_all
30
34
  fixtures.each do |fixture|
31
- model_class.create(fixture)
35
+ model_class.new(fixture).save!(validate: validate)
36
+ end
37
+ end
38
+ end
39
+
40
+ # insert records. if same record exists, updated
41
+ #
42
+ # @param file_path [String]
43
+ # @param model_class [Class] require. extends {ActiveRecord::Base}
44
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
45
+ def self.insert_or_update(file_path: nil, model_class: nil, validate: true)
46
+ fixtures = HeartSeed::Converter.read_fixture_yml(file_path)
47
+ model_class.transaction do
48
+ fixtures.each do |fixture|
49
+ model = model_class.find_by(id: fixture["id"])
50
+ if model
51
+ model.attributes = fixture
52
+ model.save!(validate: validate)
53
+ else
54
+ model_class.new(fixture).save!(validate: validate)
55
+ end
32
56
  end
33
57
  end
34
58
  end
@@ -42,26 +66,43 @@ module HeartSeed
42
66
  # @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names.
43
67
  # if empty, import all seed yaml.
44
68
  # 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.
48
- def self.import_all(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"], mode: ENV["MODE"])
69
+ # @param insert_mode [String] const `ACTIVE_RECORD` or `UPDATE` other string.
70
+ # if `ACTIVE_RECORD`, import with ActiveRecord. (`delete_all` and `create!`)
71
+ # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
72
+ # other, using bulk insert. (`delete_all` and BULK INSERT)
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)
49
75
  mode ||= BULK
50
76
  target_table_names = parse_target_table_names(tables: tables, catalogs: catalogs)
51
77
 
52
78
  raise "require TABLES or CATALOGS if production" if HeartSeed::Helper.production? && target_table_names.empty?
53
79
 
54
80
  ActiveRecord::Migration.verbose = true
55
- Dir.glob(File.join(seed_dir, "*.yml")) do |file_path|
56
- table_name = File.basename(file_path, '.*')
57
- next unless target_table?(table_name, target_table_names)
58
81
 
59
- ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
60
- begin
61
- insert_seed(file_path: file_path, table_name: table_name, mode: mode)
82
+ if target_table_names.empty?
83
+ # seed all tables
84
+ Dir.glob(File.join(seed_dir, "*.yml")) do |file_path|
85
+ table_name = File.basename(file_path, '.*')
86
+
87
+ ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
88
+ insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
89
+ ActiveRecord::Migration.say("[INFO] success", true)
90
+ end
91
+ end
92
+
93
+ else
94
+ # seed specified tables (follow the order)
95
+ target_table_names.each do |table_name|
96
+ file_path = File.join(seed_dir, "#{table_name}.yml")
97
+
98
+ unless File.exists?(file_path)
99
+ ActiveRecord::Migration.say("[WARN] #{file_path} is not exists")
100
+ next
101
+ end
102
+
103
+ ActiveRecord::Migration.say_with_time("#{file_path} -> #{table_name}") do
104
+ insert_seed(file_path: file_path, table_name: table_name, mode: mode, validate: validate)
62
105
  ActiveRecord::Migration.say("[INFO] success", true)
63
- rescue => e
64
- ActiveRecord::Migration.say("[ERROR] #{e.message}", true)
65
106
  end
66
107
  end
67
108
  end
@@ -76,16 +117,18 @@ module HeartSeed
76
117
  # @param catalogs [Array<String>,String] catalogs names array or comma separated catalog names.
77
118
  # if empty, import all seed yaml.
78
119
  # if not empty, import only these tables in catalogs.
79
- # @param insert_mode [String] const ACTIVE_RECORD or other string.
80
- # if empty or not ACTIVE_RECORD, using bulk insert.
81
- # if ACTIVE_RECORD, import with ActiveRecord.
120
+ # @param insert_mode [String] const `ACTIVE_RECORD` or `UPDATE` other string.
121
+ # if `ACTIVE_RECORD`, import with ActiveRecord. (`delete_all` and `create!`)
122
+ # if `UPDATE`, import with ActiveRecord. (if exists same record, `update!`)
123
+ # other, using bulk insert. (`delete_all` and BULK INSERT)
82
124
  # @param shard_names [Array<String>]
125
+ # @param validate [Boolean] run ActiveRecord's validation. default: true
83
126
  def self.import_all_with_shards(seed_dir: HeartSeed::Helper.seed_dir, tables: ENV["TABLES"], catalogs: ENV["CATALOGS"],
84
- mode: ENV["MODE"] || BULK, shard_names: [])
127
+ mode: ENV["MODE"] || BULK, shard_names: [], validate: true)
85
128
  shard_names.each do |shard_name|
86
129
  ActiveRecord::Migration.say_with_time("import to shard: #{shard_name}") do
87
130
  ActiveRecord::Base.establish_connection(shard_name.to_sym)
88
- 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)
89
132
  end
90
133
  end
91
134
  end
@@ -118,22 +161,21 @@ module HeartSeed
118
161
  tables.compact
119
162
  end
120
163
 
121
- def self.target_table?(source_table, target_tables)
122
- return true if target_tables.empty?
123
- target_tables.include?(source_table)
124
- end
125
- private_class_method :target_table?
126
-
127
164
  # insert yaml file to table
128
165
  # @param file_path [String] source seed yaml file
129
166
  # @param table_name [String] output destination table
130
- # @param mode [String] #{BULK} or #{ACTIVE_RECORD}
131
- def self.insert_seed(file_path: nil, table_name: nil, mode: BULK)
167
+ # @param mode [String] #{BULK}, #{UPDARE} or #{ACTIVE_RECORD}
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)
132
170
  model_class = table_name.classify.constantize
133
- if mode == ACTIVE_RECORD
134
- insert(file_path: file_path, model_class: model_class)
171
+ case mode
172
+ when ACTIVE_RECORD
173
+ insert(file_path: file_path, model_class: model_class, validate: validate)
174
+ when UPDATE
175
+ insert_or_update(file_path: file_path, model_class: model_class, validate: validate)
135
176
  else
136
- bulk_insert(file_path: file_path, model_class: model_class)
177
+ # default is BULK mode
178
+ bulk_insert(file_path: file_path, model_class: model_class, validate: validate)
137
179
  end
138
180
  end
139
181
  private_class_method :insert_seed
@@ -1,3 +1,3 @@
1
1
  module HeartSeed
2
- VERSION = "0.0.4"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/heart_seed.rb CHANGED
@@ -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"
Binary file
data/spec/db/migration.rb CHANGED
@@ -5,26 +5,26 @@ ActiveRecord::Schema.define(version: 1) do
5
5
  t.string :title
6
6
  t.text :description
7
7
 
8
- t.timestamps
8
+ t.timestamps null: false
9
9
  end
10
10
 
11
11
  create_table :comments do |t|
12
12
  t.references :article, index: true
13
13
  t.text :message
14
14
 
15
- t.timestamps
15
+ t.timestamps null: false
16
16
  end
17
17
 
18
18
  create_table :likes do |t|
19
19
  t.references :article, index: true
20
20
 
21
- t.timestamps
21
+ t.timestamps null: false
22
22
  end
23
23
 
24
24
  create_table :shard_articles do |t|
25
25
  t.string :title
26
26
  t.text :description
27
27
 
28
- t.timestamps
28
+ t.timestamps null: false
29
29
  end
30
30
  end
data/spec/db/setup.rb CHANGED
@@ -10,7 +10,7 @@ SHARD_NAMES.each do |shard_name|
10
10
  # database: ":memory:",
11
11
  database: db_file,
12
12
  timeout: 500
13
- }
13
+ }.with_indifferent_access
14
14
 
15
15
  DatabaseRewinder.create_cleaner(shard_name)
16
16
  ActiveRecord::Base.establish_connection(shard_name.to_sym)
@@ -4,6 +4,8 @@ end
4
4
 
5
5
  class Comment < ActiveRecord::Base
6
6
  establish_connection(:test)
7
+
8
+ validates_numericality_of :article_id, greater_than: 0
7
9
  end
8
10
 
9
11
  class Like < ActiveRecord::Base
@@ -0,0 +1,9 @@
1
+ ---
2
+ comments_1:
3
+ id: 1
4
+ article_id: 1
5
+ message: message1
6
+ comments_2:
7
+ id: 2
8
+ article_id: 0 # this is invalid!
9
+ message: message2
@@ -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'
@@ -11,10 +11,76 @@ describe HeartSeed::DbSeed do
11
11
  describe "#insert" do
12
12
  subject { HeartSeed::DbSeed.insert(file_path: file_path, model_class: model_class) }
13
13
 
14
- let(:file_path) { "#{FIXTURE_DIR}/comments.yml" }
15
14
  let(:model_class) { Comment }
16
15
 
17
- it{ expect{ subject }.to change(Comment, :count).by(2) }
16
+ context "When valid data" do
17
+ let(:file_path) { "#{FIXTURE_DIR}/comments.yml" }
18
+
19
+ it{ expect{ subject }.to change(Comment, :count).by(2) }
20
+ end
21
+
22
+ context "When invalid data" do
23
+ let(:file_path) { "#{FIXTURE_DIR}/invalid/invalid_comments.yml" }
24
+
25
+ it{ expect{ subject }.to raise_error ActiveRecord::RecordInvalid }
26
+ it{ expect{ subject rescue nil }.to change(Comment, :count).by(0) }
27
+ end
28
+ end
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
+
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
18
84
  end
19
85
 
20
86
  describe "#import_all" do
@@ -25,16 +91,6 @@ describe HeartSeed::DbSeed do
25
91
  let(:catalogs) { [] }
26
92
  let(:mode){}
27
93
 
28
- before do
29
- # FIXME can not clear if using `DatabaseRewinder.clean`
30
- DatabaseRewinder.clean_all
31
- end
32
-
33
- after do
34
- # FIXME can not clear if using `DatabaseRewinder.clean`
35
- DatabaseRewinder.clean_all
36
- end
37
-
38
94
  context "When empty tables" do
39
95
  it{ expect{ subject }.to change(Article, :count).by(2) }
40
96
  it{ expect{ subject }.to change(Comment, :count).by(2) }
@@ -54,10 +110,17 @@ describe HeartSeed::DbSeed do
54
110
 
55
111
  before do
56
112
  allow(HeartSeed::Helper).to receive(:catalogs){
57
- { "article" => ["articles", "likes"] }
113
+ { "article" => ["articles", "likes", "missing_table"] }
58
114
  }
59
115
  end
60
116
 
117
+ it "should execute seed do follow the order in catalog file" do
118
+ expect(HeartSeed::DbSeed).to receive(:insert_seed).with(hash_including(table_name: "articles")).ordered
119
+ expect(HeartSeed::DbSeed).to receive(:insert_seed).with(hash_including(table_name: "likes")).ordered
120
+ expect(HeartSeed::DbSeed).not_to receive(:insert_seed).with(hash_including(table_name: "missing_table")).ordered
121
+ subject
122
+ end
123
+
61
124
  it{ expect{ subject }.to change(Article, :count).by(2) }
62
125
  it{ expect{ subject }.to change(Comment, :count).by(0) }
63
126
  it{ expect{ subject }.to change(Like , :count).by(1) }
data/spec/spec_helper.rb CHANGED
@@ -15,12 +15,12 @@
15
15
  #
16
16
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
17
 
18
- if ENV["COVERAGE"]
19
- require "codeclimate-test-reporter"
20
- CodeClimate::TestReporter.start
18
+ if ENV["CI"]
19
+ require "simplecov"
20
+ require "coveralls"
21
21
 
22
- require 'coveralls'
23
- Coveralls.wear!
22
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
23
+ SimpleCov.start
24
24
  end
25
25
 
26
26
  $LOAD_PATH.unshift(File.join(__dir__, "..", "lib"))
@@ -29,7 +29,6 @@ require "rspec/its"
29
29
  require "rspec/collection_matchers"
30
30
  require "rspec-parameterized"
31
31
  require "rspec/temp_dir"
32
- require "pry"
33
32
  require "rake_shared_context"
34
33
  require "database_rewinder"
35
34
 
@@ -120,6 +119,12 @@ RSpec.configure do |config|
120
119
 
121
120
  # database_rewinder
122
121
  config.before :suite do
122
+ # FIXME: fool automatic_reconnect ...
123
+ # https://github.com/rails/rails/blob/v4.2.0/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb#L444
124
+ SHARD_NAMES.each do |shard_name|
125
+ DatabaseRewinder[nil, connection: shard_name].pool.automatic_reconnect = true
126
+ end
127
+
123
128
  DatabaseRewinder.clean_all
124
129
  # or
125
130
  # DatabaseRewinder.clean_with :any_arg_that_would_be_actually_ignored_anyway
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
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - sue445
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-16 00:00:00.000000000 Z
11
+ date: 2021-11-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -58,30 +58,16 @@ 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
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '1.6'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '1.6'
83
- - !ruby/object:Gem::Dependency
84
- name: codeclimate-test-reporter
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - ">="
@@ -112,18 +98,18 @@ dependencies:
112
98
  name: database_rewinder
113
99
  requirement: !ruby/object:Gem::Requirement
114
100
  requirements:
115
- - - ">"
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
- version: 0.2.0
103
+ version: 0.4.2
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
107
  requirements:
122
- - - ">"
108
+ - - ">="
123
109
  - !ruby/object:Gem::Version
124
- version: 0.2.0
110
+ version: 0.4.2
125
111
  - !ruby/object:Gem::Dependency
126
- name: pry
112
+ name: rake
127
113
  requirement: !ruby/object:Gem::Requirement
128
114
  requirements:
129
115
  - - ">="
@@ -137,21 +123,21 @@ dependencies:
137
123
  - !ruby/object:Gem::Version
138
124
  version: '0'
139
125
  - !ruby/object:Gem::Dependency
140
- name: pry-nav
126
+ name: rake_shared_context
141
127
  requirement: !ruby/object:Gem::Requirement
142
128
  requirements:
143
- - - ">="
129
+ - - '='
144
130
  - !ruby/object:Gem::Version
145
- version: '0'
131
+ version: 0.2.2
146
132
  type: :development
147
133
  prerelease: false
148
134
  version_requirements: !ruby/object:Gem::Requirement
149
135
  requirements:
150
- - - ">="
136
+ - - '='
151
137
  - !ruby/object:Gem::Version
152
- version: '0'
138
+ version: 0.2.2
153
139
  - !ruby/object:Gem::Dependency
154
- name: pry-remote
140
+ name: roo-xls
155
141
  requirement: !ruby/object:Gem::Requirement
156
142
  requirements:
157
143
  - - ">="
@@ -165,7 +151,7 @@ dependencies:
165
151
  - !ruby/object:Gem::Version
166
152
  version: '0'
167
153
  - !ruby/object:Gem::Dependency
168
- name: rake
154
+ name: rspec
169
155
  requirement: !ruby/object:Gem::Requirement
170
156
  requirements:
171
157
  - - ">="
@@ -179,7 +165,7 @@ dependencies:
179
165
  - !ruby/object:Gem::Version
180
166
  version: '0'
181
167
  - !ruby/object:Gem::Dependency
182
- name: rake_shared_context
168
+ name: rspec-collection_matchers
183
169
  requirement: !ruby/object:Gem::Requirement
184
170
  requirements:
185
171
  - - ">="
@@ -193,21 +179,7 @@ dependencies:
193
179
  - !ruby/object:Gem::Version
194
180
  version: '0'
195
181
  - !ruby/object:Gem::Dependency
196
- name: rspec
197
- requirement: !ruby/object:Gem::Requirement
198
- requirements:
199
- - - '='
200
- - !ruby/object:Gem::Version
201
- version: 3.0.0
202
- type: :development
203
- prerelease: false
204
- version_requirements: !ruby/object:Gem::Requirement
205
- requirements:
206
- - - '='
207
- - !ruby/object:Gem::Version
208
- version: 3.0.0
209
- - !ruby/object:Gem::Dependency
210
- name: rspec-collection_matchers
182
+ name: rspec-its
211
183
  requirement: !ruby/object:Gem::Requirement
212
184
  requirements:
213
185
  - - ">="
@@ -221,7 +193,7 @@ dependencies:
221
193
  - !ruby/object:Gem::Version
222
194
  version: '0'
223
195
  - !ruby/object:Gem::Dependency
224
- name: rspec-its
196
+ name: rspec-parameterized
225
197
  requirement: !ruby/object:Gem::Requirement
226
198
  requirements:
227
199
  - - ">="
@@ -235,7 +207,7 @@ dependencies:
235
207
  - !ruby/object:Gem::Version
236
208
  version: '0'
237
209
  - !ruby/object:Gem::Dependency
238
- name: rspec-parameterized
210
+ name: rspec-temp_dir
239
211
  requirement: !ruby/object:Gem::Requirement
240
212
  requirements:
241
213
  - - ">="
@@ -249,7 +221,7 @@ dependencies:
249
221
  - !ruby/object:Gem::Version
250
222
  version: '0'
251
223
  - !ruby/object:Gem::Dependency
252
- name: rspec-temp_dir
224
+ name: simplecov
253
225
  requirement: !ruby/object:Gem::Requirement
254
226
  requirements:
255
227
  - - ">="
@@ -298,10 +270,9 @@ extensions: []
298
270
  extra_rdoc_files: []
299
271
  files:
300
272
  - ".coveralls.yml"
273
+ - ".github/workflows/test.yml"
301
274
  - ".gitignore"
302
- - ".hound.yml"
303
275
  - ".rspec"
304
- - ".travis.yml"
305
276
  - ".yardopts"
306
277
  - CHANGELOG.md
307
278
  - Gemfile
@@ -379,22 +350,25 @@ files:
379
350
  - spec/dummy/vendor/assets/stylesheets/.keep
380
351
  - spec/fixtures/articles.yml
381
352
  - spec/fixtures/comments.yml
353
+ - spec/fixtures/invalid/invalid_comments.yml
382
354
  - spec/fixtures/likes.yml
355
+ - spec/fixtures/other/old_articles.yml
383
356
  - spec/fixtures/shard_articles.yml
384
357
  - spec/heart_seed/converter_spec.rb
385
358
  - spec/heart_seed/db_seed_spec.rb
386
359
  - spec/heart_seed/tasks/heart_seed_rake_spec.rb
387
360
  - spec/heart_seed_spec.rb
388
361
  - spec/spec_helper.rb
389
- - spec/support/models/article.rb
390
- - spec/support/models/comment.rb
391
- - spec/support/models/like.rb
392
362
  - spec/support/shared_contexts/rake_in_app_dir.rb
393
363
  homepage: https://github.com/sue445/heart_seed
394
364
  licenses:
395
365
  - MIT
396
- metadata: {}
397
- post_install_message:
366
+ metadata:
367
+ homepage_uri: https://github.com/sue445/heart_seed
368
+ source_code_uri: https://github.com/sue445/heart_seed
369
+ changelog_uri: https://github.com/sue445/heart_seed/blob/master/CHANGELOG.md
370
+ rubygems_mfa_required: 'true'
371
+ post_install_message:
398
372
  rdoc_options: []
399
373
  require_paths:
400
374
  - lib
@@ -409,9 +383,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
409
383
  - !ruby/object:Gem::Version
410
384
  version: '0'
411
385
  requirements: []
412
- rubyforge_project:
413
- rubygems_version: 2.4.5
414
- signing_key:
386
+ rubygems_version: 3.2.22
387
+ signing_key:
415
388
  specification_version: 4
416
389
  summary: seed util (convert excel to yaml and insert yaml to db)
417
390
  test_files:
@@ -477,15 +450,13 @@ test_files:
477
450
  - spec/dummy/vendor/assets/stylesheets/.keep
478
451
  - spec/fixtures/articles.yml
479
452
  - spec/fixtures/comments.yml
453
+ - spec/fixtures/invalid/invalid_comments.yml
480
454
  - spec/fixtures/likes.yml
455
+ - spec/fixtures/other/old_articles.yml
481
456
  - spec/fixtures/shard_articles.yml
482
457
  - spec/heart_seed/converter_spec.rb
483
458
  - spec/heart_seed/db_seed_spec.rb
484
459
  - spec/heart_seed/tasks/heart_seed_rake_spec.rb
485
460
  - spec/heart_seed_spec.rb
486
461
  - spec/spec_helper.rb
487
- - spec/support/models/article.rb
488
- - spec/support/models/comment.rb
489
- - spec/support/models/like.rb
490
462
  - spec/support/shared_contexts/rake_in_app_dir.rb
491
- 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
data/.travis.yml DELETED
@@ -1,21 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 2.0
4
- - 2.1
5
- - 2.2
6
- - ruby-head
7
- bundler_args: "--jobs=2"
8
- before_script:
9
- - export CODECLIMATE_REPO_TOKEN=9fe512c81149a06ced2b890ab071422bf553cbaf3253fe6830ad75ce92f37516
10
- - export COVERAGE=true
11
- script: bundle exec rspec
12
- branches:
13
- only:
14
- - master
15
- notifications:
16
- email: false
17
- slack:
18
- secure: ZlXJJ1No8ZY7I8kpl+FDdhU/f6GaTCbstE8P1iIuW0f/gmk1mGhlxT7YLdWfRlBqQxMjxid6CfFuxfYNYBj8j/V4tR2z2Q22vb7YkMvBK32CdFTWeKVdNC0Znt91dbB5UwyHuUbkrWHGzGDddpr2tytbo6shaNDH3B7KQm30/WE=
19
- matrix:
20
- allow_failures:
21
- - rvm: ruby-head
File without changes
File without changes
File without changes