@geekmidas/testkit 10.0.0-alpha.17 → 10.0.0-alpha.18
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.
- package/dist/PostgresKyselyMigrator.d.cts +1 -1
- package/dist/PostgresKyselyMigrator.d.mts +1 -1
- package/dist/{PostgresMigrator-CoHQ95cT.d.cts → PostgresMigrator-CDPnnwMJ.d.cts} +10 -3
- package/dist/{PostgresMigrator-CoHQ95cT.d.cts.map → PostgresMigrator-CDPnnwMJ.d.cts.map} +1 -1
- package/dist/{PostgresMigrator-CoHQ95cT.d.mts → PostgresMigrator-CDPnnwMJ.d.mts} +10 -3
- package/dist/{PostgresMigrator-CoHQ95cT.d.mts.map → PostgresMigrator-CDPnnwMJ.d.mts.map} +1 -1
- package/dist/PostgresMigrator.cjs +13 -4
- package/dist/PostgresMigrator.cjs.map +1 -1
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +13 -4
- package/dist/PostgresMigrator.mjs.map +1 -1
- package/dist/PostgresObjectionMigrator.d.cts +1 -1
- package/dist/PostgresObjectionMigrator.d.mts +1 -1
- package/package.json +7 -7
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as PostgresMigrator } from "./PostgresMigrator-
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import { t as PostgresMigrator } from "./PostgresMigrator-
|
|
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
|
|
@@ -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
|
|
74
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
75
|
+
* it.
|
|
75
76
|
*
|
|
76
|
-
*
|
|
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-
|
|
105
|
+
//# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostgresMigrator-
|
|
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;;;;;;;;;iBAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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
|
|
74
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
75
|
+
* it.
|
|
75
76
|
*
|
|
76
|
-
*
|
|
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-
|
|
105
|
+
//# sourceMappingURL=PostgresMigrator-CDPnnwMJ.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PostgresMigrator-
|
|
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;;;;;;;;;iBAsCA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAuCf,SAAK,cAAA"}
|
|
@@ -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
|
|
132
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
133
|
+
* it.
|
|
133
134
|
*
|
|
134
|
-
*
|
|
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
|
|
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 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,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;IAEpB,IAAI,OAAO,SAAS,SACnB,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,2 +1,2 @@
|
|
|
1
|
-
import { t as PostgresMigrator } from "./PostgresMigrator-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.cjs";
|
|
2
2
|
export { PostgresMigrator };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as PostgresMigrator } from "./PostgresMigrator-
|
|
1
|
+
import { t as PostgresMigrator } from "./PostgresMigrator-CDPnnwMJ.mjs";
|
|
2
2
|
export { PostgresMigrator };
|
|
@@ -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
|
|
129
|
+
* Returns a cleanup function that drops the database — if this call created
|
|
130
|
+
* it.
|
|
130
131
|
*
|
|
131
|
-
*
|
|
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
|
|
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 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;IAEpB,IAAI,OAAO,SAAS,SACnB,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@geekmidas/testkit",
|
|
3
|
-
"version": "10.0.0-alpha.
|
|
3
|
+
"version": "10.0.0-alpha.18",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -120,17 +120,17 @@
|
|
|
120
120
|
],
|
|
121
121
|
"dependencies": {
|
|
122
122
|
"@faker-js/faker": "~10.6.0",
|
|
123
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
124
|
-
"@geekmidas/
|
|
125
|
-
"@geekmidas/
|
|
123
|
+
"@geekmidas/envkit": "^10.0.0-alpha.18",
|
|
124
|
+
"@geekmidas/services": "^10.0.0-alpha.18",
|
|
125
|
+
"@geekmidas/logger": "^10.0.0-alpha.18"
|
|
126
126
|
},
|
|
127
127
|
"devDependencies": {
|
|
128
128
|
"@better-auth/test-utils": "~1.7.6",
|
|
129
129
|
"@types/pg": "~8.23.1",
|
|
130
130
|
"better-auth": "~1.7.6",
|
|
131
|
-
"@geekmidas/envkit": "^10.0.0-alpha.
|
|
132
|
-
"@geekmidas/logger": "^10.0.0-alpha.
|
|
133
|
-
"@geekmidas/services": "^10.0.0-alpha.
|
|
131
|
+
"@geekmidas/envkit": "^10.0.0-alpha.18",
|
|
132
|
+
"@geekmidas/logger": "^10.0.0-alpha.18",
|
|
133
|
+
"@geekmidas/services": "^10.0.0-alpha.18"
|
|
134
134
|
},
|
|
135
135
|
"repository": {
|
|
136
136
|
"type": "git",
|