@geekmidas/testkit 10.0.0-alpha.15 → 10.0.0-alpha.150

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 (72) hide show
  1. package/README.md +85 -0
  2. package/dist/KyselyFactory.d.cts.map +1 -1
  3. package/dist/KyselyFactory.d.mts.map +1 -1
  4. package/dist/ObjectionFactory.d.cts.map +1 -1
  5. package/dist/ObjectionFactory.d.mts.map +1 -1
  6. package/dist/PostgresKyselyMigrator.d.cts +1 -1
  7. package/dist/PostgresKyselyMigrator.d.cts.map +1 -1
  8. package/dist/PostgresKyselyMigrator.d.mts +1 -1
  9. package/dist/PostgresKyselyMigrator.d.mts.map +1 -1
  10. package/dist/{PostgresMigrator-CoHQ95cT.d.cts → PostgresMigrator-CDPnnwMJ.d.cts} +10 -3
  11. package/dist/PostgresMigrator-CDPnnwMJ.d.cts.map +1 -0
  12. package/dist/{PostgresMigrator-CoHQ95cT.d.mts → PostgresMigrator-CDPnnwMJ.d.mts} +10 -3
  13. package/dist/PostgresMigrator-CDPnnwMJ.d.mts.map +1 -0
  14. package/dist/PostgresMigrator.cjs +14 -5
  15. package/dist/PostgresMigrator.cjs.map +1 -1
  16. package/dist/PostgresMigrator.d.cts +1 -1
  17. package/dist/PostgresMigrator.d.mts +1 -1
  18. package/dist/PostgresMigrator.mjs +14 -5
  19. package/dist/PostgresMigrator.mjs.map +1 -1
  20. package/dist/PostgresObjectionMigrator.d.cts +1 -1
  21. package/dist/PostgresObjectionMigrator.d.cts.map +1 -1
  22. package/dist/PostgresObjectionMigrator.d.mts +1 -1
  23. package/dist/PostgresObjectionMigrator.d.mts.map +1 -1
  24. package/dist/VitestTransactionIsolator.d.cts.map +1 -1
  25. package/dist/VitestTransactionIsolator.d.mts.map +1 -1
  26. package/dist/better-auth.d.cts.map +1 -1
  27. package/dist/better-auth.d.mts.map +1 -1
  28. package/dist/browser.cjs +211 -0
  29. package/dist/browser.cjs.map +1 -0
  30. package/dist/browser.d.cts +113 -0
  31. package/dist/browser.d.cts.map +1 -0
  32. package/dist/browser.d.mts +113 -0
  33. package/dist/browser.d.mts.map +1 -0
  34. package/dist/browser.mjs +207 -0
  35. package/dist/browser.mjs.map +1 -0
  36. package/dist/context.cjs +80 -0
  37. package/dist/context.cjs.map +1 -0
  38. package/dist/context.d.cts +55 -0
  39. package/dist/context.d.cts.map +1 -0
  40. package/dist/context.d.mts +55 -0
  41. package/dist/context.d.mts.map +1 -0
  42. package/dist/context.mjs +73 -0
  43. package/dist/context.mjs.map +1 -0
  44. package/dist/faker.cjs +51 -0
  45. package/dist/faker.cjs.map +1 -1
  46. package/dist/faker.d.cts +26 -19
  47. package/dist/faker.d.cts.map +1 -1
  48. package/dist/faker.d.mts +26 -19
  49. package/dist/faker.d.mts.map +1 -1
  50. package/dist/faker.mjs +51 -1
  51. package/dist/faker.mjs.map +1 -1
  52. package/dist/mailbox.cjs +94 -0
  53. package/dist/mailbox.cjs.map +1 -0
  54. package/dist/mailbox.d.cts +54 -0
  55. package/dist/mailbox.d.cts.map +1 -0
  56. package/dist/mailbox.d.mts +54 -0
  57. package/dist/mailbox.d.mts.map +1 -0
  58. package/dist/mailbox.mjs +91 -0
  59. package/dist/mailbox.mjs.map +1 -0
  60. package/dist/requestContext.d.cts.map +1 -1
  61. package/dist/requestContext.d.mts.map +1 -1
  62. package/dist/transactions.cjs +239 -0
  63. package/dist/transactions.cjs.map +1 -0
  64. package/dist/transactions.d.cts +44 -0
  65. package/dist/transactions.d.cts.map +1 -0
  66. package/dist/transactions.d.mts +44 -0
  67. package/dist/transactions.d.mts.map +1 -0
  68. package/dist/transactions.mjs +234 -0
  69. package/dist/transactions.mjs.map +1 -0
  70. package/package.json +57 -8
  71. package/dist/PostgresMigrator-CoHQ95cT.d.cts.map +0 -1
  72. package/dist/PostgresMigrator-CoHQ95cT.d.mts.map +0 -1
