Use extension from knexfile for generating migrations unless overriden (#3282)

This commit is contained in:
Igor Savin 2019-06-14 08:44:02 +02:00 committed by GitHub
parent a65a95bc67
commit 04b12f234b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 11 deletions

View File

@ -12,7 +12,7 @@ const fs = Promise.promisifyAll(require('fs'));
const cliPkg = require('../package');
const {
mkConfigObj,
tryLoadingDefaultConfiguration,
resolveKnexFilePath,
} = require('./utils/cli-config-utils');
const { DEFAULT_EXT } = require('./utils/constants');
@ -68,8 +68,15 @@ function initKnex(env, opts) {
}
if (!opts.knexfile) {
const configuration = tryLoadingDefaultConfiguration();
const configurationPath = resolveKnexFilePath();
const configuration = configurationPath
? require(configurationPath.path)
: undefined;
env.configuration = configuration || mkConfigObj(opts);
if (!env.configuration.ext && configurationPath) {
env.configuration.ext = configurationPath.extension;
}
}
// If knexfile is specified
else {
@ -83,6 +90,12 @@ function initKnex(env, opts) {
'Knexfile not found. Specify a path with --knexfile or pass --client and --connection params in commandline'
);
}
if (!env.configuration.ext) {
env.configuration.ext = path
.extname(resolvedKnexfilePath)
.replace('.', '');
}
}
let environment = opts.env || process.env.NODE_ENV;

View File

@ -27,15 +27,21 @@ function mkConfigObj(opts) {
};
}
function tryLoadingDefaultConfiguration() {
function resolveKnexFilePath() {
const jsPath = resolveDefaultKnexfilePath('js');
if (fs.existsSync(jsPath)) {
return require(jsPath);
return {
path: jsPath,
extension: 'js',
};
}
const tsPath = resolveDefaultKnexfilePath('ts');
if (fs.existsSync(tsPath)) {
return require(tsPath);
return {
path: tsPath,
extension: 'ts',
};
}
console.warn(
@ -51,5 +57,5 @@ function resolveDefaultKnexfilePath(extension) {
module.exports = {
mkConfigObj,
tryLoadingDefaultConfiguration,
resolveKnexFilePath,
};

View File

@ -35,16 +35,39 @@ describe('migrate:make', () => {
);
});
it('Create new migration with js knexfile passed', () => {
it('Create new migration with js knexfile passed', async () => {
fileHelper.registerGlobForCleanup(
'test/jake-util/knexfile_migrations/*_somename.js'
);
return execCommand(
await execCommand(
`node ${KNEX} migrate:make somename --knexfile=test/jake-util/knexfile/knexfile.js --knexpath=../knex.js`,
{
expectedOutput: 'Created Migration',
}
);
const fileCount = fileHelper.fileGlobExists(
'test/jake-util/knexfile_migrations/*_somename.js'
);
expect(fileCount).to.equal(1);
});
it('Create new migration with ts knexfile passed', async () => {
fileHelper.registerGlobForCleanup(
'test/jake-util/knexfile_migrations/*_somename.ts'
);
await execCommand(
`node ${KNEX} migrate:make somename --knexfile=test/jake-util/knexfile-ts/knexfile.ts --knexpath=../knex.js`,
{
expectedOutput: 'Created Migration',
}
);
const fileCount = fileHelper.fileGlobExists(
'test/jake-util/knexfile_migrations/*_somename.ts'
);
expect(fileCount).to.equal(1);
});
it('Create new migration with default knexfile', () => {
@ -74,9 +97,9 @@ module.exports = {
);
});
it('Create new migration with default ts knexfile', () => {
it('Create new migration with default ts knexfile', async () => {
fileHelper.registerGlobForCleanup(
'test/jake-util/knexfile_migrations/*_somename.js'
'test/jake-util/knexfile_migrations/*_somename.ts'
);
fileHelper.createFile(
process.cwd() + '/knexfile.ts',
@ -93,12 +116,17 @@ module.exports = {
`,
{ isPathAbsolute: true }
);
return execCommand(
await execCommand(
`node ${KNEX} migrate:make somename --knexpath=../knex.js`,
{
expectedOutput: 'Created Migration',
}
);
const fileCount = fileHelper.fileGlobExists(
'test/jake-util/knexfile_migrations/*_somename.ts'
);
expect(fileCount).to.equal(1);
});
it('Create new migration with ts extension using -x switch', async () => {

View File

@ -0,0 +1,9 @@
module.exports = {
client: 'sqlite3',
connection: {
filename: __dirname + '/../test.sqlite3',
},
migrations: {
directory: __dirname + '/../knexfile_migrations',
},
};