pg-altergen
Version:
A Node.js CLI tool for managing PostgreSQL schema changes by organizing database objects in SQL files and generating ordered migration scripts with automatic dependency resolution.
141 lines (107 loc) • 4.53 kB
JavaScript
const fs = require('fs');
// Example requires – adapt to your actual locations
const files = require('../utils/files');
const schemas = require('../processors/schemas');
const tables = require('../processors/tables');
const extensions = require('../processors/extensions');
const types = require('../processors/types');
const others = require('../processors/others');
const inserts = require('../processors/inserts');
const updates = require('../processors/updates');
const indexes = require('../processors/indexes');
const migrations = require('../processors/migrations');
/**
* generateCommand:
* 1) Collect/scan all relevant SQL parts (schemas, tables, etc.)
* 2) Construct the final “alter” script
* 3) Write to config.output_file
*/
async function generateCommand(config) {
let alter = '';
// SCHEMAS
const schema_files = files.listfiles('schemas', 'all');
schemas.process(schema_files);
// EXTENSIONS
const extension_files = files.listfiles('extensions', 'all');
extensions.process(extension_files); // Process extension files
// TYPES
const type_files = files.listfiles('types', 'all');
types.process(type_files); // Process type files
// TABLES
const table_files = files.listfiles('tables', 'all');
tables.process(table_files);
// VIEWS
const view_files = files.listfiles('views', 'all');
others.process('views', view_files);
// FUNCTIONS
const function_files = files.listfiles('functions', 'all');
others.process('functions', function_files);
// PROCEDURES
const procedure_files = files.listfiles('procedures', 'all');
others.process('procedures', procedure_files);
// TRIGGERS TODO
const trigger_files = files.listfiles('triggers', 'all');
// SEQUENCES TODO
const sequence_files = files.listfiles('sequences', 'all');
// INSERTS TODO
const insert_files = files.listfiles('inserts', 'all');
let inserts_res = await inserts.generate(insert_files);
// UPDATES TODO
const update_files = files.listfiles('updates', 'all');
let updates_res = await updates.generate(update_files);
// MIGRATIONS
const migration_files = files.listfiles('migrations', 'all');
let migrations_res = await migrations.generate(migration_files);
// GENERATE
let schemas_res = schemas.generate();
let extensions_res = extensions.generate();
let types_res = types.generate();
let tables_res = tables.generate();
let other_res = others.generate();
let drop_res = others.drop();
let indexes_res = indexes.generate(table_files);
// // INSERTS
// const insertFiles = files.listfiles('inserts', 'all');
// alter += insert.generate(insertFiles);
// Merge portions
alter += [
// CREATE SCHEMAS
...schemas_res,
// CREATE EXTENSIONS (early on, as other objects might depend on them)
...extensions_res,
// CREATE TYPES (before tables that might use them)
...types_res,
// DROP
...tables_res.drop_constraints,
...drop_res,
...indexes_res.drop_indexes,
// CREATE
...tables_res.create,
...migrations_res,
...tables_res.constraints,
...indexes_res.create_indexes,
...other_res,
...inserts_res,
...updates_res,
].join('\n-- step\n') + '\n-- step\n';
// Final version label + note
const version = require('../../package.json').version;
const now = new Date().toISOString();
let note =
`-- This file was generated by pg-altergen v${version}\n-- ${now}\n\n` +
`-- with config:\n\n-- The MIT License (MIT)\n-- © ${new Date().getFullYear()} Marek Mráz <info@marek-mraz.com>\n\n`;
// Copy config minus the Postgres password or other sensitive data
const config_copy = { ...config };
delete config_copy.postgres;
for (let key in config_copy) {
note += `-- ${key}: ${JSON.stringify(config_copy[key])}\n`;
}
// Write everything to disk
files.writeFileRecursive(config.output_file, note + '\n' + alter + '\n' + note);
console.log(`Generated SQL file: ${config.output_file}`);
if (config.create_drop_columns_file && config.drop_columns_file) {
let drop_not_included_columns = tables.drop_not_included_columns();
files.writeFileRecursive(config.drop_columns_file, `-- DROP NOT INCLUDED COLUMNS\n${note}\n${drop_not_included_columns} \n ${note}`);
}
}
module.exports = { generateCommand };