tgriesser/knex — pull request #391 by nsantorello (2014-07-22 00:56:08+00:00)
Title: Database seed file support

Hello,

I've been using knex and bookshelf, and they've been great! I come from a rails background and found knex missing seed files, so I've developed the functionality. Included in this pull request are:
* Seed functionality (described below) and corresponding tests and website documentation
* Made migration config directory relative instead of absolute (might have been a bug?)

### Seed functionality

Create seed files with `knex seed:make `, similar to creating migrations. Example: `knex seed:make test_data`

Run seed files with `knex seed:run`. Looks at `seeds.directory` property in `knexfile.js` for the given environment and executes all seed files.

Seed files are intended to destroy existing data if necessary and then seed with correct data. Example seed file:

```
'use strict';

exports.seed = function(knex, Promise) {
return Promise.join(
// Remove any already existing payment plans
knex('payment_plans').del(),

// Seed initial payment plans
knex('payment_plans').insert({name: 'Free', description: '30 day trial', cost: 0}),
knex('payment_plans').insert({name: 'Basic', description: '10 users', cost: 2000}),
knex('payment_plans').insert({name: 'Unlimited', description: 'unlimited users', cost: 5000})
);
};
```

Example knexfile config:
```
development: {
client: ...,
connection: { ... },
seeds: {
directory: 'seeds/dev'
}
},
```

Tests are included as well and cover things similar to what the migration tests cover.

Some notes:
* It appears that `gulp build` modifies the files in the `browser` directory. I have excluded those files in this pull request because there is a tremendous amount of differences between the one that my machine generates and what is in git.
* Made migration CLI output absolute paths instead of just filenames.
* There is some copy/pasta between functionality in `lib/migrate/index.js` and `lib/seed/index.js`. It would be good in the future to refactor to share that functionality.

Comments, thoughts, and questions are welcome!