UNPKG

graphile-migrate

Version:

Opinionated SQL-powered migration tool for PostgreSQL

70 lines 3.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const actions_1 = require("../actions"); const pg_1 = require("../pg"); const settings_1 = require("../settings"); const _common_1 = require("./_common"); const migrate_1 = require("./migrate"); async function _reset(parsedSettings, shadow) { const connectionString = shadow ? parsedSettings.shadowConnectionString : parsedSettings.connectionString; if (!connectionString) { throw new Error("Could not determine connection string for reset"); } await actions_1.executeActions(parsedSettings, shadow, parsedSettings.beforeReset); await pg_1.withClient(parsedSettings.rootConnectionString, parsedSettings, async (pgClient) => { const databaseName = shadow ? parsedSettings.shadowDatabaseName : parsedSettings.databaseName; if (!databaseName) { throw new Error("Database name unknown"); } const databaseOwner = parsedSettings.databaseOwner; const logSuffix = shadow ? "[shadow]" : ""; await pgClient.query(`DROP DATABASE IF EXISTS ${pg_1.escapeIdentifier(databaseName)};`); parsedSettings.logger.info(`graphile-migrate${logSuffix}: dropped database '${databaseName}'`); try { await pgClient.query(`CREATE DATABASE ${pg_1.escapeIdentifier(databaseName)} OWNER ${pg_1.escapeIdentifier(databaseOwner)};`); } catch (e) { throw new Error(`Failed to create database '${databaseName}' with owner '${databaseOwner}': ${e.message}`); } await pgClient.query(`REVOKE ALL ON DATABASE ${pg_1.escapeIdentifier(databaseName)} FROM PUBLIC;`); parsedSettings.logger.info(`graphile-migrate${logSuffix}: recreated database '${databaseName}'`); }); await actions_1.executeActions(parsedSettings, shadow, parsedSettings.afterReset); await migrate_1._migrate(parsedSettings, shadow); } exports._reset = _reset; async function reset(settings, shadow = false) { const parsedSettings = await settings_1.parseSettings(settings, shadow); return _reset(parsedSettings, shadow); } exports.reset = reset; exports.resetCommand = { command: "reset", aliases: [], describe: "Drops and re-creates the database, re-running all committed migrations from the start. **HIGHLY DESTRUCTIVE**.", builder: { shadow: { type: "boolean", default: false, description: "Applies migrations to shadow DB.", }, erase: { type: "boolean", default: false, description: "This is your double opt-in to make it clear this DELETES EVERYTHING.", }, }, handler: async (argv) => { if (!argv.erase) { // eslint-disable-next-line no-console console.error("DANGER! Reset is highly destructive. If you're sure you want to do this, please add --erase to your command."); process.exit(2); } await reset(await _common_1.getSettings({ configFile: argv.config }), argv.shadow); }, }; //# sourceMappingURL=reset.js.map