GithubHelp home page GithubHelp logo

navigatingcancer / seed_dump Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rroblak/seed_dump

0.0 33.0 0.0 164 KB

Rails 4 task to dump (parts) of your database to db/seeds.rb

License: MIT License

Ruby 100.00%

seed_dump's Introduction

Seed Dump

Seed Dump is a Rails 4 plugin that adds a rake task named db:seed:dump.

It allows you to create seed data files from the existing data in your database.

You can also use Seed Dump from the Rails console. See below for usage examples.

Note: if you want to use Seed Dump with Rails 3 or earlier, use version 0.5.3.

Installation

Add it to your Gemfile with:

gem 'seed_dump'

Or install it by hand:

$ gem install seed_dump

Examples

Rake task

Dump all data directly to db/seeds.rb:

$ rake db:seed:dump

Result:

Product.create!([
  {category_id: 1, description: "Long Sleeve Shirt", name: "Long Sleeve Shirt"},
  {category_id: 3, description: "Plain White Tee Shirt", name: "Plain T-Shirt"}
])
User.create!([
  {id: 1, password: "123456", username: "test_1"},
  {id: 2, password: "234567", username: "test_2"}
])

Dump only data from the users table and dump a maximum of 1 record:

$ rake db:seed:dump MODELS=User LIMIT=1

Result:

User.create!([
  {id: 1, password: "123456", username: "test_1"}
])

Append to db/seeds.rb instead of overwriting it:

rake db:seed:dump APPEND=true

Use another output file instead of db/seeds.rb:

rake db:seed:dump FILE=db/seeds/users.rb

There are more options that can be set— see below for all of them.

Console

Output a dump of all User records:

irb(main):001:0> puts SeedDump.dump(User)
User.create!([
  {id: 1, password: "123456", username: "test_1"},
  {id: 2, password: "234567", username: "test_2"}
])

Write the dump to a file:

irb(main):002:0> SeedDump.dump(User, file: 'db/seeds.rb')

Append the dump to a file:

irb(main):003:0> SeedDump.dump(User, file: 'db/seeds.rb', append: true)

Options are specified as a Hash for the second argument.

Options

Options are common to both the Rake task and the console, except where noted.

append: If set to true, append the data to the file instead of overwriting it. Default: false.

batch_size: Controls the number of records that are written to file at a given time. Default: 1000. If you're running out of memory when dumping, try decreasing this. If things are dumping too slow, trying increasing this.

exclude: Attributes to be excluded from the dump. Default: id, created_at, updated_at. If you need to include these columns pass "EXCLUDE=" as empty and the columns will be included.

file: Write to the specified output file. Default in Rake task is db/seeds.rb. Console returns the dump as a string by default.

limit: Dump no more than this amount of data. Default: no limit. Rake task only. In the console just pass in an ActiveRecord::Relation with the appropriate limit (e.g. SeedDump.dump(User.limit(5))).

model[s]: Restrict the dump to the specified comma-separated list of models. Default: all models. If you are using a Rails engine you can dump a specific model by passing "EngineName::ModelName". Rake task only.

Running Tests

bundle exec rake spec

seed_dump's People

Contributors

rhalff avatar rroblak avatar alexesdev avatar guilhermesimoes avatar h6y3 avatar limratana avatar ognevsky avatar uberbrodt avatar cschramm avatar tapocol avatar jhirbour avatar luka-n avatar marythought avatar nobrick avatar olegpasko avatar

Watchers

Adrian Pike avatar Michael Long avatar  avatar angel brown avatar Andrew Rich avatar Nick Cox avatar James Cloos avatar  avatar Blue Box Implementations Team avatar Carol Glenn avatar David Jantzen avatar Jeff Oberlander avatar  avatar Joshua McClintock avatar Alyssa Roush-Tatum avatar  avatar Skylar Broad avatar  avatar  avatar  avatar  avatar  avatar R Marckel avatar  avatar Micah Graff avatar  avatar jenkins-navcan avatar  avatar  avatar  avatar Howard Jess avatar  avatar Ajay Jolly avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.