Skip to content

Commit

Permalink
Hide external exports in Ent Framework; export barrel file
Browse files Browse the repository at this point in the history
  • Loading branch information
dimikot committed Jan 13, 2024
1 parent 437354d commit 76d616d
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 54 deletions.
14 changes: 7 additions & 7 deletions src/Dest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
) {}

/**
Expand All @@ -27,7 +27,7 @@ export class Dest {
this.user,
this.pass,
this.db,
schema
schema,
);
}

Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 '%\\_%'",
);
}

Expand All @@ -119,15 +119,15 @@ 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)) {
rows.push(...(await this.query(list.join(" UNION ALL "))));
}

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));
Expand Down
24 changes: 12 additions & 12 deletions src/Grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -117,7 +117,7 @@ class Worker {

constructor(
private chainsQueue: Chain[],
private semaphores: Record<string, Semaphore>
private semaphores: Record<string, Semaphore>,
) {}

get succeededMigrations(): number {
Expand Down Expand Up @@ -173,21 +173,21 @@ class Worker {

private async processMigration(
dest: Dest,
migration: Migration
migration: Migration,
): Promise<void> {
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 {
Expand All @@ -197,15 +197,15 @@ class Worker {
migration.newVersions,
(proc) => {
this._curLine = proc.lastOutLine;
}
},
);
if (res.code) {
throw res.out.trimEnd();
}

if (migration.file.delay > 0) {
await new Promise((resolve) =>
setTimeout(resolve, migration.file.delay)
setTimeout(resolve, migration.file.delay),
);
}
} finally {
Expand Down
16 changes: 8 additions & 8 deletions src/Patch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class Patch {
constructor(
private hosts: Dest[],
private registry: Registry,
private mode: Mode
private mode: Mode,
) {}

async getChains(): Promise<Chain[]> {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/Psql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand Down
12 changes: 6 additions & 6 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function main() {
"undo",
"make",
],
["dry", "ci", "list"]
["dry", "ci", "list"],
);
return migrate({
hosts: args
Expand Down Expand Up @@ -81,8 +81,8 @@ export async function migrate(options: {
options.user,
options.pass,
options.db,
"public"
)
"public",
),
);
const registry = new Registry(options.migDir);

Expand All @@ -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}`);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -112,7 +112,7 @@ export function renderPatchSummary(chains: Chain[]): [string, boolean] {
"Migrations to apply:\n" +
(rows.length ? rows : ["<no changes>"])
.map((s) => " * " + s)
.join("\n")
.join("\n"),
),
rows.length > 0,
];
Expand All @@ -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 || "<no versions>"));
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/__tests__/extractVars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
8 changes: 4 additions & 4 deletions src/utils/__tests__/validateCreateIndexConcurrently.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;"',
Expand All @@ -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([]);
});
6 changes: 3 additions & 3 deletions src/utils/extractVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())];
})
}),
);
}
2 changes: 1 addition & 1 deletion src/utils/makeMigration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Loading

0 comments on commit 76d616d

Please sign in to comment.