heart_seed
heart_seed copied to clipboard
seed util (convert excel to yaml and insert yaml to db)
NO LONGER MAINTAINED
HeartSeed
seed util (convert excel to yaml and insert yaml to db)
Require
- ruby 2.0+
- Ruby on Rails , Padrino or other ruby app
Installation
Add this line to your application's Gemfile:
gem 'heart_seed'
And then execute:
$ bundle
Or install it yourself as:
$ gem install heart_seed
xls support
If you want to use .xls file (NOT .xlsx file), gem install (or write to Gemfile) roo-xls too
And see License!
Usage
bundle exec rake heart_seed:init- create
config/heart_seed.yml,db/xls,db/seeds - append to
db/seeds.rb
- create
- Create xls
- sheet name (xls,xlsx) = table name (DB)
- example https://github.com/sue445/heart_seed/tree/master/spec/dummy/db/xls
bundle exec rake heart_seed:xls- Generate yml to
db/seeds - If you want to specify files:
FILES=comments_and_likes.xls SHEETS=comments,likes bundle exec rake heart_seed:xls
- Generate yml to
bundle exec rake db:seedorbundle exec rake heart_seed:db:seed- Import yml to db
- Exists
TABLES,CATALOGSoptions
examples
TABLES=articles,comments bundle exec rake db:seed
CATALOGS=article,user bundle exec rake db:seed
not Rails
append this to Rakefile
require 'heart_seed/tasks'
Note
- if production
db:seed, requireENV["TABLES"]orENV["CATALOGS"]
Snippets
config/heart_seed.yml
seed_dir: db/seeds
xls_dir: db/xls
catalogs:
# user:
# - users
# - user_profiles
db/seeds.rb
# 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)
Specification
Supported xls/xlsx format
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 |
- Sheet name is mapped table name
- If sheet name is not found in database, this is ignored
- 1st row : table column names
- If the spaces are included in the middle, right columns are ignored
- 2nd row ~ : records
- ActiveSupport::TimeWithZone is used for timezone
Yaml format
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'
- same as ActiveRecord::FixtureSet format
Catalog
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
Shard DB
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)
Insert Mode
MODE=(bulk|active_record|update) bundle exec rake db:seed
bulk(default): using bulk insert. (delete_alland BULK INSERT)active_record: import with ActiveRecord. (delete_allandcreate!)update: import with ActiveRecord. (if exists same record,update!, otherwisecreate!)
License
While heart_seed is licensed under the MIT license, please note that the 'spreadsheet' gem is released under the GPLv3 license.
- https://github.com/roo-rb/roo#additional-libraries
- https://github.com/roo-rb/roo-xls#license
- https://github.com/zdavatz/spreadsheet/blob/master/LICENSE.txt
Contributing
- Fork it ( https://github.com/sue445/heart_seed/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
