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

This commit is contained in:
Felix Mosheev 2019-07-07 12:11:39 +03:00 committed by Igor Savin
parent 0e59624b11
commit 8b31a43761
4 changed files with 43 additions and 17 deletions

View File

@ -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'));

View File

@ -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);
});
};

View File

@ -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');
});
});
};

View File

@ -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`