package/README.md CHANGED
@@ -54,6 +54,12 @@ import { itWithDir } from '@geekmidas/testkit/os';
54
54
  import { createMockContext, createMockV1Event, createMockV2Event } from '@geekmidas/testkit/aws';
55
55
  import { createMockLogger } from '@geekmidas/testkit/logger';
56
56
  import { memoryAdapter } from '@geekmidas/testkit/better-auth';
57
+
58
+ // Feature tests: the primitives `@geekmidas/constructs/testing` builds on
59
+ import { Browser } from '@geekmidas/testkit/browser';
60
+ import { createMailbox } from '@geekmidas/testkit/mailbox';
61
+ import { runInTestContext, installContextFetch } from '@geekmidas/testkit/context';
62
+ import { TransactionRegistry } from '@geekmidas/testkit/transactions';
57
63
  ```
58
64
 
59
65
  ## Quick Start
@@ -321,6 +327,85 @@ describe('Authentication', () => {
321
327
  });
322
328
  ```
323
329
 
330
+ ## Feature Test Primitives
331
+
332
+ The pieces a feature test is built from — a test that drives an app the way it
333
+ runs deployed: a browser signs in, calls the API, the API calls the auth server,
334
+ each over a URL, every database in a transaction that is rolled back. The wiring
335
+ that knows about constructs lives in `@geekmidas/constructs/testing`; these know
336
+ nothing about them.
337
+
338
+ ### `Browser`
339
+
340
+ A `fetch` with a cookie jar that follows the browser's rules (RFC 6265): a
341
+ `Set-Cookie` lands in the jar, and every later request to a URL the cookie
342
+ belongs to carries it. Redirects are followed hop by hop, keeping each hop's
343
+ cookies. Give it to an app's real clients:
344
+
345
+ ```typescript
346
+ import { Browser as TestBrowser } from '@geekmidas/testkit/browser';
347
+
348
+ export class Browser extends TestBrowser {
349
+ readonly api = createApi({ baseURL: process.env.API_URL, fetch: this.fetch });
350
+ }
351
+
352
+ const browser = new Browser();
353
+ await browser.visit(magicLink); // 302 + Set-Cookie → the jar
354
+ await browser.api.get('/profile'); // sent with the session cookie
355
+ const restore = browser.install(); // the global fetch, for module-level clients
356
+ ```
357
+
358
+ On the server side of a test — a handler serving a request — `browser.fetch` is
359
+ the plain `fetch`: a server forwards what it was handed and never reaches into a
360
+ browser's jar.
361
+
362
+ Given the address of a site on every subdomain, `on(slug)` is the same browser
363
+ (the same jar, address and clients) on that tenant's host. It sends `Origin` on
364
+ every request and opens a relative `visit` there:
365
+
366
+ ```typescript
367
+ const browser = new Browser({ site: 'https://*.test.shop.localhost:8443' });
368
+ await browser.on('acme').api.get('/orders'); // Origin: https://acme.test.shop.localhost:8443
369
+ ```
370
+
371
+ A feature test passes `site` itself when the app declares one.
372
+
373
+ ### `createMailbox`
374
+
375
+ Reads the mail an app actually sent, from Mailpit's HTTP API. One address per
376
+ test keeps tests apart, since Mailpit has one inbox and no transactions:
377
+
378
+ ```typescript
379
+ const mailbox = createMailbox({ inbox: process.env.MAILER_INBOX_URL! });
380
+
381
+ const email = await mailbox('ada@shop.test').last(); // waits for it to arrive
382
+ await browser.visit(email.link!);
383
+ await mailbox('ada@shop.test').clear();
384
+ ```
385
+
386
+ ### Test context
387
+
388
+ `runInTestContext(id, fn)` carries a test's id through every await;
389
+ `installContextFetch()` stamps it onto every outgoing request as
390
+ `x-test-context-id`, so whatever serves a URL in-process finds the test the
391
+ request belongs to — including a request the code under test made while
392
+ handling another one.
393
+
394
+ ### `TransactionRegistry`
395
+
396
+ One transaction per database per test — an app's database and a schema tenant
397
+ each on their own connection, as deployed — opened the first time the test
398
+ touches it and rolled back by `rollbackAll()`. Code under test may use
399
+ transactions freely: a `BEGIN` becomes a savepoint, so nothing it does can end
400
+ the test's transaction.
401
+
402
+ ```typescript
403
+ const registry = new TransactionRegistry();
404
+ const db = await registry.get('Database', process.env.DATABASE_URL!);
405
+ // …
406
+ await registry.rollbackAll();
407
+ ```
408
+
324
409
  ## Database Migration
325
410
 
