pg-flyway
Version:
Migration tool for PostgreSQL database, NodeJS version of Java migration tool - flyway (not wrapper for https://flywaydb.org/documentation/commandline)
76 lines (75 loc) • 2.92 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.InfoService = void 0;
const log4js_1 = require("log4js");
const get_log_level_1 = require("../utils/get-log-level");
const history_table_service_1 = require("./history-table.service");
const connection_string_1 = require("connection-string");
const console_table_printer_1 = require("console-table-printer");
const date_fns_1 = require("date-fns");
class InfoService {
constructor(options) {
this.options = options;
this.logger = (0, log4js_1.getLogger)('info');
this.logger.level = (0, get_log_level_1.getLogLevel)();
this.historyTableService = new history_table_service_1.HistoryTableService(options.historyTable, options.historySchema);
}
destroy() {
if (this.client) {
this.client.release(true);
this.client = null;
}
}
async getClient() {
if (!this.client) {
if (!this.Pool) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.Pool = require('pg').Pool;
}
const pool = new this.Pool({ connectionString: this.options.databaseUrl });
this.client = await pool.connect();
}
return this.client;
}
async info() {
this.logger.info(`HistoryTable: ${this.options.historyTable}`);
const password = new connection_string_1.ConnectionString(this.options.databaseUrl).password;
if (password) {
this.logger.info(`DatabaseUrl: ${this.options.databaseUrl.split(password).join('********')}`);
}
await this.getClient();
const histories = (await this.client.query(this.historyTableService.getMigrationsHistorySql())).rows.flat();
this.logger.info(`Migrations: ${histories.length}`);
const p = new console_table_printer_1.Table();
for (const history of histories) {
let category = 'Versioned';
let undoable = 'No';
let color = 'green';
let state = 'Success';
if (!history.success) {
color = 'red';
state = 'Failed';
}
else {
if (!history.version) {
color = 'blue';
category = 'Repeatable';
}
}
if (history.type === 'UNDO_SQL') {
undoable = 'Yes';
}
p.addRow({
Category: category,
Version: history.version,
Description: history.description,
Type: history.type,
'Installed On': (0, date_fns_1.format)(history.installed_on, 'yyyy-MM-dd kk:mm:ss'),
State: state,
Undoable: undoable,
}, { color });
}
p.printTable();
}
}
exports.InfoService = InfoService;