@launchql/cli
Version:
LaunchQL CLI
93 lines (92 loc) ⢠3.68 kB
JavaScript
import { LaunchQLMigrate } from '@launchql/core';
import { parsePlanFile } from '@launchql/core';
import { Logger } from '@launchql/logger';
import { existsSync } from 'fs';
import { join } from 'path';
import { getPgEnvOptions } from 'pg-env';
import { getTargetDatabase } from '../../utils/database';
const log = new Logger('migrate-status');
export default async (argv, prompter, options) => {
const cwd = argv.cwd || process.cwd();
const planPath = join(cwd, 'launchql.plan');
if (!existsSync(planPath)) {
log.error(`No launchql.plan found in ${cwd}`);
process.exit(1);
}
// Get database configuration
const pgEnv = getPgEnvOptions();
const targetDatabase = await getTargetDatabase(argv, prompter, {
message: 'Select database to check migration status'
});
const client = new LaunchQLMigrate({
host: pgEnv.host,
port: pgEnv.port,
user: pgEnv.user,
password: pgEnv.password,
database: pgEnv.database
});
try {
// Parse plan file to get package name
const planResult = parsePlanFile(planPath);
if (!planResult.data || planResult.errors.length > 0) {
log.error('Failed to parse plan file:', planResult.errors);
process.exit(1);
}
const plan = planResult.data;
// Switch to target database
const targetClient = new LaunchQLMigrate({
host: pgEnv.host,
port: pgEnv.port,
user: pgEnv.user,
password: pgEnv.password,
database: targetDatabase
});
const statusResults = await targetClient.status(plan.package);
console.log('\nš Migration Status\n');
console.log(`Database: ${targetDatabase}`);
if (statusResults.length > 0) {
const status = statusResults[0];
console.log(`Package: ${status.package}`);
console.log(`Total Deployed: ${status.totalDeployed}`);
if (status.lastChange) {
console.log(`Last Change: ${status.lastChange}`);
console.log(`Last Deployed: ${status.lastDeployed.toLocaleString()}`);
}
else {
console.log('No changes deployed yet');
}
}
else {
console.log(`Package: ${plan.package}`);
console.log('No deployment history found');
}
// Show recent changes
const recentChanges = await targetClient.getRecentChanges(targetDatabase, 5);
if (recentChanges.length > 0) {
console.log('\nš Recent Changes:\n');
recentChanges.forEach((change) => {
const status = change.deployed_at ? 'ā
' : 'ā³';
const date = change.deployed_at ? new Date(change.deployed_at).toLocaleString() : 'Not deployed';
console.log(`${status} ${change.change_name.padEnd(30)} ${date}`);
});
}
// Show pending changes
const pendingChanges = await targetClient.getPendingChanges(planPath, targetDatabase);
if (pendingChanges.length > 0) {
console.log(`\nā³ Pending Changes: ${pendingChanges.length}\n`);
pendingChanges.slice(0, 5).forEach((change) => {
console.log(` - ${change}`);
});
if (pendingChanges.length > 5) {
console.log(` ... and ${pendingChanges.length - 5} more`);
}
}
else {
console.log('\nā
All changes deployed');
}
}
catch (error) {
log.error('Failed to get migration status:', error);
process.exit(1);
}
};