From 76d616d37e245e6dca42df431f1261ab7014e980 Mon Sep 17 00:00:00 2001 From: Dimi Kot Date: Fri, 12 Jan 2024 16:43:24 -0800 Subject: [PATCH] Hide external exports in Ent Framework; export barrel file --- src/Dest.ts | 14 +++++------ src/Grid.ts | 24 +++++++++---------- src/Patch.ts | 16 ++++++------- src/Psql.ts | 2 +- src/Registry.ts | 2 +- src/cli.ts | 12 +++++----- src/render.ts | 8 +++---- src/utils/__tests__/extractVars.test.ts | 4 ++-- .../validateCreateIndexConcurrently.test.ts | 8 +++---- src/utils/extractVars.ts | 6 ++--- src/utils/makeMigration.ts | 2 +- src/utils/validateCreateIndexConcurrently.ts | 10 ++++---- 12 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/Dest.ts b/src/Dest.ts index 4b50094..0bf33df 100644 --- a/src/Dest.ts +++ b/src/Dest.ts @@ -14,7 +14,7 @@ export class Dest { public readonly user: string, public readonly pass: string, public readonly db: string, - public readonly schema: string + public readonly schema: string, ) {} /** @@ -27,7 +27,7 @@ export class Dest { this.user, this.pass, this.db, - schema + schema, ); } @@ -45,7 +45,7 @@ export class Dest { async runFile( file: string, newVersions: string[] | null, - onOut: (proc: Psql) => void = () => {} + onOut: (proc: Psql) => void = () => {}, ) { const psql = new Psql( this, @@ -89,7 +89,7 @@ export class Dest { "DISCARD SEQUENCES;", // Commit both the migration and the version. "COMMIT;", - ].join("\n") + ].join("\n"), ); return psql.run(onOut); } @@ -99,7 +99,7 @@ export class Dest { */ async loadSchemas() { return this.queryCol( - "SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE '%\\_%'" + "SELECT nspname FROM pg_namespace WHERE nspname NOT LIKE '%\\_%'", ); } @@ -119,7 +119,7 @@ export class Dest { WHERE proname = ${this.escape(FUNC_NAME)} AND nspname IN(${inClause}) `); const selects = schemasWithFunc.map( - ([schema]) => `SELECT ${this.escape(schema)}, ${schema}.${FUNC_NAME}()` + ([schema]) => `SELECT ${this.escape(schema)}, ${schema}.${FUNC_NAME}()`, ); const rows: string[][] = []; for (const list of chunk(selects, 1000)) { @@ -127,7 +127,7 @@ export class Dest { } const versionsBySchema = new Map( - schemas.map((schema) => [schema, [] as string[]]) + schemas.map((schema) => [schema, [] as string[]]), ); for (const [schema, versionsStr] of rows) { versionsBySchema.set(schema, JSON.parse(versionsStr)); diff --git a/src/Grid.ts b/src/Grid.ts index 97a3287..11c8d19 100644 --- a/src/Grid.ts +++ b/src/Grid.ts @@ -16,7 +16,7 @@ export class Grid { private chains: Chain[], private workersPerHost: number, private beforeChains: Chain[] = [], - private afterChains: Chain[] = [] + private afterChains: Chain[] = [], ) {} get workers(): readonly Worker[] { @@ -61,7 +61,7 @@ export class Grid { } await Promise["all"]( - this._workers.map(async (worker) => worker.run(onChange)) + this._workers.map(async (worker) => worker.run(onChange)), ); if (this.numErrors) { return false; @@ -82,12 +82,12 @@ export class Grid { } this._totalMigrations += sum( - chainsQueue.map((entry) => entry.migrations.length) + chainsQueue.map((entry) => entry.migrations.length), ); } await Promise["all"]( - this._workers.map(async (worker) => worker.run(onChange)) + this._workers.map(async (worker) => worker.run(onChange)), ); // "After" sequence (we run it even on errors above). We don't clear @@ -97,7 +97,7 @@ export class Grid { } await Promise["all"]( - this._workers.map(async (worker) => worker.run(onChange)) + this._workers.map(async (worker) => worker.run(onChange)), ); if (this.numErrors) { return false; @@ -117,7 +117,7 @@ class Worker { constructor( private chainsQueue: Chain[], - private semaphores: Record + private semaphores: Record, ) {} get succeededMigrations(): number { @@ -173,21 +173,21 @@ class Worker { private async processMigration( dest: Dest, - migration: Migration + migration: Migration, ): Promise { this._curLine = "waiting to satisfy parallelism limits..."; const releases = await Promise["all"]([ this.acquireSemaphore( migration.file.runAlone ? 1 : Number.POSITIVE_INFINITY, - "alone" + "alone", ), this.acquireSemaphore( migration.file.parallelismGlobal, - migration.version + migration.version, ), this.acquireSemaphore( migration.file.parallelismPerHost, - dest.host + ":" + migration.version + dest.host + ":" + migration.version, ), ]); try { @@ -197,7 +197,7 @@ class Worker { migration.newVersions, (proc) => { this._curLine = proc.lastOutLine; - } + }, ); if (res.code) { throw res.out.trimEnd(); @@ -205,7 +205,7 @@ class Worker { if (migration.file.delay > 0) { await new Promise((resolve) => - setTimeout(resolve, migration.file.delay) + setTimeout(resolve, migration.file.delay), ); } } finally { diff --git a/src/Patch.ts b/src/Patch.ts index 3377861..9822f9e 100644 --- a/src/Patch.ts +++ b/src/Patch.ts @@ -33,7 +33,7 @@ export class Patch { constructor( private hosts: Dest[], private registry: Registry, - private mode: Mode + private mode: Mode, ) {} async getChains(): Promise { @@ -50,7 +50,7 @@ export class Patch { } const chains = await Promise["all"]( - this.hosts.map(async (hostDest) => this.getHostChains(hostDest)) + this.hosts.map(async (hostDest) => this.getHostChains(hostDest)), ); return flatten(chains); } @@ -64,21 +64,21 @@ export class Patch { this.getSchemaChain( hostDest.createSchemaDest(schema), dbVersions.get(schema)!, - reEntries.get(schema)! - ) + reEntries.get(schema)!, + ), ); return sortBy( chains.filter((chain) => chain && chain.migrations.length > 0) as Chain[], (chain) => chain.dest.host, (chain) => chain.dest.db, - (chain) => chain.dest.schema + (chain) => chain.dest.schema, ); } private getSchemaChain( dest: Dest, dbVersions: string[], - reEntries: Entry[] + reEntries: Entry[], ): Chain | null { try { if (!this.mode.undo) { @@ -94,7 +94,7 @@ export class Patch { private getChainUp( dest: Dest, dbVersions: string[], - reEntries: Entry[] + reEntries: Entry[], ): Chain { for (let i = 0; i < reEntries.length; i++) { if (i >= dbVersions.length) { @@ -142,7 +142,7 @@ export class Patch { dest: Dest, dbVersions: string[], reEntries: Entry[], - undoVersion: string + undoVersion: string, ): Chain | null { undoVersion = this.registry.extractVersion(undoVersion); if (dbVersions[dbVersions.length - 1] === undoVersion) { diff --git a/src/Psql.ts b/src/Psql.ts index 5a15502..4587ec1 100644 --- a/src/Psql.ts +++ b/src/Psql.ts @@ -21,7 +21,7 @@ export class Psql { private dest: Dest, private cwd: string, args: string[], - private stdin: string + private stdin: string, ) { this._args = ["-X", ...args]; this._cmdline = "psql " + quote(this._args); diff --git a/src/Registry.ts b/src/Registry.ts index 3c6624b..4adaad9 100644 --- a/src/Registry.ts +++ b/src/Registry.ts @@ -79,7 +79,7 @@ export class Registry { // This is needed later for duplicates removal (e.g. if some schema // name matches "sh0000" pattern, it shouldn't match "sh" pattern later). this.entriesByPrefix = new DefaultMap( - sortBy(Array.from(this.entriesByPrefix), ([prefix]) => -prefix.length) + sortBy(Array.from(this.entriesByPrefix), ([prefix]) => -prefix.length), ); } diff --git a/src/cli.ts b/src/cli.ts index 7b0fedc..3c81767 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -39,7 +39,7 @@ export async function main() { "undo", "make", ], - ["dry", "ci", "list"] + ["dry", "ci", "list"], ); return migrate({ hosts: args @@ -81,8 +81,8 @@ export async function migrate(options: { options.user, options.pass, options.db, - "public" - ) + "public", + ), ); const registry = new Registry(options.migDir); @@ -107,7 +107,7 @@ export async function migrate(options: { if (!registry.prefixes.includes(schemaPrefix)) { printText( - `WARNING: schema prefix "${schemaPrefix}" wasn't found. Valid prefixes:` + `WARNING: schema prefix "${schemaPrefix}" wasn't found. Valid prefixes:`, ); for (const prefix of registry.prefixes) { printText(`- ${prefix}`); @@ -118,7 +118,7 @@ export async function migrate(options: { const createdFiles = await makeMigration( options.migDir, migrationName, - schemaPrefix + schemaPrefix, ); for (const file of createdFiles) { printText(file); @@ -191,7 +191,7 @@ export async function migrate(options: { if (!options.ci) { logUpdate(lines.slice(0, (process.stdout.rows || 20) - 1).join("\n")); } - }, 100) + }, 100), ); if (!options.ci) { logUpdate.clear(); diff --git a/src/render.ts b/src/render.ts index 2dcdc0d..981f76b 100644 --- a/src/render.ts +++ b/src/render.ts @@ -22,7 +22,7 @@ export function renderGrid(grid: Grid) { for (const worker of sortBy( grid.workers, (worker) => worker.curDest?.host, - (worker) => worker.curDest?.schema + (worker) => worker.curDest?.schema, )) { if (worker.curDest) { activeRows.push([ @@ -112,7 +112,7 @@ export function renderPatchSummary(chains: Chain[]): [string, boolean] { "Migrations to apply:\n" + (rows.length ? rows : [""]) .map((s) => " * " + s) - .join("\n") + .join("\n"), ), rows.length > 0, ]; @@ -131,12 +131,12 @@ export async function renderLatestVersions(dests: Dest[], registry: Registry) { .getOrAdd(versions[versions.length - 1] || "", []) .push(formatHost(dest.host) + ":" + schema); } - }) + }), ); const rows = []; for (const [key, dests] of sortBy( Array.from(destsGrouped), - ([key]) => key + ([key]) => key, ).reverse()) { rows.push(collapse(dests) + ": " + (key || "")); } diff --git a/src/utils/__tests__/extractVars.test.ts b/src/utils/__tests__/extractVars.test.ts index 96959dd..af0ecbc 100644 --- a/src/utils/__tests__/extractVars.test.ts +++ b/src/utils/__tests__/extractVars.test.ts @@ -4,7 +4,7 @@ test("extractVars", () => { expect( extractVars( "abc", - "some\n--$delay = 10\nother\n-- $parallelism_global=1\ntail" - ) + "some\n--$delay = 10\nother\n-- $parallelism_global=1\ntail", + ), ).toEqual({ $delay: 10, $parallelism_global: 1 }); }); diff --git a/src/utils/__tests__/validateCreateIndexConcurrently.test.ts b/src/utils/__tests__/validateCreateIndexConcurrently.test.ts index 88ff944..d7c8a32 100644 --- a/src/utils/__tests__/validateCreateIndexConcurrently.test.ts +++ b/src/utils/__tests__/validateCreateIndexConcurrently.test.ts @@ -4,8 +4,8 @@ test("validateCreateIndexConcurrently errors", () => { expect( validateCreateIndexConcurrently( 'CREATE INDEX CONCURRENTLY "abc" ON tbl(col);', - {} - ) + {}, + ), ).toEqual([ '(due to having "CREATE INDEX CONCURRENTLY")', 'start with "COMMIT;"', @@ -24,7 +24,7 @@ test("validateCreateIndexConcurrently success", () => { CREATE INDEX CONCURRENTLY "abc""def" ON tbl(col);', BEGIN; CREATE TABLE some(id bigint);`, - { $parallelism_global: 1 } - ) + { $parallelism_global: 1 }, + ), ).toEqual([]); }); diff --git a/src/utils/extractVars.ts b/src/utils/extractVars.ts index 7e1bbf8..e3fd673 100644 --- a/src/utils/extractVars.ts +++ b/src/utils/extractVars.ts @@ -16,7 +16,7 @@ const VALID_VARS = [ ] as const; export type Vars = { - [k in typeof VALID_VARS[number]]?: number; + [k in (typeof VALID_VARS)[number]]?: number; }; export function extractVars(fileName: string, content: string): Vars { @@ -28,11 +28,11 @@ export function extractVars(fileName: string, content: string): Vars { return Object.fromEntries( pairs.map(([k, v]) => { - if (!VALID_VARS.includes(k as typeof VALID_VARS[number])) { + if (!VALID_VARS.includes(k as (typeof VALID_VARS)[number])) { throw `Unknown variable ${k} in ${fileName}`; } return [k, parseInt(v.trim())]; - }) + }), ); } diff --git a/src/utils/makeMigration.ts b/src/utils/makeMigration.ts index 357971c..d7942d0 100644 --- a/src/utils/makeMigration.ts +++ b/src/utils/makeMigration.ts @@ -5,7 +5,7 @@ import moment from "moment"; export async function makeMigration( migrationDir: string, migrationName: string, - schemaPrefix: string + schemaPrefix: string, ) { const utcTimestamp = moment(Date.now()).utc().format("YYYYMMDDHHmmss"); diff --git a/src/utils/validateCreateIndexConcurrently.ts b/src/utils/validateCreateIndexConcurrently.ts index f383fd1..d23aa52 100644 --- a/src/utils/validateCreateIndexConcurrently.ts +++ b/src/utils/validateCreateIndexConcurrently.ts @@ -3,7 +3,7 @@ import type { Vars } from "./extractVars"; export function validateCreateIndexConcurrently( content: string, - vars: Vars + vars: Vars, ): string[] { content = content .replace(/--[^\n]*/gm, "") @@ -21,11 +21,11 @@ export function validateCreateIndexConcurrently( const index = RegExp.$1; if ( !content.match( - new RegExp(`DROP INDEX IF EXISTS ${escapeRegExp(index)}`, "is") + new RegExp(`DROP INDEX IF EXISTS ${escapeRegExp(index)}`, "is"), ) ) { errors.push( - `include "DROP INDEX IF EXISTS ${index};" statement before "CREATE INDEX CONCURRENTLY"` + `include "DROP INDEX IF EXISTS ${index};" statement before "CREATE INDEX CONCURRENTLY"`, ); } } @@ -38,7 +38,7 @@ export function validateCreateIndexConcurrently( ] as const; if (!requiredVars.some((k) => !!vars[k])) { errors.unshift( - "start with one of the following vars: " + requiredVars.join(", ") + "start with one of the following vars: " + requiredVars.join(", "), ); } @@ -48,7 +48,7 @@ export function validateCreateIndexConcurrently( if (!content.match(/\bBEGIN\s*;/is)) { errors.push( - 'end with "BEGIN;" with other optional SQL statements after it' + 'end with "BEGIN;" with other optional SQL statements after it', ); } }