UNPKG

@clickup/pg-mig

Version:

PostgreSQL schema migration tool with microsharding and clustering support

57 lines (50 loc) 1.86 kB
import sortBy from "lodash/sortBy"; import type { MigrateOptions } from "../cli"; import type { Dest } from "../internal/Dest"; import { collapse } from "../internal/helpers/collapse"; import { DefaultMap } from "../internal/helpers/DefaultMap"; import { promiseAllMap } from "../internal/helpers/promiseAllMap"; import { printText } from "../internal/render"; /** * Prints the list of migration versions applied in the database by querying * mig_versions_const() on each schema. Groups schemas with identical version * lists together for readability. */ export async function actionListDB( _options: MigrateOptions, hostDests: Dest[], ): Promise<boolean> { // Groups schemas that share the same set of applied versions. // Key is the JSON-serialized version list, value is the list of "host:schema" names. // e.g. '["20260212173432.create_cred_data.sh"]' => ["127.0.0.1(1):sh0001", "127.0.0.1(1):sh0002"] const versionsByGroup = new DefaultMap<string, string[]>(); await promiseAllMap(hostDests, async (dest) => { const allSchemas = await dest.loadSchemas(); const versionsBySchema = await dest.loadVersionsBySchema(allSchemas); for (const [schema, versions] of versionsBySchema) { const key = JSON.stringify(versions); versionsByGroup .getOrAdd(key, []) .push(dest.getName("short") + ":" + schema); } }); if (versionsByGroup.size === 0) { printText("No schemas found in the database."); return true; } for (const [key, dests] of sortBy( Array.from(versionsByGroup), ([key]) => key, )) { const versions: string[] = JSON.parse(key); printText(`\n${collapse(dests)}:`); if (versions.length === 0) { printText(" <no versions>"); } else { for (const version of versions) { printText(` > ${version}`); } } } return true; }