326
411
  TestKit includes utilities for managing test database migrations.
@@ -1 +1 @@
1
- {"version":3,"file":"KyselyFactory.d.cts","names":[],"sources":["../src/KyselyFactory.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA+Ca,cACZ,IACA,iBAAiB,qBACjB,cAAc,6BACL,QAAQ,UAAU;UAkClB;UACA;UACA;;;;;;;;;;;;;;;;;;;;;;SAdO,WAAW,aAAa,aAAa,QAAQ,OAAO;;;;;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AI,MACJ,OAAO,iBACH,QAAQ;;;;;;;;EAoDb,YACS,UAAU,UACV,OAAO,OACP,IAAI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAoCP,OAAO,gBAAgB,UAC5B,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+DjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QAAQ,WAAW,SAAS,SAC1B,QAAQ,QAAQ,WAAW,SAAS;EACjC,WAAW,gBAAgB,UAChC,eACA,aAAa,GACb,QAAQ,aAAa,OAAO,iBAAiB,WAAW,SAAS,SAC/D,QAAQ,QAAQ,WAAW,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAgFvC,KAAK,gBAAgB,OACpB,UAAU,GACV,QAAQ,iBAAiB,MAAM,MAC7B,WAAW,MAAM"}
@@ -1,4 +1,4 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.cjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
2
2
  import { Kysely } from "kysely";
3
3
  import { MigrationProvider } from "kysely/migration";
4
4
  //#region src/PostgresKyselyMigrator.d.ts
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresKyselyMigrator.d.cts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EAAA,YAAA;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
1
+ {"version":3,"file":"PostgresKyselyMigrator.d.cts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EADT,YACS;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
@@ -1,4 +1,4 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.mjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
2
2
  import { MigrationProvider } from "kysely/migration";
3
3
  import { Kysely } from "kysely";
4
4
  //#region src/PostgresKyselyMigrator.d.ts
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresKyselyMigrator.d.mts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EAAA,YAAA;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
1
+ {"version":3,"file":"PostgresKyselyMigrator.d.mts","names":[],"sources":["../src/PostgresKyselyMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBA2Da,+BAA+B;UAWlC;;;;;;;;;;EADT,YACS;IACP;IACA,IAAI;IACJ,UAAU;IACV,eAAe,gBAAgB;;;;;;;;;EAa3B,WAAW"}
@@ -71,9 +71,16 @@ declare abstract class PostgresMigrator {
71
71
  private static drop;
72
72
  /**
73
73
  * Starts the migration process by creating the database and running migrations.
74
- * Returns a cleanup function that will drop the database when called.
74
+ * Returns a cleanup function that drops the database — if this call created
75
+ * it.
75
76
  *
76
- * @returns Async cleanup function that drops the created database
77
+ * A database that was already there belongs to whoever made it, and the
78
+ * cleanup leaves it alone. `gkm test` provisions the test database before
79
+ * the suite starts, with an owner role that may migrate it but not drop it:
80
+ * dropping it was never the suite's to do, and failed there as a permission
81
+ * error on the way out of an otherwise green run.
82
+ *
83
+ * @returns Async cleanup function that drops the database if it created it
77
84
  *
78
85
  * @example
79
86
  * ```typescript
@@ -95,4 +102,4 @@ declare abstract class PostgresMigrator {
95
102
  }
96
103
  //#endregion
97
104
  export { PostgresMigrator as t };
98
- //# sourceMappingURL=PostgresMigrator-CoHQ95cT.d.cts.map
105
+ //# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresMigrator-CDPnnwMJ.d.cts","names":[],"sources":["../src/PostgresMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA4EsB;UAQZ;UACA;;;;;;;EAFT,YACS,aACA,gBAAe,gBAAgB;;;;;;;WAS/B,WAAW;;;;;;;;;iBAUC;;;;;;;;;iBA4CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCf,SAAK,cAAA"}
@@ -71,9 +71,16 @@ declare abstract class PostgresMigrator {
71
71
  private static drop;
72
72
  /**
73
73
  * Starts the migration process by creating the database and running migrations.
74
- * Returns a cleanup function that will drop the database when called.
74
+ * Returns a cleanup function that drops the database — if this call created
75
+ * it.
75
76
  *
76
- * @returns Async cleanup function that drops the created database
77
+ * A database that was already there belongs to whoever made it, and the
78
+ * cleanup leaves it alone. `gkm test` provisions the test database before
79
+ * the suite starts, with an owner role that may migrate it but not drop it:
80
+ * dropping it was never the suite's to do, and failed there as a permission
81
+ * error on the way out of an otherwise green run.
82
+ *
83
+ * @returns Async cleanup function that drops the database if it created it
77
84
  *
78
85
  * @example
79
86
  * ```typescript
@@ -95,4 +102,4 @@ declare abstract class PostgresMigrator {
95
102
  }
96
103
  //#endregion
97
104
  export { PostgresMigrator as t };
98
- //# sourceMappingURL=PostgresMigrator-CoHQ95cT.d.mts.map
105
+ //# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PostgresMigrator-CDPnnwMJ.d.mts","names":[],"sources":["../src/PostgresMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;uBA4EsB;UAQZ;UACA;;;;;;;EAFT,YACS,aACA,gBAAe,gBAAgB;;;;;;;WAS/B,WAAW;;;;;;;;;iBAUC;;;;;;;;;iBA4CA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCf,SAAK,cAAA"}
@@ -102,7 +102,7 @@ var PostgresMigrator = class PostgresMigrator {
102
102
  if (result.rowCount === 0) try {
103
103
  await db.query(`CREATE DATABASE "${database}"`);
104
104
  } catch (error) {
105
- if (error?.code === "42P04") return { alreadyExisted: true };
105
+ if (error?.code === "42P04" || error?.code === "23505" && error?.constraint === "pg_database_datname_index") return { alreadyExisted: true };
106
106
  throw error;
107
107
  }
108
108
  return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
@@ -129,9 +129,16 @@ var PostgresMigrator = class PostgresMigrator {
129
129
  }
130
130
  /**
131
131
  * Starts the migration process by creating the database and running migrations.
132
- * Returns a cleanup function that will drop the database when called.
132
+ * Returns a cleanup function that drops the database — if this call created
133
+ * it.
133
134
  *
134
- * @returns Async cleanup function that drops the created database
135
+ * A database that was already there belongs to whoever made it, and the
136
+ * cleanup leaves it alone. `gkm test` provisions the test database before
137
+ * the suite starts, with an owner role that may migrate it but not drop it:
138
+ * dropping it was never the suite's to do, and failed there as a permission
139
+ * error on the way out of an otherwise green run.
140
+ *
141
+ * @returns Async cleanup function that drops the database if it created it
135
142
  *
136
143
  * @example
137
144
  * ```typescript
@@ -151,8 +158,10 @@ var PostgresMigrator = class PostgresMigrator {
151
158
  */
152
159
  async start() {
153
160
  const { database, db } = await setupClient(this.uri);
161
+ let created = false;
154
162
  try {
155
- await PostgresMigrator.create(this.uri);
163
+ const { alreadyExisted } = await PostgresMigrator.create(this.uri);
164
+ created = !alreadyExisted;
156
165
  if (this.afterCreate) await this.afterCreate(this.uri);
157
166
  await this.migrate();
158
167
  logger.log(`Migrating database: ${database}`);
@@ -160,7 +169,7 @@ var PostgresMigrator = class PostgresMigrator {
160
169
  await db.end();
161
170
  }
162
171
  return async () => {
163
- await PostgresMigrator.drop(this.uri);
172
+ if (created) await PostgresMigrator.drop(this.uri);
164
173
  };
165
174
  }
166
175
  };
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresMigrator.cjs","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // With afterCreate hook (e.g. run init script to create per-app users)\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new MyMigrator(\n * 'postgresql://localhost:5432/test_db',\n * async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * );\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @param afterCreate - Optional hook called after database creation but before migrations\n\t */\n\tconstructor(\n\t\tprivate uri: string,\n\t\tprivate afterCreate?: (uri: string) => Promise<void>,\n\t) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// 42P04 = duplicate_database — another process created it between our check and create\n\t\t\t\t\tif (error?.code === '42P04') {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that will drop the database when called.\n\t *\n\t * @returns Async cleanup function that drops the created database\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\ttry {\n\t\t\tawait PostgresMigrator.create(this.uri);\n\t\t\tif (this.afterCreate) {\n\t\t\t\tawait this.afterCreate(this.uri);\n\t\t\t}\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tawait PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;;;;AAEA,MAAM,EAAE,WAAW,GAAA;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAY,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI,GAAG;CAEvB,MAAM,KAAK,IAAI,OAAO;EACrB,MAAM,IAAI;EACV,UAAU,IAAI;EACd,MAAM,IAAI;EACV,MAAM,SAAS,IAAI,MAAM,EAAE;EAC3B,UAAU;CACX,CAAC;CAED,IAAI,WAAW,IAAI,SAAS,MAAM,CAAC;CACnC,IAAI,SAAS,SAAS,GAAG,GACxB,WAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,GAAG,CAAC;CAEvD,OAAO;EAAE;EAAU;CAAG;AACvB;;;;AAKA,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file":"PostgresMigrator.cjs","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // With afterCreate hook (e.g. run init script to create per-app users)\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new MyMigrator(\n * 'postgresql://localhost:5432/test_db',\n * async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * );\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @param afterCreate - Optional hook called after database creation but before migrations\n\t */\n\tconstructor(\n\t\tprivate uri: string,\n\t\tprivate afterCreate?: (uri: string) => Promise<void>,\n\t) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// Another process created it between our check and our create. Postgres\n\t\t\t\t\t// reports that as 42P04 (duplicate_database) — or, when the two creates\n\t\t\t\t\t// race inside the catalog itself, as 23505 on pg_database's name index.\n\t\t\t\t\tif (\n\t\t\t\t\t\terror?.code === '42P04' ||\n\t\t\t\t\t\t(error?.code === '23505' &&\n\t\t\t\t\t\t\terror?.constraint === 'pg_database_datname_index')\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that drops the database — if this call created\n\t * it.\n\t *\n\t * A database that was already there belongs to whoever made it, and the\n\t * cleanup leaves it alone. `gkm test` provisions the test database before\n\t * the suite starts, with an owner role that may migrate it but not drop it:\n\t * dropping it was never the suite's to do, and failed there as a permission\n\t * error on the way out of an otherwise green run.\n\t *\n\t * @returns Async cleanup function that drops the database if it created it\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\tlet created = false;\n\t\ttry {\n\t\t\tconst { alreadyExisted } = await PostgresMigrator.create(this.uri);\n\t\t\tcreated = !alreadyExisted;\n\t\t\tif (this.afterCreate) {\n\t\t\t\tawait this.afterCreate(this.uri);\n\t\t\t}\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tif (created) await PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;;;;AAEA,MAAM,EAAE,WAAW,GAAA;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAY,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI,GAAG;CAEvB,MAAM,KAAK,IAAI,OAAO;EACrB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AU,OAAO,MAAM,YAAY,KAAK,GAAG;EACnD,IAAI,UAAU;EACd,IAAI;GACH,MAAM,EAAE,mBAAmB,MAAM,iBAAiB,OAAO,KAAK,GAAG;GACjE,UAAU,CAAC;GACX,IAAI,KAAK,aACR,MAAM,KAAK,YAAY,KAAK,GAAG;GAEhC,MAAM,KAAK,QAAQ;GACnB,OAAO,IAAI,uBAAuB,UAAU;EAE7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;EAEA,OAAO,YAAY;GAClB,IAAI,SAAS,MAAM,iBAAiB,KAAK,KAAK,GAAG;EAClD;CACD;AACD"}
@@ -1,2 +1,2 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.cjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
2
2
  export { PostgresMigrator };
@@ -1,2 +1,2 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.mjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
2
2
  export { PostgresMigrator };
@@ -99,7 +99,7 @@ var PostgresMigrator = class PostgresMigrator {
99
99
  if (result.rowCount === 0) try {
100
100
  await db.query(`CREATE DATABASE "${database}"`);
101
101
  } catch (error) {
102
- if (error?.code === "42P04") return { alreadyExisted: true };
102
+ if (error?.code === "42P04" || error?.code === "23505" && error?.constraint === "pg_database_datname_index") return { alreadyExisted: true };
103
103
  throw error;
104
104
  }
105
105
  return { alreadyExisted: result.rowCount ? result.rowCount > 0 : false };
@@ -126,9 +126,16 @@ var PostgresMigrator = class PostgresMigrator {
126
126
  }
127
127
  /**
128
128
  * Starts the migration process by creating the database and running migrations.
129
- * Returns a cleanup function that will drop the database when called.
129
+ * Returns a cleanup function that drops the database — if this call created
130
+ * it.
130
131
  *
131
- * @returns Async cleanup function that drops the created database
132
+ * A database that was already there belongs to whoever made it, and the
133
+ * cleanup leaves it alone. `gkm test` provisions the test database before
134
+ * the suite starts, with an owner role that may migrate it but not drop it:
135
+ * dropping it was never the suite's to do, and failed there as a permission
136
+ * error on the way out of an otherwise green run.
137
+ *
138
+ * @returns Async cleanup function that drops the database if it created it
132
139
  *
133
140
  * @example
134
141
  * ```typescript
@@ -148,8 +155,10 @@ var PostgresMigrator = class PostgresMigrator {
148
155
  */
149
156
  async start() {
150
157
  const { database, db } = await setupClient(this.uri);
158
+ let created = false;
151
159
  try {
152
- await PostgresMigrator.create(this.uri);
160
+ const { alreadyExisted } = await PostgresMigrator.create(this.uri);
161
+ created = !alreadyExisted;
153
162
  if (this.afterCreate) await this.afterCreate(this.uri);
154
163
  await this.migrate();
155
164
  logger.log(`Migrating database: ${database}`);
@@ -157,7 +166,7 @@ var PostgresMigrator = class PostgresMigrator {
157
166
  await db.end();
158
167
  }
159
168
  return async () => {
160
- await PostgresMigrator.drop(this.uri);
169
+ if (created) await PostgresMigrator.drop(this.uri);
161
170
  };
162
171
  }
163
172
  };
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresMigrator.mjs","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // With afterCreate hook (e.g. run init script to create per-app users)\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new MyMigrator(\n * 'postgresql://localhost:5432/test_db',\n * async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * );\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @param afterCreate - Optional hook called after database creation but before migrations\n\t */\n\tconstructor(\n\t\tprivate uri: string,\n\t\tprivate afterCreate?: (uri: string) => Promise<void>,\n\t) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// 42P04 = duplicate_database — another process created it between our check and create\n\t\t\t\t\tif (error?.code === '42P04') {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that will drop the database when called.\n\t *\n\t * @returns Async cleanup function that drops the created database\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\ttry {\n\t\t\tawait PostgresMigrator.create(this.uri);\n\t\t\tif (this.afterCreate) {\n\t\t\t\tawait this.afterCreate(this.uri);\n\t\t\t}\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tawait PostgresMigrator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AAM,kBAAkB,SAAS,EAAE;EAC7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;CACD;;;;;;;;;;;;;;;;;;;;;;;CAwBA,MAAM,QAAQ;EACb,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,KAAK,GAAG;EACnD,IAAI;GACH,MAAM,iBAAiB,OAAO,KAAK,GAAG;GACtC,IAAI,KAAK,aACR,MAAM,KAAK,YAAY,KAAK,GAAG;GAEhC,MAAM,KAAK,QAAQ;GACnB,OAAO,IAAI,uBAAuB,UAAU;EAE7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;EAEA,OAAO,YAAY;GAClB,MAAM,iBAAiB,KAAK,KAAK,GAAG;EACrC;CACD;AACD"}
1
+ {"version":3,"file":"PostgresMigrator.mjs","names":[],"sources":["../src/PostgresMigrator.ts"],"sourcesContent":["import pg from 'pg';\n\nconst { Client } = pg;\n\n/**\n * Creates a PostgreSQL client connected to the 'postgres' database.\n * Extracts connection details from the provided URI.\n *\n * @param uri - PostgreSQL connection URI\n * @returns Object containing the target database name and client instance\n *\n * @example\n * ```typescript\n * const { database, db } = await setupClient('postgresql://user:pass@localhost:5432/mydb');\n * // database = 'mydb'\n * // db = Client instance connected to 'postgres' database\n * ```\n */\nasync function setupClient(uri: string) {\n\tconst url = new URL(uri);\n\n\tconst db = new Client({\n\t\tuser: url.username,\n\t\tpassword: url.password,\n\t\thost: url.hostname,\n\t\tport: parseInt(url.port, 10),\n\t\tdatabase: 'postgres',\n\t});\n\n\tlet database = url.pathname.slice(1);\n\tif (database.includes('?')) {\n\t\tdatabase = database.substring(0, database.indexOf('?'));\n\t}\n\treturn { database, db };\n}\n\n/**\n * Default logger instance for migration operations.\n */\nconst logger = console;\n\n/**\n * Abstract base class for PostgreSQL database migration utilities.\n * Provides database creation, migration, and cleanup functionality for testing.\n * Subclasses must implement the migrate() method to define migration logic.\n *\n * @example\n * ```typescript\n * class MyMigrator extends PostgresMigrator {\n * async migrate(): Promise<void> {\n * // Run your migrations here\n * await this.runMigrations();\n * }\n * }\n *\n * // Use in tests\n * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n * const cleanup = await migrator.start();\n *\n * // With afterCreate hook (e.g. run init script to create per-app users)\n * import { runInitScript } from '@geekmidas/testkit/postgres';\n *\n * const migrator = new MyMigrator(\n * 'postgresql://localhost:5432/test_db',\n * async (uri) => {\n * await runInitScript('docker/postgres/init.sh', uri, env);\n * },\n * );\n * const cleanup = await migrator.start();\n *\n * // Run tests...\n *\n * // Clean up\n * await cleanup();\n * ```\n */\nexport abstract class PostgresMigrator {\n\t/**\n\t * Creates a new PostgresMigrator instance.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @param afterCreate - Optional hook called after database creation but before migrations\n\t */\n\tconstructor(\n\t\tprivate uri: string,\n\t\tprivate afterCreate?: (uri: string) => Promise<void>,\n\t) {}\n\n\t/**\n\t * Abstract method to be implemented by subclasses.\n\t * Should contain the migration logic for setting up database schema.\n\t *\n\t * @returns Promise that resolves when migrations are complete\n\t */\n\tabstract migrate(): Promise<void>;\n\n\t/**\n\t * Creates a PostgreSQL database if it doesn't already exist.\n\t * Connects to the 'postgres' database to check and create the target database.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @returns Object indicating whether the database already existed\n\t * @private\n\t */\n\tprivate static async create(\n\t\turi: string,\n\t): Promise<{ alreadyExisted: boolean }> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tconst result = await db.query(\n\t\t\t\t`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,\n\t\t\t);\n\n\t\t\tif (result.rowCount === 0) {\n\t\t\t\ttry {\n\t\t\t\t\tawait db.query(`CREATE DATABASE \"${database}\"`);\n\t\t\t\t} catch (error: any) {\n\t\t\t\t\t// Another process created it between our check and our create. Postgres\n\t\t\t\t\t// reports that as 42P04 (duplicate_database) — or, when the two creates\n\t\t\t\t\t// race inside the catalog itself, as 23505 on pg_database's name index.\n\t\t\t\t\tif (\n\t\t\t\t\t\terror?.code === '42P04' ||\n\t\t\t\t\t\t(error?.code === '23505' &&\n\t\t\t\t\t\t\terror?.constraint === 'pg_database_datname_index')\n\t\t\t\t\t) {\n\t\t\t\t\t\treturn { alreadyExisted: true };\n\t\t\t\t\t}\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\talreadyExisted: result.rowCount ? result.rowCount > 0 : false,\n\t\t\t};\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Drops a PostgreSQL database.\n\t * Used for cleanup after tests are complete.\n\t *\n\t * @param uri - PostgreSQL connection URI\n\t * @throws Error if database cannot be dropped\n\t * @private\n\t */\n\tprivate static async drop(uri: string): Promise<void> {\n\t\tconst { database, db } = await setupClient(uri);\n\t\ttry {\n\t\t\tawait db.connect();\n\t\t\tawait db.query(`DROP DATABASE \"${database}\"`);\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\t}\n\n\t/**\n\t * Starts the migration process by creating the database and running migrations.\n\t * Returns a cleanup function that drops the database — if this call created\n\t * it.\n\t *\n\t * A database that was already there belongs to whoever made it, and the\n\t * cleanup leaves it alone. `gkm test` provisions the test database before\n\t * the suite starts, with an owner role that may migrate it but not drop it:\n\t * dropping it was never the suite's to do, and failed there as a permission\n\t * error on the way out of an otherwise green run.\n\t *\n\t * @returns Async cleanup function that drops the database if it created it\n\t *\n\t * @example\n\t * ```typescript\n\t * const migrator = new MyMigrator('postgresql://localhost:5432/test_db');\n\t *\n\t * // Start migrations and get cleanup function\n\t * const cleanup = await migrator.start();\n\t *\n\t * try {\n\t * // Run your tests here\n\t * await runTests();\n\t * } finally {\n\t * // Always clean up\n\t * await cleanup();\n\t * }\n\t * ```\n\t */\n\tasync start() {\n\t\tconst { database, db } = await setupClient(this.uri);\n\t\tlet created = false;\n\t\ttry {\n\t\t\tconst { alreadyExisted } = await PostgresMigrator.create(this.uri);\n\t\t\tcreated = !alreadyExisted;\n\t\t\tif (this.afterCreate) {\n\t\t\t\tawait this.afterCreate(this.uri);\n\t\t\t}\n\t\t\tawait this.migrate();\n\t\t\tlogger.log(`Migrating database: ${database}`);\n\t\t\t// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');\n\t\t} finally {\n\t\t\tawait db.end();\n\t\t}\n\n\t\treturn async () => {\n\t\t\tif (created) await PostgresMigrator.drop(this.uri);\n\t\t};\n\t}\n}\n"],"mappings":";;AAEA,MAAM,EAAE,WAAW;;;;;;;;;;;;;;;AAgBnB,eAAe,YAAY,KAAa;CACvC,MAAM,MAAM,IAAI,IAAI,GAAG;CAEvB,MAAM,KAAK,IAAI,OAAO;EACrB,MAAM,IAAI;EACV,UAAU,IAAI;EACd,MAAM,IAAI;EACV,MAAM,SAAS,IAAI,MAAM,EAAE;EAC3B,UAAU;CACX,CAAC;CAED,IAAI,WAAW,IAAI,SAAS,MAAM,CAAC;CACnC,IAAI,SAAS,SAAS,GAAG,GACxB,WAAW,SAAS,UAAU,GAAG,SAAS,QAAQ,GAAG,CAAC;CAEvD,OAAO;EAAE;EAAU;CAAG;AACvB;;;;AAKA,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCf,IAAsB,mBAAtB,MAAsB,iBAAiB;CAQ7B;CACA;;;;;;;CAFT,YACC,KACA,aACC;EAFO,KAAA,MAAA;EACA,KAAA,cAAA;CACN;;;;;;;;;CAkBH,aAAqB,OACpB,KACuC;EACvC,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,GAAG;EAC9C,IAAI;GACH,MAAM,GAAG,QAAQ;GACjB,MAAM,SAAS,MAAM,GAAG,MACvB,yDAAyD,SAAS,EACnE;GAEA,IAAI,OAAO,aAAa,GACvB,IAAI;IACH,MAAM,GAAG,MAAM,oBAAoB,SAAS,EAAE;GAC/C,SAAS,OAAY;IAIpB,IACC,OAAO,SAAS,WACf,OAAO,SAAS,WAChB,OAAO,eAAe,6BAEvB,OAAO,EAAE,gBAAgB,KAAK;IAE/B,MAAM;GACP;GAGD,OAAO,EACN,gBAAgB,OAAO,WAAW,OAAO,WAAW,IAAI,MACzD;EACD,UAAU;GACT,MAAM,GAAG,IAAI;EACd;CACD;;;;;;;;;CAUA,aAAqB,KAAK,KAA4B;EACrD,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,GAAG;EAC9C,IAAI;GACH,MAAM,GAAG,QAAQ;GACjB,MAAM,GAAG,MAAM,kBAAkB,SAAS,EAAE;EAC7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;CACD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+BA,MAAM,QAAQ;EACb,MAAM,EAAE,UAAU,OAAO,MAAM,YAAY,KAAK,GAAG;EACnD,IAAI,UAAU;EACd,IAAI;GACH,MAAM,EAAE,mBAAmB,MAAM,iBAAiB,OAAO,KAAK,GAAG;GACjE,UAAU,CAAC;GACX,IAAI,KAAK,aACR,MAAM,KAAK,YAAY,KAAK,GAAG;GAEhC,MAAM,KAAK,QAAQ;GACnB,OAAO,IAAI,uBAAuB,UAAU;EAE7C,UAAU;GACT,MAAM,GAAG,IAAI;EACd;EAEA,OAAO,YAAY;GAClB,IAAI,SAAS,MAAM,iBAAiB,KAAK,KAAK,GAAG;EAClD;CACD;AACD"}
@@ -1,4 +1,4 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.cjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
2
2
  import { Knex } from "knex";
3
3
  //#region src/PostgresObjectionMigrator.d.ts
4
4
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresObjectionMigrator.d.cts","names":[],"sources":["../src/PostgresObjectionMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuCa,kCAAkC;UASrC;;;;;;;;EAAA,YAAA;IACP;IACA,MAAM;;;;;;;;;EAaF,WAAW;;;;;;;EA8BX,YAAY;;;;;;;EA4BZ,UAAU;IACf;IACA"}
1
+ {"version":3,"file":"PostgresObjectionMigrator.d.cts","names":[],"sources":["../src/PostgresObjectionMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuCa,kCAAkC;UASrC;;;;;;;;EADT,YACS;IACP;IACA,MAAM;;;;;;;;;EAaF,WAAW;;;;;;;EA8BX,YAAY;;;;;;;EA4BZ,UAAU;IACf;IACA"}
@@ -1,4 +1,4 @@
1
- import { t as PostgresMigrator } from "./PostgresMigrator-CoHQ95cT.mjs";
1
+ import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
2
2
  import { Knex } from "knex";
3
3
  //#region src/PostgresObjectionMigrator.d.ts
4
4
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"PostgresObjectionMigrator.d.mts","names":[],"sources":["../src/PostgresObjectionMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuCa,kCAAkC;UASrC;;;;;;;;EAAA,YAAA;IACP;IACA,MAAM;;;;;;;;;EAaF,WAAW;;;;;;;EA8BX,YAAY;;;;;;;EA4BZ,UAAU;IACf;IACA"}
1
+ {"version":3,"file":"PostgresObjectionMigrator.d.mts","names":[],"sources":["../src/PostgresObjectionMigrator.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;qBAuCa,kCAAkC;UASrC;;;;;;;;EADT,YACS;IACP;IACA,MAAM;;;;;;;;;EAaF,WAAW;;;;;;;EA8BX,YAAY;;;;;;;EA4BZ,UAAU;IACf;IACA"}
@@ -1 +1 @@
1
- {"version":3,"file":"VitestTransactionIsolator.d.cts","names":[],"sources":["../src/VitestTransactionIsolator.ts"],"mappings":";;;;;;;;;;iBAWiB,iBAAiB,aAAa;;;;;EAK9C,KAAK;;;;;YAMM,yBACX,aACA,qBACG,iBAAiB,eAAe;;;;;;;;;;;;;;YAexB,gBAAgB,aAAa,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