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

No client override during migrate:make #5109

Merged
merged 3 commits into from
Apr 11, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function openKnexfile(configPath) {
return config;
}

async function initKnex(env, opts) {
async function initKnex(env, opts, noClientOverride) {
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
Expand All @@ -54,24 +54,27 @@ async function initKnex(env, opts) {

env.configuration = env.configPath
? await openKnexfile(env.configPath)
: mkConfigObj(opts);
: mkConfigObj(opts, noClientOverride);

const resolvedConfig = resolveEnvironmentConfig(
let resolvedConfig = resolveEnvironmentConfig(
opts,
env.configuration,
env.configPath
);

const optionsConfig = parseConfigObj(opts);
const config = merge(resolvedConfig, optionsConfig);
// In the other case, config is already override in mkConfigObj.
if (env.configPath) {
const optionsConfig = parseConfigObj(opts, noClientOverride);
resolvedConfig = merge(resolvedConfig, optionsConfig);
}

// Migrations directory gets defaulted if it is undefined.
if (!env.configPath && !config.migrations.directory) {
config.migrations.directory = null;
if (!env.configPath && !resolvedConfig.migrations.directory) {
resolvedConfig.migrations.directory = null;
}

const knex = require(env.modulePath);
return knex(config);
return knex(resolvedConfig);
}

function invoke() {
Expand Down Expand Up @@ -123,8 +126,6 @@ function invoke() {
configuration: null,
};

// rechoir.prepare(interpret.jsVariants, configPath, cwd);

let modulePackage = {};
try {
modulePackage = require(path.join(
Expand Down Expand Up @@ -210,7 +211,7 @@ function invoke() {
.action(async (name) => {
const opts = commander.opts();
opts.client = opts.client || 'sqlite3'; // We don't really care about client when creating migrations
const instance = await initKnex(env, opts);
const instance = await initKnex(env, opts, true);
const ext = getMigrationExtension(env, opts);
const configOverrides = { extension: ext };

Expand Down
14 changes: 4 additions & 10 deletions bin/utils/cli-config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ const tildify = require('tildify');
const color = require('colorette');
const argv = require('getopts')(process.argv.slice(2));

function findCaseInsensitiveProperty(propertyName, object) {
return Object.keys(object).find(
(key) => key.toLowerCase() === propertyName.toLowerCase()
);
}

function parseConfigObj(opts) {
function parseConfigObj(opts, noClientOverride) {
const config = { migrations: {} };

if (opts.client) {
if (opts.client && (!noClientOverride || !config.client)) {
config.client = opts.client;
}

Expand All @@ -34,7 +28,7 @@ function parseConfigObj(opts) {
return config;
}

function mkConfigObj(opts) {
function mkConfigObj(opts, noClientOverride) {
if (!opts.client) {
throw new Error(
`No configuration file found and no commandline connection parameters passed`
Expand All @@ -44,7 +38,7 @@ function mkConfigObj(opts) {
const envName = opts.env || process.env.NODE_ENV || 'development';
const resolvedClientName = resolveClientNameWithAliases(opts.client);
const useNullAsDefault = resolvedClientName === 'sqlite3';
const parsedConfig = parseConfigObj(opts);
const parsedConfig = parseConfigObj(opts, noClientOverride);

return {
ext: DEFAULT_EXT,
Expand Down
27 changes: 27 additions & 0 deletions test/cli/migrate-make.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,33 @@ module.exports = {
);
});

it('Create new migration with default knexfile with pg client', () => {
fileHelper.registerGlobForCleanup(
'test/jake-util/knexfile_migrations/*_somename.js'
);
fileHelper.createFile(
process.cwd() + '/knexfile.js',
`
module.exports = {
client: 'pg',
connection: {
filename: __dirname + '/test/jake-util/test.sqlite3',
},
migrations: {
directory: __dirname + '/test/jake-util/knexfile_migrations',
},
};
`,
{ isPathAbsolute: true }
);
return execCommand(
`node ${KNEX} migrate:make somename --knexpath=../knex.js`,
{
expectedOutput: 'Created Migration',
}
);
});

it('Create new migration with default ts knexfile', async () => {
fileHelper.registerGlobForCleanup(
'test/jake-util/knexfile_migrations/*_somename1.ts'
Expand Down