Skip to content
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
103 changes: 40 additions & 63 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { CLIOptions, Inquirerer, Question } from 'inquirerer';
import { ParsedArgs } from 'minimist';

import {
errors,
getEnvOptions,
LaunchQLOptions
} from '@launchql/types';

import {
getPgEnvOptions,
getSpawnEnvWithPg,
} from 'pg-env';

import { deploy, deployFast } from '@launchql/core';
import { deployFast } from '@launchql/core';
import { Logger } from '@launchql/logger';
import { execSync } from 'child_process';
import { LaunchQLProject } from '@launchql/core';
import { deployCommand } from '@launchql/migrate';
import { deployProject } from '@launchql/migrate';
import { getTargetDatabase } from '../utils';
import { selectModule } from '../utils/module-utils';

export default async (
argv: Partial<ParsedArgs>,
Expand Down Expand Up @@ -71,77 +71,54 @@ export default async (

log.debug(`Using current directory: ${cwd}`);

const project = new LaunchQLProject(cwd);

if (createdb) {
log.info(`Creating database ${database}...`);
execSync(`createdb ${database}`, {
env: getSpawnEnvWithPg(pgEnv)
});
}

const options: LaunchQLOptions = getEnvOptions({
pg: {
database
}
});

let projectName: string | undefined;
if (recursive) {
const modules = await project.getModules();
const moduleNames = modules.map(mod => mod.getModuleName());

if (!moduleNames.length) {
log.error('No modules found in the specified directory.');
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}
projectName = await selectModule(argv, prompter, 'Choose a project to deploy', cwd);
log.info(`Selected project: ${projectName}`);
}

const { project: selectedProject } = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'project',
message: 'Choose a project to deploy',
options: moduleNames,
required: true
// Handle fast deploy separately as it uses a different API
if (argv.fast && recursive) {
const options: LaunchQLOptions = getEnvOptions({
pg: {
database
}
]);

const selected = modules.find(mod => mod.getModuleName() === selectedProject);
if (!selected) {
throw new Error(`Module ${selectedProject} not found`);
}

const dir = selected.getModulePath()!;
log.success(`Deploying project ${selectedProject} from ${dir} to database ${database}...`);

if (argv.fast) {
await deployFast({
opts: options,
database,
dir,
name: selectedProject,
usePlan: true,
cache: false
});
} else {
await deploy(options, selectedProject, database, dir, { useSqitch, useTransaction: tx });
}

log.success('Deployment complete.');
});

// Fast deploy needs the module path, so we need to get it
// This is a limitation of the current fast deploy API
const { LaunchQLProject } = await import('@launchql/core');
const project = new LaunchQLProject(cwd);
const modules = project.getModuleMap();
const modulePath = modules[projectName!].path;

await deployFast({
opts: options,
database,
dir: modulePath,
name: projectName!,
usePlan: true,
cache: false
});
} else {
if (useSqitch) {
log.info(`Running: sqitch deploy db:pg:${database} (using legacy Sqitch)`);
execSync(`sqitch deploy db:pg:${database}`, {
cwd,
env: getSpawnEnvWithPg(pgEnv),
stdio: 'inherit'
});
} else {
log.info(`Running: launchql migrate deploy db:pg:${database}`);
await deployCommand(pgEnv, database, cwd, { useTransaction: tx });
}
log.success('Deployment complete.');
await deployProject({
database,
cwd,
recursive,
projectName,
useSqitch,
useTransaction: tx
});
}

log.success('Deployment complete.');

return argv;
};
63 changes: 15 additions & 48 deletions packages/cli/src/commands/revert.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { CLIOptions, Inquirerer, Question } from 'inquirerer';
import { LaunchQLProject, revert } from '@launchql/core';
import { errors, getEnvOptions, LaunchQLOptions } from '@launchql/types';
import { getPgEnvOptions, getSpawnEnvWithPg } from 'pg-env';
import { Logger } from '@launchql/logger';
import { revertCommand } from '@launchql/migrate';
import { execSync } from 'child_process';
import { revertProject } from '@launchql/migrate';
import { getTargetDatabase } from '../utils';
import { selectModule } from '../utils/module-utils';

const log = new Logger('revert');

