@supabase/pg-delta 1.0.0-alpha.7 → 1.0.0-alpha.9

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.
@@ -27,7 +27,7 @@ export const declarativeApplyCommand = buildCommand({
27
27
  parse: Number,
28
28
  optional: true,
29
29
  },
30
- "no-validate-functions": {
30
+ "skip-function-validation": {
31
31
  kind: "boolean",
32
32
  brief: "Skip final function body validation pass",
33
33
  optional: true,
@@ -110,7 +110,7 @@ Tip: Use DEBUG=pg-delta:declarative-apply for detailed defer/skip/fail logs (whi
110
110
  content,
111
111
  targetUrl: flags.target,
112
112
  maxRounds: flags["max-rounds"],
113
- validateFunctionBodies: !flags["no-validate-functions"],
113
+ validateFunctionBodies: !flags["skip-function-validation"],
114
114
  onRoundComplete,
115
115
  });
116
116
  }
@@ -2,7 +2,7 @@
2
2
  * Plan creation - the main entry point for creating migration plans.
3
3
  */
4
4
  import type { Pool } from "pg";
5
- import { Catalog } from "../catalog.model.ts";
5
+ import type { Catalog } from "../catalog.model.ts";
6
6
  import type { Change } from "../change.types.ts";
7
7
  import type { DiffContext } from "../context.ts";
8
8
  import type { CreatePlanOptions, Plan } from "./types.ts";
@@ -3,13 +3,29 @@
3
3
  */
4
4
  import { escapeIdentifier } from "pg";
5
5
  import { diffCatalogs } from "../catalog.diff.js";
6
- import { Catalog, createEmptyCatalog, extractCatalog, } from "../catalog.model.js";
6
+ import { createEmptyCatalog, extractCatalog } from "../catalog.model.js";
7
7
  import { buildPlanScopeFingerprint, hashStableIds } from "../fingerprint.js";
8
8
  import { compileFilterDSL, } from "../integrations/filter/dsl.js";
9
9
  import { compileSerializeDSL, } from "../integrations/serialize/dsl.js";
10
10
  import { createManagedPool, endPool } from "../postgres-config.js";
11
11
  import { sortChanges } from "../sort/sort-changes.js";
12
12
  import { classifyChangesRisk } from "./risk.js";
13
+ /**
14
+ * Bundle-safe catalog detection: treat input as a resolved Catalog when it has
15
+ * the catalog shape and is not a pg Pool. Deserialized or cross-bundle Catalog
16
+ * instances may fail `instanceof Catalog` but pass this guard.
17
+ */
18
+ function isResolvedCatalog(input) {
19
+ return (typeof input === "object" &&
20
+ input !== null &&
21
+ typeof input.query !== "function" &&
22
+ "version" in input &&
23
+ "currentUser" in input &&
24
+ "depends" in input &&
25
+ "schemas" in input &&
26
+ "tables" in input &&
27
+ "views" in input);
28
+ }
13
29
  /**
14
30
  * Create a migration plan by comparing two catalog states.
15
31
  *
@@ -42,7 +58,7 @@ export async function createPlan(source, target, options = {}) {
42
58
  * Resolve a CatalogInput to a Catalog, tracking pools that need cleanup.
43
59
  */
44
60
  const resolveCatalog = async (input, label, pools) => {
45
- if (input instanceof Catalog) {
61
+ if (isResolvedCatalog(input)) {
46
62
  return input;
47
63
  }
48
64
  const resolved = await resolvePool(input, label);
@@ -107,8 +107,49 @@ export function createPool(connectionString, options) {
107
107
  connectionTimeoutMillis: DEFAULT_CONNECTION_TIMEOUT_MS,
108
108
  ...config,
109
109
  });
110
- if (onConnect)
111
- pool.on("connect", onConnect);
110
+ if (onConnect) {
111
+ const pendingClientSetup = new WeakMap();
112
+ const waitForClientSetup = async (client) => {
113
+ const setup = pendingClientSetup.get(client);
114
+ if (setup) {
115
+ await setup;
116
+ return;
117
+ }
118
+ throw new Error("Internal error: pool client was acquired before async onConnect setup was registered. This indicates a bug in the pool wrapper logic; please report it with reproduction steps.");
119
+ };
120
+ const originalConnect = pool.connect.bind(pool);
121
+ pool.on("connect", (client) => {
122
+ pendingClientSetup.set(client, Promise.resolve().then(() => onConnect(client)));
123
+ });
124
+ pool.connect = ((callback) => {
125
+ if (!callback) {
126
+ return originalConnect().then(async (client) => {
127
+ try {
128
+ await waitForClientSetup(client);
129
+ return client;
130
+ }
131
+ catch (setupError) {
132
+ client.release?.(setupError);
133
+ throw setupError;
134
+ }
135
+ });
136
+ }
137
+ return originalConnect(async (err, client, release) => {
138
+ if (err || !client) {
139
+ callback(err, client, release);
140
+ return;
141
+ }
142
+ try {
143
+ await waitForClientSetup(client);
144
+ callback(err, client, release);
145
+ }
146
+ catch (setupError) {
147
+ release(setupError);
148
+ callback(setupError, undefined, () => { });
149
+ }
150
+ });
151
+ });
152
+ }
112
153
  if (onError)
113
154
  pool.on("error", onError);
114
155
  if (onAcquire)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@supabase/pg-delta",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.9",
4
4
  "description": "PostgreSQL migrations made easy",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -42,7 +42,7 @@ export const declarativeApplyCommand = buildCommand({
42
42
  parse: Number,
43
43
  optional: true,
44
44
  },
45
- "no-validate-functions": {
45
+ "skip-function-validation": {
46
46
  kind: "boolean",
47
47
  brief: "Skip final function body validation pass",
48
48
  optional: true,
@@ -93,7 +93,7 @@ Tip: Use DEBUG=pg-delta:declarative-apply for detailed defer/skip/fail logs (whi
93
93
  path: string;
94
94
  target: string;
95
95
  "max-rounds"?: number;
96
- "no-validate-functions"?: boolean;
96
+ "skip-function-validation"?: boolean;
97
97
  verbose?: boolean;
98
98
  "ungroup-diagnostics"?: boolean;
99
99
  },
@@ -144,7 +144,7 @@ Tip: Use DEBUG=pg-delta:declarative-apply for detailed defer/skip/fail logs (whi
144
144
  content,
145
145
  targetUrl: flags.target,
146
146
  maxRounds: flags["max-rounds"],
147
- validateFunctionBodies: !flags["no-validate-functions"],
147
+ validateFunctionBodies: !flags["skip-function-validation"],
148
148
  onRoundComplete,
149
149
  });
150
150
  } catch (error) {
@@ -289,6 +289,19 @@ describe("catalog snapshot serde", () => {
289
289
  expect(result).toBeNull();
290
290
  });
291
291
 
292
+ test("createPlan accepts catalog-shaped plain object (bundle-boundary regression)", async () => {
293
+ const { createPlan } = await import("./plan/create.ts");
294
+
295
+ const sourceCatalog = await createEmptyCatalog(160000, "postgres");
296
+ const targetCatalog = await createEmptyCatalog(160000, "postgres");
297
+ const source = { ...sourceCatalog };
298
+
299
+ expect(source instanceof Catalog).toBe(false);
300
+
301
+ const result = await createPlan(source as Catalog, targetCatalog);
302
+ expect(result).toBeNull();
303
+ });
304
+
292
305
  test("createPlan with null source produces plan when target has objects", async () => {
293
306
  const { createPlan } = await import("./plan/create.ts");
294
307
 
@@ -5,11 +5,8 @@
5
5
  import type { Pool } from "pg";
6
6
  import { escapeIdentifier } from "pg";
7
7
  import { diffCatalogs } from "../catalog.diff.ts";
8
- import {
9
- Catalog,
10
- createEmptyCatalog,
11
- extractCatalog,
12
- } from "../catalog.model.ts";
8
+ import type { Catalog } from "../catalog.model.ts";
9
+ import { createEmptyCatalog, extractCatalog } from "../catalog.model.ts";
13
10
  import type { Change } from "../change.types.ts";
14
11
  import type { DiffContext } from "../context.ts";
15
12
  import { buildPlanScopeFingerprint, hashStableIds } from "../fingerprint.ts";
@@ -38,6 +35,25 @@ import type { CreatePlanOptions, Plan } from "./types.ts";
38
35
  */
39
36
  export type CatalogInput = string | Pool | Catalog;
40
37
 
38
+ /**
39
+ * Bundle-safe catalog detection: treat input as a resolved Catalog when it has
40
+ * the catalog shape and is not a pg Pool. Deserialized or cross-bundle Catalog
41
+ * instances may fail `instanceof Catalog` but pass this guard.
42
+ */
43
+ function isResolvedCatalog(input: CatalogInput): input is Catalog {
44
+ return (
45
+ typeof input === "object" &&
46
+ input !== null &&
47
+ typeof (input as { query?: unknown }).query !== "function" &&
48
+ "version" in input &&
49
+ "currentUser" in input &&
50
+ "depends" in input &&
51
+ "schemas" in input &&
52
+ "tables" in input &&
53
+ "views" in input
54
+ );
55
+ }
56
+
41
57
  /**
42
58
  * Create a migration plan by comparing two catalog states.
43
59
  *
@@ -82,7 +98,7 @@ export async function createPlan(
82
98
  label: "source" | "target",
83
99
  pools: Array<{ pool: Pool; shouldClose: boolean }>,
84
100
  ): Promise<Catalog> => {
85
- if (input instanceof Catalog) {
101
+ if (isResolvedCatalog(input)) {
86
102
  return input;
87
103
  }
88
104
  const resolved = await resolvePool(input, label);
@@ -139,7 +139,62 @@ export function createPool(
139
139
  ...config,
140
140
  });
141
141
 
142
- if (onConnect) pool.on("connect", onConnect);
142
+ if (onConnect) {
143
+ const pendingClientSetup = new WeakMap<PoolClient, Promise<void>>();
144
+ const waitForClientSetup = async (client: PoolClient) => {
145
+ const setup = pendingClientSetup.get(client);
146
+ if (setup) {
147
+ await setup;
148
+ return;
149
+ }
150
+ throw new Error(
151
+ "Internal error: pool client was acquired before async onConnect setup was registered. This indicates a bug in the pool wrapper logic; please report it with reproduction steps.",
152
+ );
153
+ };
154
+ const originalConnect = pool.connect.bind(pool);
155
+
156
+ pool.on("connect", (client) => {
157
+ pendingClientSetup.set(
158
+ client,
159
+ Promise.resolve().then(() => onConnect(client)),
160
+ );
161
+ });
162
+
163
+ pool.connect = ((
164
+ callback?: (
165
+ err: Error | undefined,
166
+ client: PoolClient | undefined,
167
+ release: (err?: Error | boolean) => void,
168
+ ) => void,
169
+ ) => {
170
+ if (!callback) {
171
+ return originalConnect().then(async (client) => {
172
+ try {
173
+ await waitForClientSetup(client);
174
+ return client;
175
+ } catch (setupError) {
176
+ (client as PoolClient).release?.(setupError as Error);
177
+ throw setupError;
178
+ }
179
+ });
180
+ }
181
+
182
+ return originalConnect(async (err, client, release) => {
183
+ if (err || !client) {
184
+ callback(err, client, release);
185
+ return;
186
+ }
187
+
188
+ try {
189
+ await waitForClientSetup(client);
190
+ callback(err, client, release);
191
+ } catch (setupError) {
192
+ release(setupError as Error);
193
+ callback(setupError as Error, undefined, () => {});
194
+ }
195
+ });
196
+ }) as Pool["connect"];
197
+ }
143
198
  if (onError) pool.on("error", onError);
144
199
  if (onAcquire) pool.on("acquire", onAcquire);
145
200
  if (onRemove) pool.on("remove", onRemove);