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 cli migrate:make sqlite dependency #5106

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 20 additions & 15 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
exit,
success,
checkLocalModule,
checkConfigurationOptions,
getMigrationExtension,
getSeedExtension,
getStubPath,
Expand All @@ -42,7 +43,7 @@ async function openKnexfile(configPath) {
return config;
}

async function initKnex(env, opts, noClientOverride) {
async function initKnex(env, opts, useDefaultClientIfNotSpecified) {
checkLocalModule(env);
if (process.cwd() !== env.cwd) {
process.chdir(env.cwd);
Expand All @@ -52,29 +53,35 @@ async function initKnex(env, opts, noClientOverride) {
);
}

if (!useDefaultClientIfNotSpecified) {
checkConfigurationOptions(env, opts);
}

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

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

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

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

// Client gets defaulted if undefined and it's allowed
if (useDefaultClientIfNotSpecified && config.client === undefined) {
config.client = 'sqlite3';
}

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

function invoke() {
Expand Down Expand Up @@ -210,8 +217,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, true);
const instance = await initKnex(env, opts, true); // Skip config check, we don't really care about client when creating migrations
const ext = getMigrationExtension(env, opts);
const configOverrides = { extension: ext };

Expand Down Expand Up @@ -377,8 +383,7 @@ function invoke() {
)
.action(async (name) => {
const opts = commander.opts();
opts.client = opts.client || 'sqlite3'; // We don't really care about client when creating seeds
const instance = await initKnex(env, opts);
const instance = await initKnex(env, opts, true); // Skip config check, we don't really care about client when creating seeds
const ext = getSeedExtension(env, opts);
const configOverrides = { extension: ext };
const stub = getStubPath('seeds', env, opts);
Expand Down
23 changes: 13 additions & 10 deletions bin/utils/cli-config-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ const tildify = require('tildify');
const color = require('colorette');
const argv = require('getopts')(process.argv.slice(2));

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

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

Expand All @@ -28,17 +28,11 @@ function parseConfigObj(opts, noClientOverride) {
return config;
}

function mkConfigObj(opts, noClientOverride) {
if (!opts.client) {
throw new Error(
`No configuration file found and no commandline connection parameters passed`
);
}

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, noClientOverride);
const parsedConfig = parseConfigObj(opts);

return {
ext: DEFAULT_EXT,
Expand Down Expand Up @@ -105,6 +99,14 @@ function checkLocalModule(env) {
}
}

function checkConfigurationOptions(env, opts) {
if (!env.configPath && !opts.client) {
throw new Error(
`No configuration file found and no commandline connection parameters passed`
);
}
}

function getMigrationExtension(env, opts) {
const config = resolveEnvironmentConfig(
opts,
Expand Down Expand Up @@ -199,6 +201,7 @@ module.exports = {
exit,
success,
checkLocalModule,
checkConfigurationOptions,
getSeedExtension,
getMigrationExtension,
getStubPath,
Expand Down