@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,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import { type PgDepend } from "./depend.ts";
3
3
  import { type Aggregate } from "./objects/aggregate/aggregate.model.ts";
4
4
  import type { TableLikeObject } from "./objects/base.model.ts";
@@ -89,5 +89,5 @@ export declare class Catalog {
89
89
  readonly currentUser: CatalogProps["currentUser"];
90
90
  constructor(props: CatalogProps);
91
91
  }
92
- export declare function extractCatalog(sql: Sql): Promise<Catalog>;
92
+ export declare function extractCatalog(pool: Pool): Promise<Catalog>;
93
93
  export {};
@@ -88,36 +88,36 @@ export class Catalog {
88
88
  this.currentUser = props.currentUser;
89
89
  }
90
90
  }
91
- export async function extractCatalog(sql) {
91
+ export async function extractCatalog(pool) {
92
92
  const [aggregates, collations, compositeTypes, domains, enums, extensions, indexes, materializedViews, subscriptions, publications, procedures, rlsPolicies, roles, schemas, sequences, tables, triggers, eventTriggers, rules, ranges, views, foreignDataWrappers, servers, userMappings, foreignTables, depends, version, currentUser,] = await Promise.all([
93
- extractAggregates(sql).then(listToRecord),
94
- extractCollations(sql).then(listToRecord),
95
- extractCompositeTypes(sql).then(listToRecord),
96
- extractDomains(sql).then(listToRecord),
97
- extractEnums(sql).then(listToRecord),
98
- extractExtensions(sql).then(listToRecord),
99
- extractIndexes(sql).then(listToRecord),
100
- extractMaterializedViews(sql).then(listToRecord),
101
- extractSubscriptions(sql).then(listToRecord),
102
- extractPublications(sql).then(listToRecord),
103
- extractProcedures(sql).then(listToRecord),
104
- extractRlsPolicies(sql).then(listToRecord),
105
- extractRoles(sql).then(listToRecord),
106
- extractSchemas(sql).then(listToRecord),
107
- extractSequences(sql).then(listToRecord),
108
- extractTables(sql).then(listToRecord),
109
- extractTriggers(sql).then(listToRecord),
110
- extractEventTriggers(sql).then(listToRecord),
111
- extractRules(sql).then(listToRecord),
112
- extractRanges(sql).then(listToRecord),
113
- extractViews(sql).then(listToRecord),
114
- extractForeignDataWrappers(sql).then(listToRecord),
115
- extractServers(sql).then(listToRecord),
116
- extractUserMappings(sql).then(listToRecord),
117
- extractForeignTables(sql).then(listToRecord),
118
- extractDepends(sql),
119
- extractVersion(sql),
120
- extractCurrentUser(sql),
93
+ extractAggregates(pool).then(listToRecord),
94
+ extractCollations(pool).then(listToRecord),
95
+ extractCompositeTypes(pool).then(listToRecord),
96
+ extractDomains(pool).then(listToRecord),
97
+ extractEnums(pool).then(listToRecord),
98
+ extractExtensions(pool).then(listToRecord),
99
+ extractIndexes(pool).then(listToRecord),
100
+ extractMaterializedViews(pool).then(listToRecord),
101
+ extractSubscriptions(pool).then(listToRecord),
102
+ extractPublications(pool).then(listToRecord),
103
+ extractProcedures(pool).then(listToRecord),
104
+ extractRlsPolicies(pool).then(listToRecord),
105
+ extractRoles(pool).then(listToRecord),
106
+ extractSchemas(pool).then(listToRecord),
107
+ extractSequences(pool).then(listToRecord),
108
+ extractTables(pool).then(listToRecord),
109
+ extractTriggers(pool).then(listToRecord),
110
+ extractEventTriggers(pool).then(listToRecord),
111
+ extractRules(pool).then(listToRecord),
112
+ extractRanges(pool).then(listToRecord),
113
+ extractViews(pool).then(listToRecord),
114
+ extractForeignDataWrappers(pool).then(listToRecord),
115
+ extractServers(pool).then(listToRecord),
116
+ extractUserMappings(pool).then(listToRecord),
117
+ extractForeignTables(pool).then(listToRecord),
118
+ extractDepends(pool),
119
+ extractVersion(pool),
120
+ extractCurrentUser(pool),
121
121
  ]);
122
122
  const indexableObjects = {
123
123
  ...tables,
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  import type { Catalog } from "./catalog.model.ts";
3
3
  /**
4
4
  * Context for diff operations, containing both source and target catalogs.
@@ -7,5 +7,5 @@ export interface DiffContext {
7
7
  mainCatalog: Catalog;
8
8
  branchCatalog: Catalog;
9
9
  }
10
- export declare function extractVersion(sql: Sql): Promise<number>;
11
- export declare function extractCurrentUser(sql: Sql): Promise<string>;
10
+ export declare function extractVersion(pool: Pool): Promise<number>;
11
+ export declare function extractCurrentUser(pool: Pool): Promise<string>;
@@ -1,12 +1,9 @@
1
- export async function extractVersion(sql) {
2
- const [{ version }] = await sql `
3
- select current_setting('server_version_num')::int as version;
4
- `;
5
- return version;
1
+ import { sql } from "@ts-safeql/sql-tag";
2
+ export async function extractVersion(pool) {
3
+ const { rows } = await pool.query(sql `select current_setting('server_version_num')::int as version`);
4
+ return rows[0].version;
6
5
  }
7
- export async function extractCurrentUser(sql) {
8
- const [{ current_user }] = await sql `
9
- select quote_ident(current_user) as current_user;
10
- `;
11
- return current_user;
6
+ export async function extractCurrentUser(pool) {
7
+ const { rows } = await pool.query(sql `select quote_ident(current_user) as current_user`);
8
+ return rows[0].current_user;
12
9
  }
@@ -1,4 +1,4 @@
1
- import type { Sql } from "postgres";
1
+ import type { Pool } from "pg";
2
2
  /**
3
3
  * Dependency type as defined in PostgreSQL's pg_depend.deptype.
4
4
  * n: normal
@@ -27,5 +27,5 @@ export interface PgDepend {
27
27
  * @param params - Object containing arrays of OIDs for filtering (user_oids, user_namespace_oids, etc.)
28
28
  * @returns Array of dependency objects with class names.
29
29
  */
30
- export declare function extractDepends(sql: Sql): Promise<PgDepend[]>;
30
+ export declare function extractDepends(pool: Pool): Promise<PgDepend[]>;
31
31
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  /**
2
3
  * Extract dependencies for privileges and memberships so that GRANT/REVOKE
3
4
  * operations are properly ordered with respect to their target objects/roles.
@@ -13,8 +14,8 @@
13
14
  * - membership:<role>-><member> -> role:<role>
14
15
  * - membership:<role>-><member> -> role:<member>
15
16
  */
16
- async function extractPrivilegeAndMembershipDepends(sql) {
17
- const rows = await sql `
17
+ async function extractPrivilegeAndMembershipDepends(pool) {
18
+ const { rows } = await pool.query(sql `
18
19
  with
19
20
  -- OBJECT PRIVILEGES (relations)
20
21
  extension_rel_oids as (
@@ -468,7 +469,7 @@ where dependent_stable_id <> referenced_stable_id
468
469
  and NOT (
469
470
  COALESCE(dep_schema, '') LIKE ANY (ARRAY['pg\\_%','information\\_schema'])
470
471
  )
471
- `;
472
+ `);
472
473
  return rows;
473
474
  }
474
475
  /**
@@ -477,8 +478,8 @@ where dependent_stable_id <> referenced_stable_id
477
478
  * @param params - Object containing arrays of OIDs for filtering (user_oids, user_namespace_oids, etc.)
478
479
  * @returns Array of dependency objects with class names.
479
480
  */
480
- export async function extractDepends(sql) {
481
- const dependsRows = await sql `
481
+ export async function extractDepends(pool) {
482
+ const { rows: dependsRows } = await pool.query(sql `
482
483
  WITH ids AS (
483
484
  -- only the objects that actually show up in dependencies (both sides)
484
485
  SELECT DISTINCT classid, objid, objsubid FROM pg_depend WHERE deptype IN ('n','a')
@@ -1823,9 +1824,9 @@ export async function extractDepends(sql) {
1823
1824
  COALESCE(dep_schema, '') LIKE ANY (ARRAY['pg\\_%','information\\_schema'])
1824
1825
  )
1825
1826
  ORDER BY dependent_stable_id, referenced_stable_id;
1826
- `;
1827
+ `);
1827
1828
  // Extract privilege and membership dependencies
1828
- const privilegeDepends = await extractPrivilegeAndMembershipDepends(sql);
1829
+ const privilegeDepends = await extractPrivilegeAndMembershipDepends(pool);
1829
1830
  // Combine all dependency sources and remove duplicates
1830
1831
  const allDepends = new Set([...dependsRows, ...privilegeDepends]);
1831
1832
  return Array.from(allDepends).sort((a, b) => a.dependent_stable_id.localeCompare(b.dependent_stable_id) ||
@@ -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";
@@ -170,5 +170,5 @@ export declare class Aggregate extends BasePgModel {
170
170
  }[];
171
171
  };
172
172
  }
173
- export declare function extractAggregates(sql: Sql): Promise<Aggregate[]>;
173
+ export declare function extractAggregates(pool: Pool): Promise<Aggregate[]>;
174
174
  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";
@@ -208,10 +209,8 @@ export class Aggregate extends BasePgModel {
208
209
  };
209
210
  }
210
211
  }
211
- export async function extractAggregates(sql) {
212
- return sql.begin(async (sql) => {
213
- await sql `set search_path = ''`;
214
- const aggregateRows = await sql `
212
+ export async function extractAggregates(pool) {
213
+ const { rows: aggregateRows } = await pool.query(sql `
215
214
  with extension_oids as (
216
215
  select
217
216
  objid
@@ -289,9 +288,8 @@ where
289
288
  and not p.pronamespace::regnamespace::text like any(array['pg\\_%', 'information\\_schema'])
290
289
  and e.objid is null
291
290
  order by
292
- 1, 2, 3;
293
- `;
294
- const validatedRows = aggregateRows.map((row) => aggregatePropsSchema.parse(row));
295
- return validatedRows.map((row) => new Aggregate(row));
296
- });
291
+ 1, 2, 3
292
+ `);
293
+ const validatedRows = aggregateRows.map((row) => aggregatePropsSchema.parse(row));
294
+ return validatedRows.map((row) => new Aggregate(row));
297
295
  }
@@ -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
  /**
@@ -63,5 +63,5 @@ export declare class Collation extends BasePgModel {
63
63
  comment: string | null;
64
64
  };
65
65
  }
66
- export declare function extractCollations(sql: Sql): Promise<Collation[]>;
66
+ export declare function extractCollations(pool: Pool): Promise<Collation[]>;
67
67
  export {};
@@ -1,3 +1,4 @@
1
+ import { sql } from "@ts-safeql/sql-tag";
1
2
  import z from "zod";
2
3
  import { extractVersion } from "../../context.js";
3
4
  import { BasePgModel } from "../base.model.js";
@@ -88,15 +89,13 @@ export class Collation extends BasePgModel {
88
89
  };
89
90
  }
90
91
  }
91
- export async function extractCollations(sql) {
92
- return sql.begin(async (sql) => {
93
- await sql `set search_path = ''`;
94
- const version = await extractVersion(sql);
95
- const isPostgres17OrGreater = version >= 170000;
96
- const isPostgres16OrGreater = version >= 160000;
97
- let collations;
98
- if (isPostgres17OrGreater) {
99
- collations = await sql `
92
+ export async function extractCollations(pool) {
93
+ const version = await extractVersion(pool);
94
+ const isPostgres17OrGreater = version >= 170000;
95
+ const isPostgres16OrGreater = version >= 160000;
96
+ let collationRows;
97
+ if (isPostgres17OrGreater) {
98
+ const result = await pool.query(sql `
100
99
  with extension_oids as (
101
100
  select
102
101
  objid
@@ -125,12 +124,13 @@ export async function extractCollations(sql) {
125
124
  where not c.collnamespace::regnamespace::text like any(array['pg\\_%', 'information\\_schema'])
126
125
  and e.objid is null
127
126
  order by
128
- 1, 2;
129
- `;
130
- }
131
- else if (isPostgres16OrGreater) {
132
- // On postgres 16 there colllocale column was named colliculocale
133
- collations = await sql `
127
+ 1, 2
128
+ `);
129
+ collationRows = result.rows;
130
+ }
131
+ else if (isPostgres16OrGreater) {
132
+ // On postgres 16 there colllocale column was named colliculocale
133
+ const result = await pool.query(sql `
134
134
  with extension_oids as (
135
135
  select
136
136
  objid
@@ -161,12 +161,13 @@ export async function extractCollations(sql) {
161
161
  and e.objid is null
162
162
  -- </EXCLUDE_INTERNAL>
163
163
  order by
164
- 1, 2;
165
- `;
166
- }
167
- else {
168
- // On postgres 15 icu_rules does not exist
169
- collations = await sql `
164
+ 1, 2
165
+ `);
166
+ collationRows = result.rows;
167
+ }
168
+ else {
169
+ // On postgres 15 icu_rules does not exist
170
+ const result = await pool.query(sql `
170
171
  with extension_oids as (
171
172
  select
172
173
  objid
@@ -197,11 +198,11 @@ export async function extractCollations(sql) {
197
198
  and e.objid is null
198
199
  -- </EXCLUDE_INTERNAL>
199
200
  order by
200
- 1, 2;
201
- `;
202
- }
203
- // Validate and parse each row using the Zod schema
204
- const validatedRows = collations.map((row) => collationPropsSchema.parse(row));
205
- return validatedRows.map((row) => new Collation(row));
206
- });
201
+ 1, 2
202
+ `);
203
+ collationRows = result.rows;
204
+ }
205
+ // Validate and parse each row using the Zod schema
206
+ const validatedRows = collationRows.map((row) => collationPropsSchema.parse(row));
207
+ return validatedRows.map((row) => new Collation(row));
207
208
  }
@@ -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";
@@ -99,5 +99,5 @@ export declare class Domain extends BasePgModel {
99
99
  * @param sql - The SQL client.
100
100
  * @returns A list of domains.
101
101
  */
102
- export declare function extractDomains(sql: Sql): Promise<Domain[]>;
102
+ export declare function extractDomains(pool: Pool): Promise<Domain[]>;
103
103
  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";
@@ -98,10 +99,8 @@ export class Domain extends BasePgModel {
98
99
  * @param sql - The SQL client.
99
100
  * @returns A list of domains.
100
101
  */
101
- export async function extractDomains(sql) {
102
- return sql.begin(async (sql) => {
103
- await sql `set search_path = ''`;
104
- const domainRows = await sql `
102
+ export async function extractDomains(pool) {
103
+ const { rows: domainRows } = await pool.query(sql `
105
104
  with extension_oids as (
106
105
  select
107
106
  objid
@@ -163,10 +162,9 @@ export async function extractDomains(sql) {
163
162
  and e.objid is null
164
163
  and t.typtype = 'd'
165
164
  order by
166
- 1, 2;
167
- `;
168
- // Validate and parse each row using the Zod schema
169
- const validatedRows = domainRows.map((row) => domainPropsSchema.parse(row));
170
- return validatedRows.map((row) => new Domain(row));
171
- });
165
+ 1, 2
166
+ `);
167
+ // Validate and parse each row using the Zod schema
168
+ const validatedRows = domainRows.map((row) => domainPropsSchema.parse(row));
169
+ return validatedRows.map((row) => new Domain(row));
172
170
  }
@@ -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 eventTriggerPropsSchema: z.ZodObject<{
@@ -41,5 +41,5 @@ export declare class EventTrigger extends BasePgModel {
41
41
  comment: string | null;
42
42
  };
43
43
  }
44
- export declare function extractEventTriggers(sql: Sql): Promise<EventTrigger[]>;
44
+ export declare function extractEventTriggers(pool: Pool): Promise<EventTrigger[]>;
45
45
  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 EventTriggerEnabledSchema = z.enum([
@@ -58,10 +59,8 @@ export class EventTrigger extends BasePgModel {
58
59
  };
59
60
  }
60
61
  }
61
- export async function extractEventTriggers(sql) {
62
- return sql.begin(async (sql) => {
63
- await sql `set search_path = ''`;
64
- const rows = await sql `
62
+ export async function extractEventTriggers(pool) {
63
+ const { rows } = await pool.query(sql `
65
64
  with extension_oids as (
66
65
  select objid
67
66
  from pg_depend d
@@ -81,9 +80,8 @@ from pg_catalog.pg_event_trigger et
81
80
  join pg_catalog.pg_proc p on p.oid = et.evtfoid
82
81
  left join extension_oids e on e.objid = et.oid
83
82
  where e.objid is null
84
- order by 1;
85
- `;
86
- const validatedRows = rows.map((row) => eventTriggerPropsSchema.parse(row));
87
- return validatedRows.map((row) => new EventTrigger(row));
88
- });
83
+ order by 1
84
+ `);
85
+ const validatedRows = rows.map((row) => eventTriggerPropsSchema.parse(row));
86
+ return validatedRows.map((row) => new EventTrigger(row));
89
87
  }
@@ -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
  /**
@@ -48,5 +48,5 @@ export declare class Extension extends BasePgModel {
48
48
  comment: string | null;
49
49
  };
50
50
  }
51
- export declare function extractExtensions(sql: Sql): Promise<Extension[]>;
51
+ export declare function extractExtensions(pool: Pool): Promise<Extension[]>;
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
  /**
@@ -65,10 +66,8 @@ export class Extension extends BasePgModel {
65
66
  }
66
67
  }
67
68
  // TODO: fetch extension dependencies so we can determine when to use CASCADE on creation
68
- export async function extractExtensions(sql) {
69
- return sql.begin(async (sql) => {
70
- await sql `set search_path = ''`;
71
- const extensionRows = await sql `
69
+ export async function extractExtensions(pool) {
70
+ const { rows: extensionRows } = await pool.query(sql `
72
71
  with extension_rows as (
73
72
  select
74
73
  e.oid,
@@ -259,10 +258,9 @@ export async function extractExtensions(sql) {
259
258
  ) as members
260
259
  from extension_rows er
261
260
  order by
262
- er.name;
263
- `;
264
- // Validate and parse each row using the Zod schema
265
- const validatedRows = extensionRows.map((row) => extensionPropsSchema.parse(row));
266
- return validatedRows.map((row) => new Extension(row));
267
- });
261
+ er.name
262
+ `);
263
+ // Validate and parse each row using the Zod schema
264
+ const validatedRows = extensionRows.map((row) => extensionPropsSchema.parse(row));
265
+ return validatedRows.map((row) => new Extension(row));
268
266
  }
@@ -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";
@@ -55,5 +55,5 @@ export declare class ForeignDataWrapper extends BasePgModel {
55
55
  }[];
56
56
  };
57
57
  }
58
- export declare function extractForeignDataWrappers(sql: Sql): Promise<ForeignDataWrapper[]>;
58
+ export declare function extractForeignDataWrappers(pool: Pool): Promise<ForeignDataWrapper[]>;
59
59
  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";
@@ -59,10 +60,8 @@ export class ForeignDataWrapper extends BasePgModel {
59
60
  };
60
61
  }
61
62
  }
62
- export async function extractForeignDataWrappers(sql) {
63
- return sql.begin(async (sql) => {
64
- await sql `set search_path = ''`;
65
- const fdwRows = await sql `
63
+ export async function extractForeignDataWrappers(pool) {
64
+ const { rows: fdwRows } = await pool.query(sql `
66
65
  with extension_oids as (
67
66
  select objid
68
67
  from pg_depend d
@@ -104,25 +103,24 @@ export async function extractForeignDataWrappers(sql) {
104
103
  not fdw.fdwname like any(array['pg\\_%'])
105
104
  and e.objid is null
106
105
  order by
107
- fdw.fdwname;
108
- `;
109
- // Validate and parse each row using the Zod schema
110
- const validatedRows = fdwRows.map((row) => {
111
- const parsed = foreignDataWrapperPropsSchema.parse(row);
112
- // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
113
- if (parsed.options && parsed.options.length > 0) {
114
- const parsedOptions = [];
115
- for (const opt of parsed.options) {
116
- const eqIndex = opt.indexOf("=");
117
- if (eqIndex > 0) {
118
- parsedOptions.push(opt.substring(0, eqIndex));
119
- parsedOptions.push(opt.substring(eqIndex + 1));
120
- }
106
+ fdw.fdwname
107
+ `);
108
+ // Validate and parse each row using the Zod schema
109
+ const validatedRows = fdwRows.map((row) => {
110
+ const parsed = foreignDataWrapperPropsSchema.parse(row);
111
+ // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
112
+ if (parsed.options && parsed.options.length > 0) {
113
+ const parsedOptions = [];
114
+ for (const opt of parsed.options) {
115
+ const eqIndex = opt.indexOf("=");
116
+ if (eqIndex > 0) {
117
+ parsedOptions.push(opt.substring(0, eqIndex));
118
+ parsedOptions.push(opt.substring(eqIndex + 1));
121
119
  }
122
- parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
123
120
  }
124
- return parsed;
125
- });
126
- return validatedRows.map((row) => new ForeignDataWrapper(row));
121
+ parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
122
+ }
123
+ return parsed;
127
124
  });
125
+ return validatedRows.map((row) => new ForeignDataWrapper(row));
128
126
  }
@@ -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";
@@ -113,5 +113,5 @@ export declare class ForeignTable extends BasePgModel implements TableLikeObject
113
113
  };
114
114
  };
115
115
  }
116
- export declare function extractForeignTables(sql: Sql): Promise<ForeignTable[]>;
116
+ export declare function extractForeignTables(pool: Pool): Promise<ForeignTable[]>;
117
117
  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";
@@ -82,10 +83,8 @@ export class ForeignTable extends BasePgModel {
82
83
  };
83
84
  }
84
85
  }
85
- export async function extractForeignTables(sql) {
86
- return sql.begin(async (sql) => {
87
- await sql `set search_path = ''`;
88
- const tableRows = await sql `
86
+ export async function extractForeignTables(pool) {
87
+ const { rows: tableRows } = await pool.query(sql `
89
88
  with extension_oids as (
90
89
  select objid
91
90
  from pg_depend d
@@ -191,25 +190,24 @@ export async function extractForeignTables(sql) {
191
190
  group by
192
191
  ft.oid, ft.schema, ft.name, ft.owner, ft.server, ft.options
193
192
  order by
194
- ft.schema, ft.name;
195
- `;
196
- // Validate and parse each row using the Zod schema
197
- const validatedRows = tableRows.map((row) => {
198
- const parsed = foreignTablePropsSchema.parse(row);
199
- // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
200
- if (parsed.options && parsed.options.length > 0) {
201
- const parsedOptions = [];
202
- for (const opt of parsed.options) {
203
- const eqIndex = opt.indexOf("=");
204
- if (eqIndex > 0) {
205
- parsedOptions.push(opt.substring(0, eqIndex));
206
- parsedOptions.push(opt.substring(eqIndex + 1));
207
- }
193
+ ft.schema, ft.name
194
+ `);
195
+ // Validate and parse each row using the Zod schema
196
+ const validatedRows = tableRows.map((row) => {
197
+ const parsed = foreignTablePropsSchema.parse(row);
198
+ // Parse options from PostgreSQL format ['key=value'] to ['key', 'value']
199
+ if (parsed.options && parsed.options.length > 0) {
200
+ const parsedOptions = [];
201
+ for (const opt of parsed.options) {
202
+ const eqIndex = opt.indexOf("=");
203
+ if (eqIndex > 0) {
204
+ parsedOptions.push(opt.substring(0, eqIndex));
205
+ parsedOptions.push(opt.substring(eqIndex + 1));
208
206
  }
209
- parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
210
207
  }
211
- return parsed;
212
- });
213
- return validatedRows.map((row) => new ForeignTable(row));
208
+ parsed.options = parsedOptions.length > 0 ? parsedOptions : null;
209
+ }
210
+ return parsed;
214
211
  });
212
+ return validatedRows.map((row) => new ForeignTable(row));
215
213
  }
@@ -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";
@@ -58,5 +58,5 @@ export declare class Server extends BasePgModel {
58
58
  }[];
59
59
  };
60
60
  }
61
- export declare function extractServers(sql: Sql): Promise<Server[]>;
61
+ export declare function extractServers(pool: Pool): Promise<Server[]>;
62
62
  export {};