@malloy-publisher/server 0.0.188 → 0.0.382-dev

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 (52) hide show
  1. package/dist/app/api-doc.yaml +423 -60
  2. package/dist/app/assets/{HomePage-DsuUvSI_.js → HomePage-Dn3E4CuB.js} +1 -1
  3. package/dist/app/assets/{MainPage-DHWFkEN6.js → MainPage-BzB3yoqi.js} +1 -1
  4. package/dist/app/assets/{ModelPage-DNwcx1nE.js → ModelPage-C9O_sAXT.js} +1 -1
  5. package/dist/app/assets/{PackagePage-DSgz9G2V.js → PackagePage-DcxKEjBX.js} +1 -1
  6. package/dist/app/assets/{ProjectPage-CSdPosLV.js → ProjectPage-BDj307rF.js} +1 -1
  7. package/dist/app/assets/{RouteError-orw1RX8q.js → RouteError-DAShbVCG.js} +1 -1
  8. package/dist/app/assets/{WorkbookPage-Bp-BpGjL.js → WorkbookPage-Cs_XYEaB.js} +1 -1
  9. package/dist/app/assets/{core-B4ZYB7aS.es-8Zh0TkSr.js → core-CjeTkq8O.es-BqRc6yhC.js} +1 -1
  10. package/dist/app/assets/{index-BL2TJgTw.js → index-15BOvhp0.js} +4 -4
  11. package/dist/app/assets/{index-BWJkzsfl.js → index-Bb2jqquW.js} +1 -1
  12. package/dist/app/assets/{index-BefdHHMa.js → index-D68X76-7.js} +1 -1
  13. package/dist/app/assets/{index.umd-lY-87l4L.js → index.umd-DGBekgSu.js} +1 -1
  14. package/dist/app/index.html +1 -1
  15. package/dist/instrumentation.js +98 -77
  16. package/dist/server.js +1834 -450
  17. package/package.json +5 -3
  18. package/src/controller/connection.controller.ts +27 -20
  19. package/src/controller/manifest.controller.ts +29 -0
  20. package/src/controller/materialization.controller.ts +125 -0
  21. package/src/controller/model.controller.ts +0 -2
  22. package/src/controller/package.controller.ts +53 -2
  23. package/src/errors.ts +24 -0
  24. package/src/server.ts +196 -5
  25. package/src/service/manifest_service.spec.ts +201 -0
  26. package/src/service/manifest_service.ts +106 -0
  27. package/src/service/materialization_service.spec.ts +648 -0
  28. package/src/service/materialization_service.ts +929 -0
  29. package/src/service/materialized_table_gc.spec.ts +383 -0
  30. package/src/service/materialized_table_gc.ts +279 -0
  31. package/src/service/model.ts +25 -4
  32. package/src/service/package.ts +50 -0
  33. package/src/service/project_store.ts +21 -2
  34. package/src/service/quoting.ts +41 -0
  35. package/src/service/resolve_project.ts +13 -0
  36. package/src/storage/DatabaseInterface.ts +103 -1
  37. package/src/storage/{StorageManager.spec.ts → StorageManager.mock.ts} +9 -0
  38. package/src/storage/StorageManager.ts +119 -1
  39. package/src/storage/duckdb/DuckDBManifestStore.ts +70 -0
  40. package/src/storage/duckdb/DuckDBRepository.ts +99 -9
  41. package/src/storage/duckdb/ManifestRepository.ts +119 -0
  42. package/src/storage/duckdb/MaterializationRepository.ts +249 -0
  43. package/src/storage/duckdb/manifest_store.spec.ts +133 -0
  44. package/src/storage/duckdb/schema.ts +59 -1
  45. package/src/storage/ducklake/DuckLakeManifestStore.ts +146 -0
  46. package/tests/fixtures/persist-test/data/orders.csv +5 -0
  47. package/tests/fixtures/persist-test/persist_test.malloy +11 -0
  48. package/tests/fixtures/persist-test/publisher.json +5 -0
  49. package/tests/fixtures/publisher.config.json +15 -0
  50. package/tests/harness/rest_e2e.ts +68 -0
  51. package/tests/integration/materialization/materialization_lifecycle.integration.spec.ts +470 -0
  52. package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +2 -2
