seed util (convert excel to yaml and insert yaml to db)
seed util (convert excel to yaml and insert yaml to db)
Add this line to your application’s Gemfile:
gem 'heart_seed'
And then execute:
$ bundle
Or install it yourself as:
$ gem install heart_seed
If you want to use .xls
file (NOT .xlsx
file), gem install
(or write to Gemfile) roo-xls too
bundle exec rake heart_seed:init
config/heart_seed.yml
, db/xls
, db/seeds
db/seeds.rb
bundle exec rake heart_seed:xls
db/seeds
FILES=comments_and_likes.xls SHEETS=comments,likes bundle exec rake heart_seed:xls
bundle exec rake db:seed
or bundle exec rake heart_seed
seed
TABLES
, CATALOGS
optionsexamples
TABLES=articles,comments bundle exec rake db:seed
CATALOGS=article,user bundle exec rake db:seed
append this to Rakefile
require 'heart_seed/tasks'
db:seed
, require ENV["TABLES"]
or ENV["CATALOGS"]
seed_dir: db/seeds
xls_dir: db/xls
catalogs:
# user:
# - users
# - user_profiles
# Appended by `rake heart_seed:init`
HeartSeed::DbSeed.import_all
# If you want to insert by ActiveRecord, replase like this.
HeartSeed::DbSeed.import_all(mode: HeartSeed::DbSeed::ACTIVE_RECORD)
# If you want to skip model validation in insert, add `validate: false` (default is true)
HeartSeed::DbSeed.import_all(validate: true)
Example sheet
id | title | description | created_at | this is dummy | |
---|---|---|---|---|---|
1 | title1 | description1 | 2014/6/1 12:10 | foo | |
2 | title2 | description2 | 2014/6/2 12:10 | baz |
example
---
articles_1:
id: 1
title: title1
description: description1
created_at: '2014-06-01 12:10:00 +0900'
articles_2:
id: 2
title: title2
description: description2
created_at: '2014-06-02 12:10:00 +0900'
Catalog is table groups defined in heart_seed.yml
example
catalogs:
user:
- users
- user_profiles
user
catalog = users
, user_profiles
tables
You can specify the catalogs at db:seed
task
CATALOGS=user bundle exec rake db:seed
# same to) TABLES=users,user_profiles bundle exec rake db:seed
When you use shard DB, write like this to db/seeds.rb
.
Rails example
SHARD_NAMES = %W(
#{Rails.env}
shard_#{Rails.env}
shard2_#{Rails.env}
)
HeartSeed::DbSeed.import_all_with_shards(shard_names: SHARD_NAMES)
MODE=(bulk|active_record|update) bundle exec rake db:seed
bulk
(default): using bulk insert. (delete_all
and BULK INSERT)active_record
: import with ActiveRecord. (delete_all
and create!
)update
: import with ActiveRecord. (if exists same record, update!
, otherwise create!
)While heart_seed is licensed under the MIT license, please note that the ‘spreadsheet’ gem is released under the GPLv3 license.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)