clicksuite
Version:
A CLI tool for managing ClickHouse database migrations with environment-specific configurations
261 lines (260 loc) • 11 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Db = exports.Runner = void 0;
exports.getContext = getContext;
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const dotenv_1 = __importDefault(require("dotenv"));
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const chalk_1 = __importDefault(require("chalk"));
const runner_1 = require("./runner");
// Export public API for programmatic usage
var runner_2 = require("./runner");
Object.defineProperty(exports, "Runner", { enumerable: true, get: function () { return runner_2.Runner; } });
var db_1 = require("./db");
Object.defineProperty(exports, "Db", { enumerable: true, get: function () { return db_1.Db; } });
__exportStar(require("./types"), exports);
// Load environment variables from .env file
dotenv_1.default.config();
function getContext(argv) {
const baseUserConfigDir = process.env.CLICKSUITE_MIGRATIONS_DIR || '.';
const actualMigrationsDir = path.resolve(baseUserConfigDir, 'migrations');
const environment = process.env.CLICKSUITE_ENVIRONMENT || 'development';
// Require CLICKHOUSE_URL
if (!process.env.CLICKHOUSE_URL) {
throw new Error('CLICKHOUSE_URL environment variable is required. Expected format: http://username:password@host:port/database');
}
// Parse database from URL for convenience
let database;
try {
const urlObj = new URL(process.env.CLICKHOUSE_URL);
database = urlObj.pathname.replace('/', '') || 'default';
// Basic URL validation
if (!urlObj.protocol || !urlObj.hostname) {
throw new Error('Invalid URL format');
}
}
catch (error) {
throw new Error('Invalid CLICKHOUSE_URL format. Expected: http://username:password@host:port/database');
}
// Handle cluster configuration
const cluster = process.env.CLICKHOUSE_CLUSTER;
const clusterValue = cluster && cluster.trim() !== '' ? cluster : undefined;
return {
url: process.env.CLICKHOUSE_URL,
database,
cluster: clusterValue,
migrationsDir: actualMigrationsDir,
nonInteractive: argv.nonInteractive !== undefined ? argv.nonInteractive : !!process.env.CI,
environment: environment,
dryRun: argv.dryRun !== undefined ? argv.dryRun : false,
verbose: argv.verbose !== undefined ? argv.verbose : false,
};
}
(0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
.option('non-interactive', {
alias: 'y',
type: 'boolean',
description: 'Run in non-interactive mode (confirming actions automatically)',
default: false,
})
.option('verbose', {
type: 'boolean',
description: 'Show detailed SQL logs and verbose output',
default: false,
})
.command('init', 'Initialize Clicksuite for the current project', async (argv) => {
const context = getContext(argv);
try {
console.log(chalk_1.default.blue(`ℹ️ Clicksuite base configuration directory: ${path.resolve(process.env.CLICKSUITE_MIGRATIONS_DIR || '.')}`));
console.log(chalk_1.default.blue(`⏳ Ensuring actual migrations (.yml files) directory exists at: ${context.migrationsDir}`));
await fs.mkdir(context.migrationsDir, { recursive: true });
console.log(chalk_1.default.green(`✅ Migrations directory for .yml files is ready at: ${context.migrationsDir}`));
const runner = new runner_1.Runner(context);
await runner.init(); // Runner's init handles DB table creation and connection test
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Initialization failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('generate <name>', 'Generate a new migration file', (yargsInstance) => {
return yargsInstance.positional('name', {
describe: 'Name of the migration (e.g., \'create_users_table\')',
type: 'string',
demandOption: true,
});
}, async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.generate(argv.name);
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Migration generation failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('migrate:status', 'Show the status of all migrations', async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.status();
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Failed to get migration status:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('migrate', 'Run all pending migrations (equivalent to migrate:up)', async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.migrate();
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Migration run failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('migrate:up [migrationVersion]', 'Run pending migrations. If no version is specified, runs all pending.', (yargsInstance) => {
return yargsInstance
.positional('migrationVersion', {
describe: 'Optional: Target migration version to run up to (inclusive).',
type: 'string',
})
.option('dry-run', {
describe: 'Preview migrations without executing them',
type: 'boolean',
default: false,
});
}, async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.up(argv.migrationVersion);
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Migrate UP failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('migrate:down [migrationVersion]', 'Roll back migrations. If no version, rolls back the last applied. Otherwise, rolls back the specified version.', (yargsInstance) => {
return yargsInstance
.positional('migrationVersion', {
describe: 'Optional: The migration version to roll back (e.g., \'20230101120000\'). If omitted, rolls back the last applied migration.',
type: 'string',
})
.option('dry-run', {
describe: 'Preview rollbacks without executing them',
type: 'boolean',
default: false,
});
}, async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.down(argv.migrationVersion);
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Migrate DOWN failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('migrate:reset', 'Roll back all applied migrations and clear the migrations table (requires confirmation)', async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.reset();
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Migration reset failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.command('schema:load', 'Load all local migrations into the database as APPLIED without running their SQL scripts', async (argv) => {
const context = getContext(argv);
const runner = new runner_1.Runner(context);
try {
await runner.schemaLoad();
}
catch (error) {
console.error(chalk_1.default.bold.red('❌ Schema loading failed:'), error.message);
if (error.stack && !context.nonInteractive)
console.error(chalk_1.default.gray(error.stack));
process.exit(1);
}
})
.strict()
.demandCommand(1, chalk_1.default.yellow('⚠️ Please specify a command. Use --help for available commands.'))
.alias('h', 'help')
.alias('v', 'version')
.epilogue(chalk_1.default.gray('For more information, find the documentation at https://github.com/GamebeastGG/clicksuite'))
.fail((msg, err, yargsInstance) => {
if (err && err.message && !err.message.startsWith('⚠️')) {
console.error(chalk_1.default.bold.red('❌ Error:'), err.message);
if (err.stack && !getContext({}).nonInteractive)
console.error(chalk_1.default.gray(err.stack));
}
else if (msg && !err) {
console.error(chalk_1.default.red(`❌ ${msg}`));
yargsInstance.showHelp();
}
process.exit(1);
})
.parse();