@supabase/pg-delta 1.0.0-alpha.1 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/core/catalog.model.d.ts +2 -2
  2. package/dist/core/catalog.model.js +29 -29
  3. package/dist/core/context.d.ts +3 -3
  4. package/dist/core/context.js +7 -10
  5. package/dist/core/depend.d.ts +2 -2
  6. package/dist/core/depend.js +8 -7
  7. package/dist/core/objects/aggregate/aggregate.model.d.ts +2 -2
  8. package/dist/core/objects/aggregate/aggregate.model.js +7 -9
  9. package/dist/core/objects/collation/collation.model.d.ts +2 -2
  10. package/dist/core/objects/collation/collation.model.js +29 -28
  11. package/dist/core/objects/domain/domain.model.d.ts +2 -2
  12. package/dist/core/objects/domain/domain.model.js +8 -10
  13. package/dist/core/objects/event-trigger/event-trigger.model.d.ts +2 -2
  14. package/dist/core/objects/event-trigger/event-trigger.model.js +7 -9
  15. package/dist/core/objects/extension/extension.model.d.ts +2 -2
  16. package/dist/core/objects/extension/extension.model.js +8 -10
  17. package/dist/core/objects/foreign-data-wrapper/foreign-data-wrapper/foreign-data-wrapper.model.d.ts +2 -2
  18. package/dist/core/objects/foreign-data-wrapper/foreign-data-wrapper/foreign-data-wrapper.model.js +20 -22
  19. package/dist/core/objects/foreign-data-wrapper/foreign-table/foreign-table.model.d.ts +2 -2
  20. package/dist/core/objects/foreign-data-wrapper/foreign-table/foreign-table.model.js +20 -22
  21. package/dist/core/objects/foreign-data-wrapper/server/server.model.d.ts +2 -2
  22. package/dist/core/objects/foreign-data-wrapper/server/server.model.js +20 -22
  23. package/dist/core/objects/foreign-data-wrapper/user-mapping/user-mapping.model.d.ts +2 -2
  24. package/dist/core/objects/foreign-data-wrapper/user-mapping/user-mapping.model.js +20 -22
  25. package/dist/core/objects/index/index.model.d.ts +4 -4
  26. package/dist/core/objects/index/index.model.js +9 -11
  27. package/dist/core/objects/language/language.model.js +5 -7
  28. package/dist/core/objects/materialized-view/materialized-view.model.d.ts +2 -2
  29. package/dist/core/objects/materialized-view/materialized-view.model.js +8 -10
  30. package/dist/core/objects/procedure/procedure.model.d.ts +2 -2
  31. package/dist/core/objects/procedure/procedure.model.js +8 -10
  32. package/dist/core/objects/publication/publication.model.d.ts +2 -2
  33. package/dist/core/objects/publication/publication.model.js +7 -9
  34. package/dist/core/objects/rls-policy/rls-policy.model.d.ts +2 -2
  35. package/dist/core/objects/rls-policy/rls-policy.model.js +8 -10
  36. package/dist/core/objects/role/role.model.d.ts +2 -2
  37. package/dist/core/objects/role/role.model.js +28 -28
  38. package/dist/core/objects/rule/rule.model.d.ts +2 -2
  39. package/dist/core/objects/rule/rule.model.js +7 -9
  40. package/dist/core/objects/schema/schema.model.d.ts +2 -2
  41. package/dist/core/objects/schema/schema.model.js +8 -10
  42. package/dist/core/objects/sequence/sequence.model.d.ts +2 -2
  43. package/dist/core/objects/sequence/sequence.model.js +8 -10
  44. package/dist/core/objects/subscription/subscription.model.d.ts +2 -2
  45. package/dist/core/objects/subscription/subscription.model.js +25 -20
  46. package/dist/core/objects/table/table.model.d.ts +2 -2
  47. package/dist/core/objects/table/table.model.js +8 -10
  48. package/dist/core/objects/trigger/trigger.model.d.ts +2 -2
  49. package/dist/core/objects/trigger/trigger.model.js +8 -10
  50. package/dist/core/objects/type/composite-type/composite-type.model.d.ts +2 -2
  51. package/dist/core/objects/type/composite-type/composite-type.model.js +8 -10
  52. package/dist/core/objects/type/enum/enum.model.d.ts +2 -2
  53. package/dist/core/objects/type/enum/enum.model.js +22 -24
  54. package/dist/core/objects/type/range/range.model.d.ts +2 -2
  55. package/dist/core/objects/type/range/range.model.js +7 -9
  56. package/dist/core/objects/view/view.model.d.ts +2 -2
  57. package/dist/core/objects/view/view.model.js +8 -10
  58. package/dist/core/plan/apply.d.ts +2 -2
  59. package/dist/core/plan/apply.js +50 -16
  60. package/dist/core/plan/create.d.ts +2 -2
  61. package/dist/core/plan/create.js +84 -38
  62. package/dist/core/postgres-config.d.ts +18 -3
  63. package/dist/core/postgres-config.js +105 -41
  64. package/package.json +4 -2
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../../base.model.js";
3
4
  import { privilegePropsSchema, } from "../../base.privilege-diff.js";