@@ -0,0 +1,648 @@
1
+ import type { Connection } from "@malloydata/malloy";
2
+ import { beforeEach, describe, expect, it } from "bun:test";
3
+ import * as sinon from "sinon";
4
+ import {
5
+ InvalidStateTransitionError,
6
+ MaterializationConflictError,
7
+ MaterializationNotFoundError,
8
+ ProjectNotFoundError,
9
+ } from "../errors";
10
+ import { DuplicateActiveMaterializationError } from "../storage/duckdb/MaterializationRepository";
11
+ import {
12
+ ManifestEntry,
13
+ Materialization,
14
+ MaterializationStatus,
15
+ ResourceRepository,
16
+ } from "../storage/DatabaseInterface";
17
+ import { ManifestService } from "./manifest_service";
18
+ import {
19
+ manifestTableKey,
20
+ MaterializationService,
21
+ tablePhysicallyExists,
22
+ } from "./materialization_service";
23
+ import { ProjectStore } from "./project_store";
24
+
25
+ function makeExecution(
26
+ overrides: Partial<Materialization> = {},
27
+ ): Materialization {
28
+ return {
29
+ id: "exec-1",
30
+ projectId: "proj-1",
31
+ packageName: "pkg",
32
+ status: "PENDING",
33
+ startedAt: null,
34
+ completedAt: null,
35
+ error: null,
36
+ metadata: null,
37
+ createdAt: new Date("2025-01-01"),
38
+ updatedAt: new Date("2025-01-01"),
39
+ ...overrides,
40
+ };
41
+ }
42
+
43
+ type MockRepo = sinon.SinonStubbedInstance<ResourceRepository>;
44
+
45
+ function createMocks() {
46
+ const sandbox = sinon.createSandbox();
47
+
48
+ const repository: MockRepo = {
49
+ listProjects: sandbox.stub(),
50
+ getProjectById: sandbox.stub(),
51
+ getProjectByName: sandbox.stub(),
52
+ createProject: sandbox.stub(),
53
+ updateProject: sandbox.stub(),
54
+ deleteProject: sandbox.stub(),
55
+ listPackages: sandbox.stub(),
56
+ getPackageById: sandbox.stub(),
57
+ getPackageByName: sandbox.stub(),
58
+ createPackage: sandbox.stub(),
59
+ updatePackage: sandbox.stub(),
60
+ deletePackage: sandbox.stub(),
61
+ listConnections: sandbox.stub(),
62
+ getConnectionById: sandbox.stub(),
63
+ getConnectionByName: sandbox.stub(),
64
+ createConnection: sandbox.stub(),
65
+ updateConnection: sandbox.stub(),
66
+ deleteConnection: sandbox.stub(),
67
+ listMaterializations: sandbox.stub(),
68
+ getMaterializationById: sandbox.stub(),
69
+ getActiveMaterialization: sandbox.stub(),
70
+ createMaterialization: sandbox.stub(),
71
+ updateMaterialization: sandbox.stub(),
72
+ deleteMaterialization: sandbox.stub(),
73
+ listManifestEntries: sandbox.stub(),
74
+ upsertManifestEntry: sandbox.stub(),
75
+ deleteManifestEntry: sandbox.stub(),
76
+ } as unknown as MockRepo;
77
+
78
+ const storageManager = {
79
+ getRepository: () => repository,
80
+ getManifestStore: sandbox.stub(),
81
+ };
82
+
83
+ const projectStore = {
84
+ storageManager,
85
+ getProject: sandbox.stub(),
86
+ } as unknown as ProjectStore;
87
+
88
+ const manifestService = {
89
+ getManifest: sandbox.stub().resolves({ entries: {}, strict: false }),
90
+ writeEntry: sandbox.stub().resolves(),
91
+ deleteEntry: sandbox.stub().resolves(),
92
+ reloadManifest: sandbox.stub().resolves({ entries: {}, strict: false }),
93
+ listEntries: sandbox.stub().resolves([]),
94
+ } as unknown as sinon.SinonStubbedInstance<ManifestService>;
95
+
96
+ const service = new MaterializationService(
97
+ projectStore,
98
+ manifestService as unknown as ManifestService,
99
+ );
100
+
101
+ // Default: resolveProjectId succeeds
102
+ repository.getProjectByName.resolves({
103
+ id: "proj-1",
104
+ name: "my-project",
105
+ path: "/test",
106
+ createdAt: new Date(),
107
+ updatedAt: new Date(),
108
+ });
109
+
110
+ return { sandbox, repository, projectStore, manifestService, service };
111
+ }
112
+
113
+ describe("MaterializationService", () => {
114
+ let ctx: ReturnType<typeof createMocks>;
115
+
116
+ beforeEach(() => {
117
+ ctx = createMocks();
118
+ });
119
+
120
+ // ==================== resolveProjectId ====================
121
+
122
+ describe("resolveProjectId (via listMaterializations)", () => {
123
+ it("should throw ProjectNotFoundError when project is not in DB", async () => {
124
+ ctx.repository.getProjectByName.resolves(null);
125
+
126
+ await expect(
127
+ ctx.service.listMaterializations("unknown", "pkg"),
128
+ ).rejects.toThrow(ProjectNotFoundError);
129
+ });
130
+ });
131
+
132
+ // ==================== BUILD QUERIES ====================
133
+
134
+ describe("listMaterializations", () => {
135
+ it("should list builds for a package", async () => {
136
+ const builds = [makeExecution(), makeExecution({ id: "exec-2" })];
137
+ ctx.repository.listMaterializations.resolves(builds);
138
+
139
+ const result = await ctx.service.listMaterializations(
140
+ "my-project",
141
+ "pkg",
142
+ );
143
+
144
+ expect(result).toEqual(builds);
145
+ });
146
+ });
147
+
148
+ describe("getMaterialization", () => {
149
+ it("should return a specific build", async () => {
150
+ const exec = makeExecution();
151
+ ctx.repository.getMaterializationById.resolves(exec);
152
+
153
+ const result = await ctx.service.getMaterialization(
154
+ "my-project",
155
+ "pkg",
156
+ "exec-1",
157
+ );
158
+
159
+ expect(result).toEqual(exec);
160
+ });
161
+
162
+ it("should throw when build not found", async () => {
163
+ ctx.repository.getMaterializationById.resolves(null);
164
+
165
+ await expect(
166
+ ctx.service.getMaterialization("my-project", "pkg", "missing"),
167
+ ).rejects.toThrow(MaterializationNotFoundError);
168
+ });
169
+
170
+ it("should throw when build belongs to a different package", async () => {
171
+ ctx.repository.getMaterializationById.resolves(
172
+ makeExecution({ packageName: "other-pkg" }),
173
+ );
174
+
175
+ await expect(
176
+ ctx.service.getMaterialization("my-project", "pkg", "exec-1"),
177
+ ).rejects.toThrow(MaterializationNotFoundError);
178
+ });
179
+ });
180
+
181
+ // ==================== STATE MACHINE ====================
182
+
183
+ describe("state transitions", () => {
184
+ const validTransitions: [MaterializationStatus, MaterializationStatus][] =
185
+ [
186
+ ["PENDING", "RUNNING"],
187
+ ["PENDING", "CANCELLED"],
188
+ ["RUNNING", "SUCCESS"],
189
+ ["RUNNING", "FAILED"],
190
+ ["RUNNING", "CANCELLED"],
191
+ ];
192
+
193
+ const invalidTransitions: [
194
+ MaterializationStatus,
195
+ MaterializationStatus,
196
+ ][] = [
197
+ ["PENDING", "SUCCESS"],
198
+ ["PENDING", "FAILED"],
199
+ ["RUNNING", "PENDING"],
200
+ ["SUCCESS", "RUNNING"],
201
+ ["SUCCESS", "FAILED"],
202
+ ["FAILED", "RUNNING"],
203
+ ["FAILED", "SUCCESS"],
204
+ ["CANCELLED", "RUNNING"],
205
+ ["CANCELLED", "SUCCESS"],
206
+ ];
207
+
208
+ for (const [from, to] of validTransitions) {
209
+ it(`should allow ${from} -> ${to}`, async () => {
210
+ const exec = makeExecution({ status: from });
211
+ ctx.repository.getMaterializationById.resolves(exec);
212
+ ctx.repository.updateMaterialization.resolves(
213
+ makeExecution({ status: to }),
214
+ );
215
+
216
+ // Trigger via stopMaterialization for RUNNING->CANCELLED (orphaned path)
217
+ if (from === "RUNNING" && to === "CANCELLED") {
218
+ const result = await ctx.service.stopMaterialization(
219
+ "my-project",
220
+ "pkg",
221
+ exec.id,
222
+ );
223
+ expect(result).not.toBeNull();
224
+ }
225
+ });
226
+ }
227
+
228
+ for (const [from, to] of invalidTransitions) {
229
+ it(`should reject ${from} -> ${to}`, async () => {
230
+ const exec = makeExecution({ status: from });
231
+ ctx.repository.getMaterializationById.resolves(exec);
232
+
233
+ await expect(
234
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
235
+ (ctx.service as any).transitionExecution(exec.id, to),
236
+ ).rejects.toThrow(InvalidStateTransitionError);
237
+ });
238
+ }
239
+ });
240
+
241
+ // ==================== CREATE / START / STOP ====================
242
+
243
+ describe("createMaterialization", () => {
244
+ it("should create a PENDING build", async () => {
245
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
246
+ getPackage: sinon.stub().resolves({}),
247
+ });
248
+ ctx.repository.getActiveMaterialization.resolves(null);
249
+ const pending = makeExecution({
250
+ status: "PENDING",
251
+ metadata: { forceRefresh: false, autoLoadManifest: true },
252
+ });
253
+ ctx.repository.createMaterialization.resolves(pending);
254
+
255
+ const result = await ctx.service.createMaterialization(
256
+ "my-project",
257
+ "pkg",
258
+ {
259
+ autoLoadManifest: true,
260
+ },
261
+ );
262
+
263
+ expect(result.status).toBe("PENDING");
264
+ expect(
265
+ (result.metadata as Record<string, unknown>)?.autoLoadManifest,
266
+ ).toBe(true);
267
+ // Options persist on the INSERT itself; no follow-up update.
268
+ expect(ctx.repository.createMaterialization.calledOnce).toBe(true);
269
+ expect(ctx.repository.createMaterialization.firstCall.args).toEqual([
270
+ "proj-1",
271
+ "pkg",
272
+ "PENDING",
273
+ { forceRefresh: false, autoLoadManifest: true },
274
+ ]);
275
+ expect(ctx.repository.updateMaterialization.called).toBe(false);
276
+ });
277
+
278
+ it("should reject creation when an active materialization exists", async () => {
279
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
280
+ getPackage: sinon.stub().resolves({}),
281
+ });
282
+ ctx.repository.getActiveMaterialization.resolves(
283
+ makeExecution({ id: "existing", status: "RUNNING" }),
284
+ );
285
+
286
+ await expect(
287
+ ctx.service.createMaterialization("my-project", "pkg"),
288
+ ).rejects.toThrow(MaterializationConflictError);
289
+ });
290
+
291
+ it("should translate DuplicateActiveMaterializationError from a lost race", async () => {
292
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
293
+ getPackage: sinon.stub().resolves({}),
294
+ });
295
+ // The pre-check finds nothing (race is still possible), but the
296
+ // atomic insert loses to a concurrent create and the repository
297
+ // raises DuplicateActiveMaterializationError.
298
+ ctx.repository.getActiveMaterialization
299
+ .onFirstCall()
300
+ .resolves(null)
301
+ .onSecondCall()
302
+ .resolves(makeExecution({ id: "winner", status: "PENDING" }));
303
+ ctx.repository.createMaterialization.rejects(
304
+ new DuplicateActiveMaterializationError("proj-1", "pkg"),
305
+ );
306
+
307
+ await expect(
308
+ ctx.service.createMaterialization("my-project", "pkg"),
309
+ ).rejects.toThrow(/winner/);
310
+ });
311
+ });
312
+
313
+ describe("startMaterialization", () => {
314
+ it("should transition PENDING to RUNNING", async () => {
315
+ const pending = makeExecution({
316
+ status: "PENDING",
317
+ metadata: { forceRefresh: false, autoLoadManifest: false },
318
+ });
319
+ const running = makeExecution({ status: "RUNNING" });
320
+ ctx.repository.getMaterializationById.resolves(pending);
321
+ ctx.repository.getActiveMaterialization.resolves(null);
322
+ ctx.repository.updateMaterialization.resolves(running);
323
+
324
+ const result = await ctx.service.startMaterialization(
325
+ "my-project",
326
+ "pkg",
327
+ "exec-1",
328
+ );
329
+
330
+ expect(result.status).toBe("RUNNING");
331
+ });
332
+
333
+ it("should reject non-PENDING builds", async () => {
334
+ const running = makeExecution({ status: "RUNNING" });
335
+ ctx.repository.getMaterializationById.resolves(running);
336
+
337
+ await expect(
338
+ ctx.service.startMaterialization("my-project", "pkg", "exec-1"),
339
+ ).rejects.toThrow(InvalidStateTransitionError);
340
+ });
341
+
342
+ it("should throw MaterializationConflictError when another is already running", async () => {
343
+ const pending = makeExecution({
344
+ status: "PENDING",
345
+ metadata: { forceRefresh: false, autoLoadManifest: false },
346
+ });
347
+ ctx.repository.getMaterializationById.resolves(pending);
348
+ ctx.repository.getActiveMaterialization.resolves(
349
+ makeExecution({ id: "other", status: "RUNNING" }),
350
+ );
351
+
352
+ await expect(
353
+ ctx.service.startMaterialization("my-project", "pkg", "exec-1"),
354
+ ).rejects.toThrow(MaterializationConflictError);
355
+ });
356
+ });
357
+
358
+ describe("stopMaterialization", () => {
359
+ it("should cancel a PENDING build", async () => {
360
+ const pending = makeExecution({ status: "PENDING" });
361
+ ctx.repository.getMaterializationById.resolves(pending);
362
+ ctx.repository.updateMaterialization.resolves(
363
+ makeExecution({ status: "CANCELLED" }),
364
+ );
365
+
366
+ const result = await ctx.service.stopMaterialization(
367
+ "my-project",
368
+ "pkg",
369
+ "exec-1",
370
+ );
371
+
372
+ expect(result.status).toBe("CANCELLED");
373
+ });
374
+
375
+ it("should force-cancel an orphaned RUNNING build", async () => {
376
+ const running = makeExecution({ id: "orphan", status: "RUNNING" });
377
+ ctx.repository.getMaterializationById.resolves(running);
378
+ ctx.repository.updateMaterialization.resolves(
379
+ makeExecution({ id: "orphan", status: "CANCELLED" }),
380
+ );
381
+
382
+ const result = await ctx.service.stopMaterialization(
383
+ "my-project",
384
+ "pkg",
385
+ "orphan",
386
+ );
387
+
388
+ expect(result.status).toBe("CANCELLED");
389
+ });
390
+
391
+ it("should reject stopping a terminal build", async () => {
392
+ const succeeded = makeExecution({ status: "SUCCESS" });
393
+ ctx.repository.getMaterializationById.resolves(succeeded);
394
+
395
+ await expect(
396
+ ctx.service.stopMaterialization("my-project", "pkg", "exec-1"),
397
+ ).rejects.toThrow(InvalidStateTransitionError);
398
+ });
399
+ });
400
+
401
+ // ==================== DELETE ====================
402
+
403
+ describe("deleteMaterialization", () => {
404
+ it("should delete a SUCCESS materialization", async () => {
405
+ const succeeded = makeExecution({ status: "SUCCESS" });
406
+ ctx.repository.getMaterializationById.resolves(succeeded);
407
+ ctx.repository.deleteMaterialization.resolves();
408
+
409
+ await ctx.service.deleteMaterialization("my-project", "pkg", "exec-1");
410
+
411
+ expect(ctx.repository.deleteMaterialization.calledOnce).toBe(true);
412
+ expect(ctx.repository.deleteMaterialization.firstCall.args[0]).toBe(
413
+ "exec-1",
414
+ );
415
+ });
416
+
417
+ it("should delete a FAILED materialization", async () => {
418
+ const failed = makeExecution({ status: "FAILED" });
419
+ ctx.repository.getMaterializationById.resolves(failed);
420
+ ctx.repository.deleteMaterialization.resolves();
421
+
422
+ await ctx.service.deleteMaterialization("my-project", "pkg", "exec-1");
423
+
424
+ expect(ctx.repository.deleteMaterialization.calledOnce).toBe(true);
425
+ });
426
+
427
+ it("should delete a CANCELLED materialization", async () => {
428
+ const cancelled = makeExecution({ status: "CANCELLED" });
429
+ ctx.repository.getMaterializationById.resolves(cancelled);
430
+ ctx.repository.deleteMaterialization.resolves();
431
+
432
+ await ctx.service.deleteMaterialization("my-project", "pkg", "exec-1");
433
+
434
+ expect(ctx.repository.deleteMaterialization.calledOnce).toBe(true);
435
+ });
436
+
437
+ it("should reject deleting a PENDING materialization", async () => {
438
+ const pending = makeExecution({ status: "PENDING" });
439
+ ctx.repository.getMaterializationById.resolves(pending);
440
+
441
+ await expect(
442
+ ctx.service.deleteMaterialization("my-project", "pkg", "exec-1"),
443
+ ).rejects.toThrow(InvalidStateTransitionError);
444
+ });
445
+
446
+ it("should reject deleting a RUNNING materialization", async () => {
447
+ const running = makeExecution({ status: "RUNNING" });
448
+ ctx.repository.getMaterializationById.resolves(running);
449
+
450
+ await expect(
451
+ ctx.service.deleteMaterialization("my-project", "pkg", "exec-1"),
452
+ ).rejects.toThrow(InvalidStateTransitionError);
453
+ });
454
+
455
+ it("should throw when materialization not found", async () => {
456
+ ctx.repository.getMaterializationById.resolves(null);
457
+
458
+ await expect(
459
+ ctx.service.deleteMaterialization("my-project", "pkg", "missing"),
460
+ ).rejects.toThrow(MaterializationNotFoundError);
461
+ });
462
+ });
463
+
464
+ // ==================== TEARDOWN ====================
465
+
466
+ describe("teardownPackage", () => {
467
+ it("refuses to run while a materialization is active", async () => {
468
+ ctx.repository.getActiveMaterialization.resolves(
469
+ makeExecution({ status: "RUNNING" }),
470
+ );
471
+
472
+ await expect(
473
+ ctx.service.teardownPackage("my-project", "pkg"),
474
+ ).rejects.toThrow(MaterializationConflictError);
475
+ });
476
+
477
+ it("drops every manifest entry for the package and deletes its row", async () => {
478
+ const runSQL = sinon.stub().resolves();
479
+ const connection = {
480
+ dialectName: "duckdb",
481
+ runSQL,
482
+ } as unknown as Connection;
483
+ const connections = new Map<string, Connection>([
484
+ ["conn", connection],
485
+ ]);
486
+ const pkg = { getConnections: () => connections };
487
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
488
+ getPackage: sinon.stub().resolves(pkg),
489
+ });
490
+ ctx.repository.getActiveMaterialization.resolves(null);
491
+ const entries: ManifestEntry[] = [
492
+ {
493
+ id: "entry-1",
494
+ projectId: "proj-1",
495
+ packageName: "pkg",
496
+ buildId: "abcdef1234567890abcdef1234567890",
497
+ tableName: "table_a",
498
+ sourceName: "src",
499
+ connectionName: "conn",
500
+ createdAt: new Date(),
501
+ updatedAt: new Date(),
502
+ },
503
+ {
504
+ id: "entry-2",
505
+ projectId: "proj-1",
506
+ packageName: "pkg",
507
+ buildId: "1234567890abcdef1234567890abcdef",
508
+ tableName: "table_b",
509
+ sourceName: "src",
510
+ connectionName: "conn",
511
+ createdAt: new Date(),
512
+ updatedAt: new Date(),
513
+ },
514
+ ];
515
+ (ctx.manifestService.listEntries as sinon.SinonStub).resolves(entries);
516
+
517
+ const result = await ctx.service.teardownPackage("my-project", "pkg");
518
+
519
+ expect(result.dropped).toHaveLength(2);
520
+ expect(result.errors).toHaveLength(0);
521
+ // Each entry triggers target DROP + staging DROP = 2 calls per entry.
522
+ expect(runSQL.callCount).toBe(4);
523
+ expect(
524
+ (ctx.manifestService.deleteEntry as sinon.SinonStub).callCount,
525
+ ).toBe(2);
526
+ });
527
+
528
+ it("deletes rows whose connection is no longer registered (teardown force-delete)", async () => {
529
+ const runSQL = sinon.stub().resolves();
530
+ const livingConn = {
531
+ dialectName: "duckdb",
532
+ runSQL,
533
+ } as unknown as Connection;
534
+ // Only "live_conn" is registered; the manifest row below points at
535
+ // a vanished "ghost_conn", which used to be impossible to tear down.
536
+ // `teardownPackage` must force-delete the row anyway so teardown
537
+ // can complete.
538
+ const connections = new Map<string, Connection>([
539
+ ["live_conn", livingConn],
540
+ ]);
541
+ const pkg = { getConnections: () => connections };
542
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
543
+ getPackage: sinon.stub().resolves(pkg),
544
+ });
545
+ ctx.repository.getActiveMaterialization.resolves(null);
546
+ const entries: ManifestEntry[] = [
547
+ {
548
+ id: "entry-ghost",
549
+ projectId: "proj-1",
550
+ packageName: "pkg",
551
+ buildId: "abcdef1234567890abcdef1234567890",
552
+ tableName: "table_ghost",
553
+ sourceName: "src",
554
+ connectionName: "ghost_conn",
555
+ createdAt: new Date(),
556
+ updatedAt: new Date(),
557
+ },
558
+ ];
559
+ (ctx.manifestService.listEntries as sinon.SinonStub).resolves(entries);
560
+
561
+ const result = await ctx.service.teardownPackage("my-project", "pkg");
562
+
563
+ expect(result.dropped).toHaveLength(1);
564
+ expect(result.dropped[0].targetDropSkipped).toBe(true);
565
+ expect(result.errors).toHaveLength(0);
566
+ // No DROP on the vanished connection, but the manifest row is gone.
567
+ expect(runSQL.called).toBe(false);
568
+ expect(
569
+ (ctx.manifestService.deleteEntry as sinon.SinonStub).calledOnce,
570
+ ).toBe(true);
571
+ });
572
+
573
+ it("dryRun reports would-drop entries without running DROP or deleting rows", async () => {
574
+ const runSQL = sinon.stub().resolves();
575
+ const connection = {
576
+ dialectName: "duckdb",
577
+ runSQL,
578
+ } as unknown as Connection;
579
+ const connections = new Map<string, Connection>([
580
+ ["conn", connection],
581
+ ]);
582
+ const pkg = { getConnections: () => connections };
583
+ (ctx.projectStore.getProject as sinon.SinonStub).resolves({
584
+ getPackage: sinon.stub().resolves(pkg),
585
+ });
586
+ ctx.repository.getActiveMaterialization.resolves(null);
587
+ const entry: ManifestEntry = {
588
+ id: "entry-1",
589
+ projectId: "proj-1",
590
+ packageName: "pkg",
591
+ buildId: "abcdef1234567890abcdef1234567890",
592
+ tableName: "orphan",
593
+ sourceName: "src",
594
+ connectionName: "conn",
595
+ createdAt: new Date(),
596
+ updatedAt: new Date(),
597
+ };
598
+ (ctx.manifestService.listEntries as sinon.SinonStub).resolves([entry]);
599
+
600
+ const result = await ctx.service.teardownPackage("my-project", "pkg", {
601
+ dryRun: true,
602
+ });
603
+
604
+ expect(result.dropped).toHaveLength(1);
605
+ expect(result.dropped[0].tableName).toBe("orphan");
606
+ expect(runSQL.called).toBe(false);
607
+ expect(
608
+ (ctx.manifestService.deleteEntry as sinon.SinonStub).called,
609
+ ).toBe(false);
610
+ });
611
+ });
612
+ });
613
+
614
+ // ==================== HELPER UNIT TESTS ====================
615
+
616
+ describe("manifestTableKey", () => {
617
+ it("returns connectionName::tableName", () => {
618
+ expect(manifestTableKey("duckdb", "orders")).toBe("duckdb::orders");
619
+ });
620
+
621
+ it("handles schema-qualified table names", () => {
622
+ expect(manifestTableKey("pg", "analytics.summary")).toBe(
623
+ "pg::analytics.summary",
624
+ );
625
+ });
626
+ });
627
+
628
+ describe("tablePhysicallyExists", () => {
629
+ it("returns true when SELECT succeeds", async () => {
630
+ const conn = {
631
+ runSQL: sinon.stub().resolves({ rows: [] }),
632
+ } as unknown as Connection;
633
+
634
+ expect(await tablePhysicallyExists(conn, '"my_table"')).toBe(true);
635
+ expect((conn.runSQL as sinon.SinonStub).calledOnce).toBe(true);
636
+ expect((conn.runSQL as sinon.SinonStub).firstCall.args[0]).toBe(
637
+ 'SELECT 1 FROM "my_table" WHERE 1=0',
638
+ );
639
+ });
640
+
641
+ it("returns false when SELECT throws (table does not exist)", async () => {
642
+ const conn = {
643
+ runSQL: sinon.stub().rejects(new Error("table not found")),
644
+ } as unknown as Connection;
645
+
646
+ expect(await tablePhysicallyExists(conn, '"missing"')).toBe(false);
647
+ });
648
+ });