graphile-migrate
Version:
Opinionated SQL-powered migration tool for PostgreSQL
60 lines • 3.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const pgMinify = require("pg-minify");
const fs_1 = require("fs");
const current_1 = require("../current");
const migration_1 = require("../migration");
const settings_1 = require("../settings");
const _common_1 = require("./_common");
const migrate_1 = require("./migrate");
const reset_1 = require("./reset");
async function _uncommit(parsedSettings) {
// Determine the last migration
const allMigrations = await migration_1.getAllMigrations(parsedSettings);
const lastMigration = allMigrations[allMigrations.length - 1];
if (!lastMigration) {
throw new Error("There's no committed migration to uncommit");
}
// Check current.sql is blank
const currentLocation = await current_1.getCurrentMigrationLocation(parsedSettings);
const currentBody = await current_1.readCurrentMigration(parsedSettings, currentLocation);
const minifiedCurrentBody = pgMinify(currentBody);
if (minifiedCurrentBody !== "") {
throw new Error("Cannot uncommit - current migration is not blank.");
}
// Restore current.sql from migration
const lastMigrationFilepath = lastMigration.fullPath;
const contents = await fs_1.promises.readFile(lastMigrationFilepath, "utf8");
const { headers, body } = migration_1.parseMigrationText(lastMigrationFilepath, contents);
// Drop Hash, Previous and AllowInvalidHash from headers; then write out
const { Hash, Previous, AllowInvalidHash } = headers, otherHeaders = tslib_1.__rest(headers, ["Hash", "Previous", "AllowInvalidHash"]);
const completeBody = migration_1.serializeMigration(body, otherHeaders);
await current_1.writeCurrentMigration(parsedSettings, currentLocation, completeBody);
// Delete the migration from committed and from the DB
await fs_1.promises.unlink(lastMigrationFilepath);
await migration_1.undoMigration(parsedSettings, lastMigration);
parsedSettings.logger.info(`graphile-migrate: migration '${lastMigrationFilepath}' undone`);
// Reset shadow
await reset_1._reset(parsedSettings, true);
await migrate_1._migrate(parsedSettings, true, true);
}
exports._uncommit = _uncommit;
async function uncommit(settings) {
const parsedSettings = await settings_1.parseSettings(settings, true);
return _uncommit(parsedSettings);
}
exports.uncommit = uncommit;
exports.uncommitCommand = {
command: "uncommit",
aliases: [],
describe: "This command is useful in development if you need to modify your latest commit before you push/merge it, or if other DB commits have been made by other developers and you need to 'rebase' your migration onto theirs. Moves the latest commit out of the committed migrations folder and back to the current migration (assuming the current migration is empty-ish). Removes the migration tracking entry from ONLY the local database. Do not use after other databases have executed this committed migration otherwise they will fall out of sync. Assuming nothing else has changed, `graphile-migrate uncommit && graphile-migrate commit` should result in the exact same hash. Development only, and liable to cause conflicts with other developers - be careful.",
builder: {},
handler: async (argv) => {
if (argv.message !== undefined && !argv.message) {
throw new Error("Missing or empty commit message after --message flag");
}
await uncommit(await _common_1.getSettings({ configFile: argv.config }));
},
};
//# sourceMappingURL=uncommit.js.map