@@ -63,10 +64,8 @@ export class Server extends BasePgModel {
63
64
  };
64
65
  }
65
66
  }
66
- export async function extractServers(sql) {
67
- return sql.begin(async (sql) => {
68
- await sql `set search_path = ''`;
69
- const serverRows = await sql `
67
+ export async function extractServers(pool) {
68
+ const { rows: serverRows } = await pool.query(sql `
70
69
  select
71
70
  quote_ident(srv.srvname) as name,
72
71
  srv.srvowner::regrole::text as owner,
@@ -94,25 +93,24 @@ export async function extractServers(sql) {
94
93
  where
95
94
  not fdw.fdwname like any(array['pg\\_%'])
96
95
  order by
97
- srv.srvname;
98
- `;
99
- // Validate and parse each row using the Zod schema
100
- const validatedRows = serverRows.map((row) => {
101
- const parsed = serverPropsSchema.parse(row);
102
- // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
103
- if (parsed.options && parsed.options.length > 0) {
104
- const parsedOptions = [];
105
- for (const opt of parsed.options) {
106
- const eqIndex = opt.indexOf("=");
107
- if (eqIndex > 0) {
108
- parsedOptions.push(opt.substring(0, eqIndex));
109
- parsedOptions.push(opt.substring(eqIndex + 1));
110
- }
96
+ srv.srvname
97
+ `);
98
+ // Validate and parse each row using the Zod schema
99
+ const validatedRows = serverRows.map((row) => {
100
+ const parsed = serverPropsSchema.parse(row);
101
+ // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
102
+ if (parsed.options && parsed.options.length > 0) {
103
+ const parsedOptions = [];
104
+ for (const opt of parsed.options) {
105
+ const eqIndex = opt.indexOf("=");
106
+ if (eqIndex > 0) {
107
+ parsedOptions.push(opt.substring(0, eqIndex));
108
+ parsedOptions.push(opt.substring(eqIndex + 1));
111
109
  }
112
- parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
113
110
  }
114
- return parsed;
115
- });
116
- return validatedRows.map((row) => new Server(row));
111
+ parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
112
+ }
113
+ return parsed;
117
114
  });
115
+ return validatedRows.map((row) => new Server(row));
118
116
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../../base.model.ts";
4
4
  /**
@@ -32,5 +32,5 @@ export declare class UserMapping extends BasePgModel {
32
32
  options: string[] | null;
33
33
  };
34
34
  }
35
- export declare function extractUserMappings(sql: Sql): Promise<UserMapping[]>;
35
+ export declare function extractUserMappings(pool: Pool): Promise<UserMapping[]>;
36
36
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../../base.model.js";
3
4
  /**
@@ -43,10 +44,8 @@ export class UserMapping extends BasePgModel {
43
44
  };
44
45
  }
45
46
  }
46
- export async function extractUserMappings(sql) {
47
- return sql.begin(async (sql) => {
48
- await sql `set search_path = ''`;
49
- const mappingRows = await sql `
47
+ export async function extractUserMappings(pool) {
48
+ const { rows: mappingRows } = await pool.query(sql `
50
49
  select
51
50
  case
52
51
  when um.umuser = 0 then 'PUBLIC'
@@ -61,25 +60,24 @@ export async function extractUserMappings(sql) {
61
60
  where
62
61
  not fdw.fdwname like any(array['pg\\_%'])
63
62
  order by
64
- srv.srvname, um.umuser;
65
- `;
66
- // Validate and parse each row using the Zod schema
67
- const validatedRows = mappingRows.map((row) => {
68
- const parsed = userMappingPropsSchema.parse(row);
69
- // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
70
- if (parsed.options && parsed.options.length > 0) {
71
- const parsedOptions = [];
72
- for (const opt of parsed.options) {
73
- const eqIndex = opt.indexOf("=");
74
- if (eqIndex > 0) {
75
- parsedOptions.push(opt.substring(0, eqIndex));
76
- parsedOptions.push(opt.substring(eqIndex + 1));
77
- }
63
+ srv.srvname, um.umuser
64
+ `);
65
+ // Validate and parse each row using the Zod schema
66
+ const validatedRows = mappingRows.map((row) => {
67
+ const parsed = userMappingPropsSchema.parse(row);
68
+ // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
69
+ if (parsed.options && parsed.options.length > 0) {
70
+ const parsedOptions = [];
71
+ for (const opt of parsed.options) {
72
+ const eqIndex = opt.indexOf("=");
73
+ if (eqIndex > 0) {
74
+ parsedOptions.push(opt.substring(0, eqIndex));
75
+ parsedOptions.push(opt.substring(eqIndex + 1));
78
76
  }
79
- parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
80
77
  }
81
- return parsed;
82
- });
83
- return validatedRows.map((row) => new UserMapping(row));
78
+ parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
79
+ }
80
+ return parsed;
84
81
  });
82
+ return validatedRows.map((row) => new UserMapping(row));
85
83
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  declare const indexPropsSchema: z.ZodObject<{
@@ -17,7 +17,7 @@ declare const indexPropsSchema: z.ZodObject<{
17
17
  is_clustered: z.ZodBoolean;
18
18
  is_replica_identity: z.ZodBoolean;
19
19
  key_columns: z.ZodArray<z.ZodNumber>;
20
- column_collations: z.ZodArray<z.ZodString>;
20
+ column_collations: z.ZodArray<z.ZodNullable<z.ZodString>>;
21
21
  operator_classes: z.ZodArray<z.ZodString>;
22
22
  column_options: z.ZodArray<z.ZodNumber>;
23
23
  index_expressions: z.ZodNullable<z.ZodString>;
@@ -98,7 +98,7 @@ export declare class Index extends BasePgModel {
98
98
  immediate: boolean;
99
99
  is_clustered: boolean;
100
100
  is_replica_identity: boolean;
101
- column_collations: string[];
101
+ column_collations: (string | null)[];
102
102
  operator_classes: string[];
103
103
  column_options: number[];
104
104
  index_expressions: string | null;
@@ -146,5 +146,5 @@ export declare class Index extends BasePgModel {
146
146
  };
147
147
  };
148
148
  }
149
- export declare function extractIndexes(sql: Sql): Promise<Index[]>;
149
+ export declare function extractIndexes(pool: Pool): Promise<Index[]>;
150
150
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  const TableRelkindSchema = z.enum([
@@ -21,7 +22,7 @@ const indexPropsSchema = z.object({
21
22
  is_clustered: z.boolean(),
22
23
  is_replica_identity: z.boolean(),
23
24
  key_columns: z.array(z.number()),
24
- column_collations: z.array(z.string()),
25
+ column_collations: z.array(z.string().nullable()),
25
26
  operator_classes: z.array(z.string()),
26
27
  column_options: z.array(z.number()),
27
28
  index_expressions: z.string().nullable(),
@@ -174,10 +175,8 @@ export class Index extends BasePgModel {
174
175
  };
175
176
  }
176
177
  }
177
- export async function extractIndexes(sql) {
178
- return sql.begin(async (sql) => {
179
- await sql `set search_path = ''`;
180
- const indexRows = await sql `
178
+ export async function extractIndexes(pool) {
179
+ const { rows: indexRows } = await pool.query(sql `
181
180
  with extension_oids as (
182
181
  select objid
183
182
  from pg_depend d
@@ -331,10 +330,9 @@ export async function extractIndexes(sql) {
331
330
  and e.objid is null
332
331
  and e_table.objid is null
333
332
 
334
- order by 1, 2;
335
- `;
336
- // Validate and parse each row using the Zod schema
337
- const validatedRows = indexRows.map((row) => indexPropsSchema.parse(row));
338
- return validatedRows.map((row) => new Index(row));
339
- });
333
+ order by 1, 2
334
+ `);
335
+ // Validate and parse each row using the Zod schema
336
+ const validatedRows = indexRows.map((row) => indexPropsSchema.parse(row));
337
+ return validatedRows.map((row) => new Index(row));
340
338
  }
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  import { privilegePropsSchema, } from "../base.privilege-diff.js";
@@ -71,10 +72,8 @@ export class Language extends BasePgModel {
71
72
  };
72
73
  }
73
74
  }
74
- async function _extractLanguages(sql) {
75
- const languageRows = await sql.begin(async (sql) => {
76
- await sql `set search_path = ''`;
77
- return sql `
75
+ async function _extractLanguages(pool) {
76
+ const { rows: languageRows } = await pool.query(sql `
78
77
  with extension_oids as (
79
78
  select
80
79
  objid
@@ -112,9 +111,8 @@ async function _extractLanguages(sql) {
112
111
  -- <EXCLUDE_INTERNAL and default>
113
112
  where lan.lanname not in ('internal', 'c')
114
113
  order by
115
- lan.lanname;
116
- `;
117
- });
114
+ lan.lanname
115
+ `);
118
116
  // Process rows to handle "-" as null values
119
117
  const processedRows = languageRows.map((row) => ({
120
118
  ...row,
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel, type TableLikeObject } from "../base.model.ts";
4
4
  import { type PrivilegeProps } from "../base.privilege-diff.ts";
@@ -148,5 +148,5 @@ export declare class MaterializedView extends BasePgModel implements TableLikeOb
148
148
  };
149
149
  };
150
150
  }
151
- export declare function extractMaterializedViews(sql: Sql): Promise<MaterializedView[]>;
151
+ export declare function extractMaterializedViews(pool: Pool): Promise<MaterializedView[]>;
152
152
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel, columnPropsSchema, } from "../base.model.js";
3
4
  import { privilegePropsSchema, } from "../base.privilege-diff.js";
@@ -113,10 +114,8 @@ export class MaterializedView extends BasePgModel {
113
114
  };
114
115
  }
115
116
  }
116
- export async function extractMaterializedViews(sql) {
117
- return sql.begin(async (sql) => {
118
- await sql `set search_path = ''`;
119
- const mvRows = await sql `
117
+ export async function extractMaterializedViews(pool) {
118
+ const { rows: mvRows } = await pool.query(sql `
120
119
  with extension_oids as (
121
120
  select
122
121
  objid
@@ -219,10 +218,9 @@ where not c.relnamespace::regnamespace::text like any(array['pg\\_%', 'informati
219
218
  group by
220
219
  c.oid, c.relnamespace, c.relname, pg_get_viewdef(c.oid), c.relrowsecurity, c.relforcerowsecurity, c.relhasindex, c.relhasrules, c.relhastriggers, c.relhassubclass, c.relispopulated, c.relreplident, c.relispartition, c.reloptions, pg_get_expr(c.relpartbound, c.oid), c.relowner
221
220
  order by
222
- c.relnamespace::regnamespace, c.relname;
223
- `;
224
- // Validate and parse each row using the Zod schema
225
- const validatedRows = mvRows.map((row) => materializedViewPropsSchema.parse(row));
226
- return validatedRows.map((row) => new MaterializedView(row));
227
- });
221
+ c.relnamespace::regnamespace, c.relname
222
+ `);
223
+ // Validate and parse each row using the Zod schema
224
+ const validatedRows = mvRows.map((row) => materializedViewPropsSchema.parse(row));
225
+ return validatedRows.map((row) => new MaterializedView(row));
228
226
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  import { type PrivilegeProps } from "../base.privilege-diff.ts";
@@ -128,5 +128,5 @@ export declare class Procedure extends BasePgModel {
128
128
  }[];
129
129
  };
130
130
  }
131
- export declare function extractProcedures(sql: Sql): Promise<Procedure[]>;
131
+ export declare function extractProcedures(pool: Pool): Promise<Procedure[]>;
132
132
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  import { privilegePropsSchema, } from "../base.privilege-diff.js";
@@ -162,10 +163,8 @@ export class Procedure extends BasePgModel {
162
163
  };
163
164
  }
164
165
  }
165
- export async function extractProcedures(sql) {
166
- return sql.begin(async (sql) => {
167
- await sql `set search_path = ''`;
168
- const procedureRows = await sql `
166
+ export async function extractProcedures(pool) {
167
+ const { rows: procedureRows } = await pool.query(sql `
169
168
  with extension_oids as (
170
169
  select
171
170
  objid
@@ -235,10 +234,9 @@ from
235
234
  and e.objid is null
236
235
  and l.lanname not in ('c', 'internal')
237
236
  order by
238
- 1, 2;
239
- `;
240
- // Validate and parse each row using the Zod schema
241
- const validatedRows = procedureRows.map((row) => procedurePropsSchema.parse(row));
242
- return validatedRows.map((row) => new Procedure(row));
243
- });
237
+ 1, 2
238
+ `);
239
+ // Validate and parse each row using the Zod schema
240
+ const validatedRows = procedureRows.map((row) => procedurePropsSchema.parse(row));
241
+ return validatedRows.map((row) => new Procedure(row));
244
242
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  declare const publicationTablePropsSchema: z.ZodObject<{
@@ -92,5 +92,5 @@ export declare class Publication extends BasePgModel {
92
92
  /**
93
93
  * Extract all logical replication publications from the database.
94
94
  */
95
- export declare function extractPublications(sql: Sql): Promise<Publication[]>;
95
+ export declare function extractPublications(pool: Pool): Promise<Publication[]>;
96
96
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  const publicationTablePropsSchema = z.object({
@@ -104,10 +105,8 @@ export class Publication extends BasePgModel {
104
105
  /**
105
106
  * Extract all logical replication publications from the database.
106
107
  */
107
- export async function extractPublications(sql) {
108
- return sql.begin(async (tx) => {
109
- await tx `set search_path = ''`;
110
- const rows = await tx `
108
+ export async function extractPublications(pool) {
109
+ const { rows } = await pool.query(sql `
111
110
  with extension_oids as (
112
111
  select objid
113
112
  from pg_depend d
@@ -178,9 +177,8 @@ export async function extractPublications(sql) {
178
177
  from pg_publication p
179
178
  left join extension_oids e on e.objid = p.oid
180
179
  where e.objid is null
181
- order by 1;
182
- `;
183
- const validated = rows.map((row) => publicationPropsSchema.parse(row));
184
- return validated.map((row) => new Publication(row));
185
- });
180
+ order by 1
181
+ `);
182
+ const validated = rows.map((row) => publicationPropsSchema.parse(row));
183
+ return validated.map((row) => new Publication(row));
186
184
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  declare const rlsPolicyPropsSchema: z.ZodObject<{
@@ -48,5 +48,5 @@ export declare class RlsPolicy extends BasePgModel {
48
48
  comment: string | null;
49
49
  };
50
50
  }
51
- export declare function extractRlsPolicies(sql: Sql): Promise<RlsPolicy[]>;
51
+ export declare function extractRlsPolicies(pool: Pool): Promise<RlsPolicy[]>;
52
52
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  const RlsPolicyCommandSchema = z.enum([
@@ -67,10 +68,8 @@ export class RlsPolicy extends BasePgModel {
67
68
  };
68
69
  }
69
70
  }
70
- export async function extractRlsPolicies(sql) {
71
- return sql.begin(async (sql) => {
72
- await sql `set search_path = ''`;
73
- const policyRows = await sql `
71
+ export async function extractRlsPolicies(pool) {
72
+ const { rows: policyRows } = await pool.query(sql `
74
73
  with extension_policy_oids as (
75
74
  select
76
75
  objid
@@ -118,10 +117,9 @@ from
118
117
  and e_policy.objid is null
119
118
  and e_table.objid is null
120
119
  order by
121
- 1, 2;
122
- `;
123
- // Validate and parse each row using the Zod schema
124
- const validatedRows = policyRows.map((row) => rlsPolicyPropsSchema.parse(row));
125
- return validatedRows.map((row) => new RlsPolicy(row));
126
- });
120
+ 1, 2
121
+ `);
122
+ // Validate and parse each row using the Zod schema
123
+ const validatedRows = policyRows.map((row) => rlsPolicyPropsSchema.parse(row));
124
+ return validatedRows.map((row) => new RlsPolicy(row));
127
125
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  declare const rolePropsSchema: z.ZodObject<{
@@ -85,5 +85,5 @@ export declare class Role extends BasePgModel {
85
85
  }[];
86
86
  };
87
87
  }
88
- export declare function extractRoles(sql: Sql): Promise<Role[]>;
88
+ export declare function extractRoles(pool: Pool): Promise<Role[]>;
89
89
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  const membershipInfoSchema = z.object({
@@ -104,11 +105,9 @@ export class Role extends BasePgModel {
104
105
  };
105
106
  }
106
107
  }
107
- export async function extractRoles(sql) {
108
- return sql.begin(async (sql) => {
109
- await sql `set search_path = ''`;
110
- // Check PostgreSQL version capabilities for membership options
111
- const [capabilities] = await sql `
108
+ export async function extractRoles(pool) {
109
+ // Check PostgreSQL version capabilities for membership options
110
+ const { rows: capabilitiesRows } = await pool.query(sql `
112
111
  select
113
112
  exists (
114
113
  select 1
@@ -122,23 +121,25 @@ export async function extractRoles(sql) {
122
121
  where attrelid = 'pg_auth_members'::regclass
123
122
  and attname = 'set_option'
124
123
  ) as has_set
125
- `;
126
- const roleRows = capabilities?.has_inherit && capabilities?.has_set
127
- ? await sql `
124
+ `);
125
+ const capabilities = capabilitiesRows[0];
126
+ let roleRows;
127
+ if (capabilities?.has_inherit && capabilities?.has_set) {
128
+ const result = await pool.query(sql `
128
129
  WITH role_memberships AS (
129
- SELECT
130
+ SELECT
130
131
  r.rolname AS role_name,
131
132
  json_agg(
132
133
  json_build_object(
133
134
  'member', m.rolname,
134
135
  'grantor', g.rolname,
135
136
  'admin_option', am.admin_option,
136
- 'inherit_option', am.inherit_option, -- PG16+
137
- 'set_option', am.set_option -- PG16+
137
+ 'inherit_option', am.inherit_option,
138
+ 'set_option', am.set_option
138
139
  )
139
140
  ) FILTER (WHERE m.rolname IS NOT NULL) AS members
140
141
  FROM pg_catalog.pg_roles r
141
- LEFT JOIN pg_auth_members am ON am.roleid = r.oid -- roles that are members of this role
142
+ LEFT JOIN pg_auth_members am ON am.roleid = r.oid
142
143
  LEFT JOIN pg_roles m ON m.oid = am.member
143
144
  LEFT JOIN pg_roles g ON g.oid = am.grantor
144
145
  GROUP BY r.rolname
@@ -201,9 +202,7 @@ export async function extractRoles(sql) {
201
202
  FROM pg_catalog.pg_roles r
202
203
  LEFT JOIN role_memberships rm ON rm.role_name = r.rolname
203
204
  WHERE
204
- -- 1) drop built-in/internal roles (anything starting with pg_)
205
205
  r.rolname !~ '^pg_'
206
- -- 2) drop roles directly tracked as extension members in pg_shdepend (if any)
207
206
  AND NOT EXISTS (
208
207
  SELECT 1
209
208
  FROM pg_catalog.pg_shdepend d
@@ -212,24 +211,26 @@ export async function extractRoles(sql) {
212
211
  AND d.refclassid = 'pg_extension'::regclass
213
212
  AND d.deptype IN ('e','x')
214
213
  )
215
- ORDER BY 1;
216
- `
217
- : await sql `
214
+ ORDER BY 1
215
+ `);
216
+ roleRows = result.rows;
217
+ }
218
+ else {
219
+ const result = await pool.query(sql `
218
220
  WITH role_memberships AS (
219
- SELECT
221
+ SELECT
220
222
  r.rolname AS role_name,
221
223
  json_agg(
222
224
  json_build_object(
223
225
  'member', m.rolname,
224
226
  'grantor', g.rolname,
225
227
  'admin_option', am.admin_option,
226
- -- PG15: these columns don't exist; emit them as nulls
227
228
  'inherit_option', NULL,
228
229
  'set_option', NULL
229
230
  )
230
231
  ) FILTER (WHERE m.rolname IS NOT NULL) AS members
231
232
  FROM pg_catalog.pg_roles r
232
- LEFT JOIN pg_auth_members am ON am.roleid = r.oid -- roles that are members of this role
233
+ LEFT JOIN pg_auth_members am ON am.roleid = r.oid
233
234
  LEFT JOIN pg_roles m ON m.oid = am.member
234
235
  LEFT JOIN pg_roles g ON g.oid = am.grantor
235
236
  GROUP BY r.rolname
@@ -292,9 +293,7 @@ export async function extractRoles(sql) {
292
293
  FROM pg_catalog.pg_roles r
293
294
  LEFT JOIN role_memberships rm ON rm.role_name = r.rolname
294
295
  WHERE
295
- -- drop built-in/internal roles
296
296
  r.rolname !~ '^pg_'
297
- -- drop roles directly tracked as extension members in pg_shdepend (if any)
298
297
  AND NOT EXISTS (
299
298
  SELECT 1
300
299
  FROM pg_catalog.pg_shdepend d
@@ -303,10 +302,11 @@ export async function extractRoles(sql) {
303
302
  AND d.refclassid = 'pg_extension'::regclass
304
303
  AND d.deptype IN ('e','x')
305
304
  )
306
- ORDER BY 1;
307
- `;
308
- // Validate and parse each row using the Zod schema
309
- const validatedRows = roleRows.map((row) => rolePropsSchema.parse(row));
310
- return validatedRows.map((row) => new Role(row));
311
- });
305
+ ORDER BY 1
306
+ `);
307
+ roleRows = result.rows;
308
+ }
309
+ // Validate and parse each row using the Zod schema
310
+ const validatedRows = roleRows.map((row) => rolePropsSchema.parse(row));
311
+ return validatedRows.map((row) => new Role(row));
312
312
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  declare const RuleEnabledStateSchema: z.ZodEnum<{
@@ -68,5 +68,5 @@ export declare class Rule extends BasePgModel {
68
68
  };
69
69
  get relationStableId(): string;
70
70
  }
71
- export declare function extractRules(sql: Sql): Promise<Rule[]>;
71
+ export declare function extractRules(pool: Pool): Promise<Rule[]>;
72
72
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { BasePgModel } from "../base.model.js";
3
4
  import { stableId } from "../utils.js";
@@ -81,10 +82,8 @@ export class Rule extends BasePgModel {
81
82
  }
82
83
  }
83
84
  }
84
- export async function extractRules(sql) {
85
- return sql.begin(async (sql) => {
86
- await sql `set search_path = ''`;
87
- const ruleRows = await sql `
85
+ export async function extractRules(pool) {
86
+ const { rows: ruleRows } = await pool.query(sql `
88
87
  WITH extension_rule_oids AS (
89
88
  SELECT
90
89
  objid
@@ -148,9 +147,8 @@ export async function extractRules(sql) {
148
147
  AND e_rel.objid IS NULL
149
148
  AND r.rulename <> '_RETURN'
150
149
  ORDER BY
151
- 1, 3, 2;
152
- `;
153
- const validatedRows = ruleRows.map((row) => rulePropsSchema.parse(row));
154
- return validatedRows.map((row) => new Rule(row));
155
- });
150
+ 1, 3, 2
151
+ `);
152
+ const validatedRows = ruleRows.map((row) => rulePropsSchema.parse(row));
153
+ return validatedRows.map((row) => new Rule(row));
156
154
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import z from "zod";
3
3
  import { BasePgModel } from "../base.model.ts";
4
4
  import { type PrivilegeProps } from "../base.privilege-diff.ts";
@@ -43,5 +43,5 @@ export declare class Schema extends BasePgModel {
43
43
  }[];
44
44
  };
45
45
  }
46
- export declare function extractSchemas(sql: Sql): Promise<Schema[]>;
46
+ export declare function extractSchemas(pool: Pool): Promise<Schema[]>;
47
47
  export {};