Expand Down Expand Up @@ -45,52 +42,22 @@ export default async (

log.debug(`Using current directory: ${cwd}`);

const project = new LaunchQLProject(cwd);

let projectName: string | undefined;
if (recursive) {
const modules = await project.getModules();
const moduleNames = modules.map(mod => mod.getModuleName());

if (!moduleNames.length) {
log.error('No modules found in the specified directory.');
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}

const { project: selectedProject } = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'project',
message: 'Choose a project to revert',
options: moduleNames,
required: true
}
]);
projectName = await selectModule(argv, prompter, 'Choose a project to revert', cwd);
log.info(`Selected project: ${projectName}`);
}

log.success(`Reverting project ${selectedProject} on database ${database}...`);
const options: LaunchQLOptions = getEnvOptions({
pg: {
database
}
});
await revertProject({
database,
cwd,
recursive,
projectName,
useSqitch,
useTransaction: tx
});

await revert(options, selectedProject, database, cwd, { useSqitch, useTransaction: tx });
log.success('Revert complete.');
} else {
const pgEnv = getPgEnvOptions();
if (useSqitch) {
log.info(`Running: sqitch revert db:pg:${database} (using legacy Sqitch)`);
execSync(`sqitch revert db:pg:${database}`, {
cwd,
env: getSpawnEnvWithPg(pgEnv),
stdio: 'inherit'
});
} else {
log.info(`Running: launchql migrate revert db:pg:${database}`);
await revertCommand(pgEnv, database, cwd, { useTransaction: tx });
}
log.success('Revert complete.');
}
log.success('Revert complete.');

return argv;
};
62 changes: 14 additions & 48 deletions packages/cli/src/commands/verify.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { CLIOptions, Inquirerer, Question } from 'inquirerer';
import { LaunchQLProject, verify } from '@launchql/core';
import { errors, getEnvOptions, LaunchQLOptions } from '@launchql/types';
import { getPgEnvOptions, getSpawnEnvWithPg } from 'pg-env';
import { Logger } from '@launchql/logger';
import { verifyCommand } from '@launchql/migrate';
import { execSync } from 'child_process';
import { verifyProject } from '@launchql/migrate';
import { getTargetDatabase } from '../utils';
import { selectModule } from '../utils/module-utils';

const log = new Logger('verify');

Expand All @@ -24,52 +21,21 @@ export default async (

log.debug(`Using current directory: ${cwd}`);

const project = new LaunchQLProject(cwd);

let projectName: string | undefined;
if (recursive) {
const modules = await project.getModules();
const moduleNames = modules.map(mod => mod.getModuleName());

if (!moduleNames.length) {
log.error('No modules found in the specified directory.');
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}

const { project: selectedProject } = await prompter.prompt(argv, [
{
type: 'autocomplete',
name: 'project',
message: 'Choose a project to verify',
options: moduleNames,
required: true
}
]);
projectName = await selectModule(argv, prompter, 'Choose a project to verify', cwd);
log.info(`Selected project: ${projectName}`);
}

const options: LaunchQLOptions = getEnvOptions({
pg: {
database
}
});
await verifyProject({
database,
cwd,
recursive,
projectName,
useSqitch
});

log.info(`Verifying project ${selectedProject} on database ${database}...`);
await verify(options, selectedProject, database, cwd, { useSqitch });
log.success('Verify complete.');
} else {
const pgEnv = getPgEnvOptions();
if (useSqitch) {
log.info(`Running: sqitch verify db:pg:${database} (using legacy Sqitch)`);
execSync(`sqitch verify db:pg:${database}`, {
cwd,
env: getSpawnEnvWithPg(pgEnv),
stdio: 'inherit'
});
} else {
log.info(`Running: launchql migrate verify db:pg:${database}`);
await verifyCommand(pgEnv, database, cwd);
}
log.success('Verify complete.');
}
log.success('Verify complete.');

return argv;
};
31 changes: 31 additions & 0 deletions packages/cli/src/utils/module-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Inquirerer } from 'inquirerer';
import { ParsedArgs } from 'minimist';
import { getAvailableModules } from '@launchql/migrate';
import { errors } from '@launchql/types';

/**
* Prompt user to select a module from available modules in the directory
*/
export async function selectModule(
argv: Partial<ParsedArgs>,
prompter: Inquirerer,
message: string,
cwd: string
): Promise<string> {
const modules = await getAvailableModules(cwd);

if (!modules.length) {
prompter.close();
throw errors.NOT_FOUND({}, 'No modules found in the specified directory.');
}

const { project } = await prompter.prompt(argv, [{
type: 'autocomplete',
name: 'project',
message,
options: modules,
required: true
}]);

return project;
}
Loading