UNPKG

@clickup/pg-microsharding

Version:
30 lines (27 loc) 719 B
import groupBy from "lodash/groupBy"; import { ident, join, sql } from "./quote"; import { runSql } from "./runSql"; export async function changeTableOwners({ toDsn, tables, comment, }: { toDsn: string; tables: Array<{ schema: string; name: string; owner: string }>; comment?: string; }): Promise<void> { if (tables.length === 0) { return; } const queries = Object.entries(groupBy(tables, (table) => table.schema)).map( ([schema, tables]) => join( tables.map( ({ name, owner }) => sql`ALTER TABLE ONLY ${ident(schema)}.${ident(name)} OWNER TO ${ident(owner)};`, ), " ", ), ); await runSql(toDsn, join(queries, "\n"), comment); }