@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
38 lines (36 loc) • 974 B
text/typescript
import { realpathSync } from "fs";
import { relative } from "path";
import { libSchema } from "../internal/names";
import { join, sql } from "../internal/quote";
import { runSqlProgressTransactional } from "../internal/runSqlProgress";
/**
* Installs/updates microsharding schema and functions.
*/
export async function install({
dsn,
schemaNameFmt,
}: {
dsn: string;
schemaNameFmt?: string;
}): Promise<void> {
const scriptPath = relative(
".",
realpathSync(`${__dirname}/../../sql/pg-microsharding-up.sql`),
);
await runSqlProgressTransactional(
dsn,
join(
[
sql`
CREATE SCHEMA IF NOT EXISTS ${libSchema()};
SET search_path TO ${libSchema()};
\\setenv PG_MICROSHARDING_SCHEMA_NAME_FMT ${schemaNameFmt ?? ""}
`,
`\\i ${scriptPath}`,
],
"\n",
),
`Updating the library at ${dsn}` +
(schemaNameFmt ? ` with schema name fmt "${schemaNameFmt}"` : ""),
);
}