Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Run specific seed file (closes #801) #3335

Merged
merged 2 commits into from Jul 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/cli.js
Expand Up @@ -289,9 +289,10 @@ function invoke(env) {
.command('seed:run')
.description(' Run seed files.')
.option('--verbose', 'verbose')
.option('--specific', 'run specific seed file')
.action(() => {
pending = initKnex(env, commander.opts())
.seed.run()
.seed.run({ specific: argv.specific })
.then(([log]) => {
if (log.length === 0) {
success(color.cyan('No seed files exist'));
Expand Down
9 changes: 7 additions & 2 deletions src/seed/Seeder.js
Expand Up @@ -23,13 +23,18 @@ function Seeder(knex) {
this.config = this.setConfig(knex.client.config.seeds);
}

// Runs all seed files for the given environment.
// Runs seed files for the given environment.
Seeder.prototype.run = async function(config) {
this.config = this.setConfig(config);
return this._seedData()
.bind(this)
.then(([all]) => {
return this._runSeeds(all);
const files =
config && config.specific
? all.filter((file) => file === config.specific)
: all;

return this._runSeeds(files);
});
};

Expand Down
38 changes: 24 additions & 14 deletions test/integration/seed/index.js
Expand Up @@ -5,23 +5,33 @@ const path = require('path');
const rimraf = require('rimraf');

module.exports = function(knex) {
describe('knex.seed.make', function() {
it('should create a new seed file with the make method', function() {
return knex.seed.make('test').then(function(name) {
rimraf.sync(path.dirname(name));
expect(path.basename(name)).to.equal('test.js');
});
describe('knex.seed.make', () => {
it('should create a new seed file with the make method', async () => {
const name = await knex.seed.make('test');

rimraf.sync(path.dirname(name));
expect(path.basename(name)).to.equal('test.js');
});
});

describe('knex.seed.run', function() {
it('should run all seed files in the configured seed directory', function() {
return knex.seed
.run({ directory: 'test/integration/seed/test' })
.then(([data]) => {
expect(path.basename(data[0])).to.equal('seed1.js');
expect(path.basename(data[1])).to.equal('seed2.js');
});
describe('knex.seed.run', () => {
it('should run all seed files in the configured seed directory', async () => {
const [data] = await knex.seed.run({
directory: 'test/integration/seed/test',
});

expect(path.basename(data[0])).to.equal('seed1.js');
expect(path.basename(data[1])).to.equal('seed2.js');
});

it('should run specific seed file in the configured seed directory', async () => {
const [data] = await knex.seed.run({
directory: 'test/integration/seed/test',
specific: 'seed2.js',
});

expect(data.length).to.equal(1);
expect(path.basename(data[0])).to.equal('seed2.js');
});
});
};
10 changes: 10 additions & 0 deletions test/jake/jakelib/seed-test.js
Expand Up @@ -35,6 +35,16 @@ test(taskList, 'seed:run prints verbose logs', (temp) => {
});
});

test(taskList, 'seed:run runs specific file', () => {
return assertExec(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-knexfile.js --knexpath=../knex.js --specific=second.js`
).then(({ stdout }) => {
assert.include(stdout, 'Ran 1 seed files');
assert.notInclude(stdout, 'first.js');
assert.notInclude(stdout, 'second.js');
});
});

test(taskList, 'Handles seeding errors correctly', (temp) => {
return assertExecError(
`node ${KNEX} seed:run --knexfile=test/jake-util/seeds-error-knexfile.js --knexpath=../knex.js`
Expand Down