Skip to content

Commit

Permalink
remove forced sqlite dependency
Browse files Browse the repository at this point in the history
alternate cli initialization for commands that don’t need a client specified

create a default client when needed

code style fixes

code style fix

fix rollback issues
  • Loading branch information
dstarke committed Apr 12, 2022
1 parent f2a7527 commit 176d293
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
35 changes: 20 additions & 15 deletions bin/cli.js
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
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

0 comments on commit 176d293

Please sign in to comment.