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

fix: Respect the knexfile stub option while generating a migration #3337

Merged
merged 3 commits into from Jul 10, 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
8 changes: 7 additions & 1 deletion bin/cli.js
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
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
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
@@ -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
@@ -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");
};