@clickup/pg-microsharding
Version:
Microshards support for PostgreSQL
60 lines • 2.37 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTablesInSchema = getTablesInSchema;
const partition_1 = __importDefault(require("lodash/partition"));
const quote_1 = require("./quote");
const runSql_1 = require("./runSql");
/**
* Returns all tables in a given schema on fromDsn.
*
* For logical subscription purposes, we only need the `tables` that may
* potentially contain data (e.g. regular tables, or partitions). But we also
* return the base tables for partitioned tables in `partitionedTables`, since
* we validate the primary key coverage in `validatePrimaryKeysFromDump()` for sanity
* checking purposes.
*/
async function getTablesInSchema({ fromDsn, schema, }) {
const rows = await (0, runSql_1.runSql)(fromDsn, (0, quote_1.sql) `
SELECT DISTINCT ON (pg_namespace.nspname, pg_class.relname, pg_roles.rolname)
pg_namespace.nspname,
pg_class.relname,
pg_roles.rolname,
pg_class.relkind,
idx_class.relname,
pg_index.indisreplident,
pg_index.indisprimary,
pg_get_indexdef(idx_class.oid)
FROM pg_class
JOIN pg_namespace ON relnamespace = pg_namespace.oid
JOIN pg_roles ON relowner = pg_roles.oid
LEFT JOIN pg_index ON indrelid = pg_class.oid AND (indisreplident OR indisprimary)
LEFT JOIN pg_class idx_class ON idx_class.oid = pg_index.indexrelid
WHERE pg_class.relkind IN ('r', 'p') AND nspname = ${schema}
ORDER BY
pg_namespace.nspname,
pg_class.relname,
pg_roles.rolname,
pg_index.indisreplident DESC,
pg_index.indisprimary DESC
`);
const res = rows.map((row) => ({
schema: row[0],
name: row[1],
owner: row[2],
isPartitioned: row[3] === "p",
identity: row[4]
? {
indexName: row[4],
isReplIdent: row[5] === "t",
isPrimaryKey: row[6] === "t",
def: row[7],
}
: null,
}));
const [partitionedTables, tables] = (0, partition_1.default)(res, (table) => table.isPartitioned);
return { tables, partitionedTables };
}
//# sourceMappingURL=getTablesInSchema.js.map