/seed_builder.rb

Seed builder for ActiveRecord.

Primary LanguageRubyMIT LicenseMIT

seed_builder.rb

Gem Version Test Status codecov

Seed builder for ActiveRecord. Includes seeds loader and generator.

Sponsored by Kisko Labs.

Installation

Using Bundler:

bundle add seed_builder

Using RubyGems:

gem install seed_builder

Gemfile

gem "seed_builder"

Usage

Run seeds

bin/rails db:seed

Generate seed file

bin/rails g seed create_users

Configuration

Load default seeds.rb file

By default seed file db/seeds.rb is loaded.

To turn off loading default seeds.rb file:

SeedBuilder.config.load_default_seeds = false

Set seed directory

Absolute path will be resolved by using Rails.root.

SeedBuilder.config.seeds_path = "db/seeds"

Turn off test script generation

SeedBuilder.config.generate_spec = false

Turn off loader usage for ActiveRecord

SeedBuilder.config.use_seed_loader = false

Specification checklist

  • User can generate seed file under db/seeds directory with common format
  • User can generate seed file with test script included
  • User can run all seeds
  • User can run specific seed file

Limitations & explanations

  • Gem patches ActiveRecord::Tasks::DatabaseTasks.seed_loader to have custom loader
  • ActiveRecord migrations generator is used to generate seed files
  • Seeded data is not reversible, there is no point to implement it as logic can be complex and operations on data might lead to irreversible changes
  • Seed file is not a migration, although as good practice is to keep it idempotent, e.g. by checking uniqueness of seeded records

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amkisko/seed_builder.rb

Contribution policy:

  • New features are not necessarily added to the gem
  • Pull request should have test coverage for affected parts
  • Pull request should have changelog entry
  • It might take up to 2 calendar weeks to review and merge critical fixes
  • It might take up to 6 calendar months to review and merge pull request
  • It might take up to 1 calendar year to review an issue

Publishing

Prefer using script usr/bin/release.sh, it will ensure that repository is synced and after publishing gem will create a tag.

GEM_VERSION=$(grep -Eo "VERSION\s*=\s*\".+\"" lib/seed_builder.rb  | grep -Eo "[0-9.]{5,}")
rm seed_builder-*.gem
gem build seed_builder.gemspec
gem push seed_builder-$GEM_VERSION.gem
git tag $GEM_VERSION && git push --tags

License

The gem is available as open source under the terms of the MIT License.