From 59cd34d0ceee48ddab68867a3db52f50e440bf68 Mon Sep 17 00:00:00 2001 From: Bohdan Shulha Date: Sun, 7 Jul 2019 12:35:24 +0300 Subject: [PATCH 1/2] fix: Respect the knexfile stub option while generating a migration --- bin/cli.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bin/cli.js b/bin/cli.js index 84321771a4..1e0e9a639e 100755 --- a/bin/cli.js +++ b/bin/cli.js @@ -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}`)); }) From 5a26f3fabaa8063b5a7e43089d8f6a95480ef3be Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Wed, 10 Jul 2019 21:10:57 +0200 Subject: [PATCH 2/2] Add missing test --- test/cli/cli-test-utils.js | 7 ++--- test/cli/migrate-make.spec.js | 33 +++++++++++++++++------ test/jake-util/knexfile-stubs/knexfile.js | 10 +++++++ test/jake-util/knexfile-stubs/table.stub | 11 ++++++++ 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 test/jake-util/knexfile-stubs/knexfile.js create mode 100644 test/jake-util/knexfile-stubs/table.stub diff --git a/test/cli/cli-test-utils.js b/test/cli/cli-test-utils.js index 6d14d4fd9a..7a1c81a98e 100644 --- a/test/cli/cli-test-utils.js +++ b/test/cli/cli-test-utils.js @@ -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'; @@ -28,7 +29,7 @@ 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); @@ -36,7 +37,7 @@ function migrationMatchesStub(stubPath, migrationGlobPath, fileHelper) { migrationGlobPath ); - return stubContent === migrationContent; + expect(migrationContent).equals(stubContent); } function setupFileHelper() { @@ -50,7 +51,7 @@ function setupFileHelper() { } module.exports = { + expectMigrationMatchesStub, migrationStubOptionSetup, - migrationMatchesStub, setupFileHelper, }; diff --git a/test/cli/migrate-make.spec.js b/test/cli/migrate-make.spec.js index 5ed6b32a11..8557eeecd2 100644 --- a/test/cli/migrate-make.spec.js +++ b/test/cli/migrate-make.spec.js @@ -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'); @@ -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 in config.migrations.directory', async () => { @@ -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 when file does not exist', async () => { diff --git a/test/jake-util/knexfile-stubs/knexfile.js b/test/jake-util/knexfile-stubs/knexfile.js new file mode 100644 index 0000000000..872a2cf6d8 --- /dev/null +++ b/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', + }, +}; diff --git a/test/jake-util/knexfile-stubs/table.stub b/test/jake-util/knexfile-stubs/table.stub new file mode 100644 index 0000000000..46fd53246a --- /dev/null +++ b/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"); +};