UNPKG

@clickup/pg-mig

Version:

PostgreSQL schema migration tool with microsharding and clustering support

66 lines 3.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.actionMake = actionMake; const fs_1 = require("fs"); const path_1 = require("path"); const compact_1 = __importDefault(require("lodash/compact")); const max_1 = __importDefault(require("lodash/max")); const moment_1 = __importDefault(require("moment")); const Registry_1 = require("../internal/Registry"); const render_1 = require("../internal/render"); const actionChain_1 = require("./actionChain"); /** * Makes new migration files. */ async function actionMake(options, registry, name) { var _a; const [migrationName, schemaPrefix] = name.split("@"); const usage = "Format: --make=migration_name@schema_prefix"; if (!(migrationName === null || migrationName === void 0 ? void 0 : migrationName.match(/^[-a-z0-9_]+$/))) { (0, render_1.printError)("migration_name is missing or includes incorrect characters (snake_case required)"); (0, render_1.printText)(usage); return false; } if (!schemaPrefix) { (0, render_1.printError)("schema_prefix is missing"); (0, render_1.printText)(usage); return false; } if (!registry.getPrefixes().includes(schemaPrefix)) { (0, render_1.printText)(`WARNING: schema prefix "${schemaPrefix}" wasn't found. Valid prefixes:`); for (const prefix of registry.getPrefixes()) { (0, render_1.printText)(`- ${prefix}`); } } (0, render_1.printText)("Making migration files..."); const createdFiles = await makeMigration(options.migDir, migrationName, schemaPrefix, (_a = (0, max_1.default)((0, compact_1.default)(registry.getVersions().map((v) => { var _a; return (_a = v.match(/^(\d+)\./)) === null || _a === void 0 ? void 0 : _a[1]; })))) !== null && _a !== void 0 ? _a : null); registry = new Registry_1.Registry(registry.dir); await (0, actionChain_1.actionChain)(options, registry); (0, render_1.printText)("Created files:"); for (const file of createdFiles) { (0, render_1.printText)(file); } return true; } async function makeMigration(migrationDir, migrationName, schemaPrefix, maxUtcTimestamp) { let utcTimestamp = (0, moment_1.default)(Date.now()).utc().format("YYYYMMDDHHmmss"); // In case we have skewed timestamp in the existing versions (like 20251424: // there is no month 14), we just add one minute to the last version timestamp // instead of using the current time. if (maxUtcTimestamp && utcTimestamp <= maxUtcTimestamp) { utcTimestamp = String(Number(maxUtcTimestamp) + 60); } const migrationFilenameBase = `${utcTimestamp}.${migrationName}.${schemaPrefix}`; const migrationFiles = [ `${migrationFilenameBase}.up.sql`, `${migrationFilenameBase}.dn.sql`, ].map((f) => (0, path_1.join)(migrationDir, f)); for (const f of migrationFiles) { (0, fs_1.writeFileSync)(f, "", { mode: 0o644 }); } return migrationFiles; } //# sourceMappingURL=actionMake.js.map