Skip to content

Commit

Permalink
fix: Respect the knexfile stub option while generating a migration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Bohdan Shulha authored and kibertoad committed Jul 10, 2019
1 parent e971c51 commit c6481e8
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
8 changes: 7 additions & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,15 @@ function invoke(env) {
opts.client = opts.client || 'sqlite3'; // We don't really care about client when creating migrations
const instance = initKnex(env, opts);
const ext = getMigrationExtension(env, opts);
const configOverrides = { extension: ext };

const stub = getStubPath(env, opts);
if (stub) {
configOverrides.stub = stub;
}

pending = instance.migrate
.make(name, { extension: ext, stub })
.make(name, configOverrides)
.then((name) => {
success(color.green(`Created Migration: ${name}`));
})
Expand Down
7 changes: 4 additions & 3 deletions test/cli/cli-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const { FileTestHelper } = require('cli-testlab');
const { expect } = require('chai');

function migrationStubOptionSetup(fileHelper) {
const migrationGlobPath = 'test/jake-util/knexfile_migrations/*_somename.js';
Expand All @@ -28,15 +29,15 @@ function migrationStubOptionSetup(fileHelper) {
return { migrationGlobPath };
}

function migrationMatchesStub(stubPath, migrationGlobPath, fileHelper) {
function expectMigrationMatchesStub(stubPath, migrationGlobPath, fileHelper) {
// accepts full or relative stub path
const relativeStubPath = stubPath.replace('test/jake-util/', '');
const stubContent = fileHelper.getFileTextContent(relativeStubPath);
const [migrationContent] = fileHelper.getFileGlobTextContent(
migrationGlobPath
);

return stubContent === migrationContent;
expect(migrationContent).equals(stubContent);
}

function setupFileHelper() {
Expand All @@ -50,7 +51,7 @@ function setupFileHelper() {
}

module.exports = {
expectMigrationMatchesStub,
migrationStubOptionSetup,
migrationMatchesStub,
setupFileHelper,
};
33 changes: 25 additions & 8 deletions test/cli/migrate-make.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

const path = require('path');
const { execCommand } = require('cli-testlab');
const expect = require('chai').expect;
const { expect } = require('chai');

const KNEX = path.normalize(__dirname + '/../../bin/cli.js');
const {
migrationStubOptionSetup,
migrationMatchesStub,
expectMigrationMatchesStub,
setupFileHelper,
} = require('./cli-test-utils');

Expand Down Expand Up @@ -252,9 +252,28 @@ development: {
'test/jake-util/knexfile_migrations/*_somename.js'
);
expect(fileCount).to.equal(1);
expect(
migrationMatchesStub(stubPath, migrationGlobPath, fileHelper)
).equal(true);
expectMigrationMatchesStub(stubPath, migrationGlobPath, fileHelper);
});

it('Create a new migration with stub parameter in knexfile', async () => {
const migrationGlobPath = 'test/jake-util/knexfile-stubs/*_somename.js';
fileHelper.registerGlobForCleanup(migrationGlobPath);

await execCommand(
`node ${KNEX} migrate:make somename --knexfile=test/jake-util/knexfile-stubs/knexfile.js --knexpath=../knex.js`,
{
expectedOutput: 'Created Migration',
}
);

const fileCount = fileHelper.fileGlobExists(
'test/jake-util/knexfile-stubs/*_somename.js'
);

const stubName = 'table.stub';
const stubPath = `test/jake-util/knexfile-stubs/${stubName}`;
expect(fileCount).to.equal(1);
expectMigrationMatchesStub(stubPath, migrationGlobPath, fileHelper);
});

it('Create a new migration with --stub <name> in config.migrations.directory', async () => {
Expand All @@ -274,9 +293,7 @@ development: {
'test/jake-util/knexfile_migrations/*_somename.js'
);
expect(fileCount).to.equal(1);
expect(
migrationMatchesStub(stubPath, migrationGlobPath, fileHelper)
).equal(true);
expectMigrationMatchesStub(stubPath, migrationGlobPath, fileHelper);
});

it('Create a new migration with --stub <name> when file does not exist', async () => {
Expand Down
10 changes: 10 additions & 0 deletions test/jake-util/knexfile-stubs/knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
client: 'sqlite3',
connection: {
filename: __dirname + '/../test.sqlite3',
},
migrations: {
directory: __dirname + '/../knexfile-stubs',
stub: 'table.stub',
},
};
11 changes: 11 additions & 0 deletions test/jake-util/knexfile-stubs/table.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports.up = function (knex) {
return knex.schema.createTable("table_name", (table) => {
table.increments();

table.timestamps(true, true);
});
};

exports.down = function (knex) {
return knex.schema.dropTable("table_name");
};

0 comments on commit c6481e8

Please sign in to comment.