@geekmidas/testkit 0.4.0 → 1.0.0
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/CHANGELOG.md +13 -0
- package/dist/Factory-BFVnMMCC.mjs.map +1 -1
- package/dist/{Factory-c16c27Y6.d.cts → Factory-BOX312yd.d.cts} +3 -2
- package/dist/Factory-BOX312yd.d.cts.map +1 -0
- package/dist/Factory-BhjUOBWN.cjs.map +1 -1
- package/dist/{Factory-BcGJjLc8.d.mts → Factory-SFupxRC2.d.mts} +2 -1
- package/dist/Factory-SFupxRC2.d.mts.map +1 -0
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +1 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +1 -1
- package/dist/KyselyFactory-DMswpwji.mjs.map +1 -1
- package/dist/{KyselyFactory-uZ45h7YU.d.cts → KyselyFactory-Dy5zzV4B.d.cts} +4 -3
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +1 -0
- package/dist/{KyselyFactory-Cj-EultY.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -2
- package/dist/KyselyFactory-vAxYodck.d.mts.map +1 -0
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +2 -2
- package/dist/{ObjectionFactory-DL4qkuF1.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -2
- package/dist/ObjectionFactory-BWjB49-i.d.mts.map +1 -0
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +1 -1
- package/dist/{ObjectionFactory-CdhzKs4f.d.cts → ObjectionFactory-CD-WFuMJ.d.cts} +4 -3
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +1 -0
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +2 -2
- package/dist/{PostgresKyselyMigrator-upT-hmrz.mjs → PostgresKyselyMigrator-6sE1KOni.mjs} +2 -2
- package/dist/PostgresKyselyMigrator-6sE1KOni.mjs.map +1 -0
- package/dist/{PostgresKyselyMigrator-CIx3AFSR.d.mts → PostgresKyselyMigrator-CBltSOq5.d.cts} +3 -2
- package/dist/PostgresKyselyMigrator-CBltSOq5.d.cts.map +1 -0
- package/dist/{PostgresKyselyMigrator-CfytARcA.cjs → PostgresKyselyMigrator-D6IbPq8t.cjs} +2 -2
- package/dist/PostgresKyselyMigrator-D6IbPq8t.cjs.map +1 -0
- package/dist/{PostgresKyselyMigrator-CQ3aUoy_.d.cts → PostgresKyselyMigrator-DrVWncqd.d.mts} +3 -2
- package/dist/PostgresKyselyMigrator-DrVWncqd.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.d.cts +2 -2
- package/dist/PostgresKyselyMigrator.d.mts +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-DbuJGAVy.mjs → PostgresMigrator-BjjenqSd.mjs} +2 -2
- package/dist/PostgresMigrator-BjjenqSd.mjs.map +1 -0
- package/dist/{PostgresMigrator-D5UkK1_K.d.cts → PostgresMigrator-Bres0U6E.d.cts} +2 -1
- package/dist/PostgresMigrator-Bres0U6E.d.cts.map +1 -0
- package/dist/{PostgresMigrator-DFcNdCvD.cjs → PostgresMigrator-D6dQn0x2.cjs} +2 -2
- package/dist/PostgresMigrator-D6dQn0x2.cjs.map +1 -0
- package/dist/{PostgresMigrator-DQaRxoaY.d.mts → PostgresMigrator-S-YYosAC.d.mts} +2 -1
- package/dist/PostgresMigrator-S-YYosAC.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{PostgresObjectionMigrator-CZHHcCOv.d.cts → PostgresObjectionMigrator-CPfBAP7r.d.cts} +3 -2
- package/dist/PostgresObjectionMigrator-CPfBAP7r.d.cts.map +1 -0
- package/dist/{PostgresObjectionMigrator-BG6ymgnt.cjs → PostgresObjectionMigrator-DK8ODIHQ.cjs} +2 -2
- package/dist/PostgresObjectionMigrator-DK8ODIHQ.cjs.map +1 -0
- package/dist/{PostgresObjectionMigrator-D_hCcrQu.d.mts → PostgresObjectionMigrator-DVEqB5tp.d.mts} +3 -2
- package/dist/PostgresObjectionMigrator-DVEqB5tp.d.mts.map +1 -0
- package/dist/{PostgresObjectionMigrator-DPj2pOpX.mjs → PostgresObjectionMigrator-D_QxXbIN.mjs} +2 -2
- package/dist/PostgresObjectionMigrator-D_QxXbIN.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +2 -2
- package/dist/PostgresObjectionMigrator.d.cts +2 -2
- package/dist/PostgresObjectionMigrator.d.mts +2 -2
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/{VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts → VitestKyselyTransactionIsolator-CduJlHoT.d.cts} +4 -3
- package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts → VitestKyselyTransactionIsolator-Cswnnj0k.d.mts} +4 -3
- package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-EvDLk5zg.cjs → VitestKyselyTransactionIsolator-D7RRXOBa.cjs} +2 -2
- package/dist/VitestKyselyTransactionIsolator-D7RRXOBa.cjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-CNURW8y6.mjs → VitestKyselyTransactionIsolator-DceyIqr4.mjs} +2 -2
- package/dist/VitestKyselyTransactionIsolator-DceyIqr4.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +1 -1
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +1 -1
- package/dist/{VitestObjectionTransactionIsolator-1TpsPqfG.d.cts → VitestObjectionTransactionIsolator-BXoR6xdG.d.cts} +4 -3
- package/dist/VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-CM5KTAFA.cjs → VitestObjectionTransactionIsolator-CdLRrzNf.cjs} +2 -2
- package/dist/VitestObjectionTransactionIsolator-CdLRrzNf.cjs.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-jQFaCz0u.mjs → VitestObjectionTransactionIsolator-OF2osYY5.mjs} +2 -2
- package/dist/VitestObjectionTransactionIsolator-OF2osYY5.mjs.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts → VitestObjectionTransactionIsolator-x6hY5j4u.d.mts} +4 -3
- package/dist/VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +1 -1
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +1 -1
- package/dist/{VitestTransactionIsolator-BvR19bYn.d.mts → VitestTransactionIsolator-BNWJqh9f.d.mts} +3 -2
- package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -1
- package/dist/{VitestTransactionIsolator-CwQaxZLP.d.cts → VitestTransactionIsolator-CSroc7Df.d.cts} +3 -2
- package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -1
- package/dist/VitestTransactionIsolator.d.cts +1 -1
- package/dist/VitestTransactionIsolator.d.mts +1 -1
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +2 -0
- package/dist/aws.d.cts.map +1 -0
- package/dist/aws.d.mts +2 -0
- package/dist/aws.d.mts.map +1 -0
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +1 -0
- package/dist/benchmark.d.cts.map +1 -0
- package/dist/benchmark.d.mts +1 -0
- package/dist/benchmark.d.mts.map +1 -0
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +29 -30
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.cts.map +1 -0
- package/dist/better-auth.d.mts.map +1 -0
- package/dist/better-auth.mjs +29 -30
- package/dist/better-auth.mjs.map +1 -1
- package/dist/directory-B-Ozljzk.mjs.map +1 -1
- package/dist/directory-BVC8g7cX.cjs.map +1 -1
- package/dist/{directory-BXavAeJZ.d.mts → directory-CVrfTq1I.d.mts} +2 -1
- package/dist/directory-CVrfTq1I.d.mts.map +1 -0
- package/dist/{directory-Mi7tdOuD.d.cts → directory-DAnMWi50.d.cts} +2 -1
- package/dist/directory-DAnMWi50.d.cts.map +1 -0
- package/dist/faker-B14IEMIN.cjs.map +1 -1
- package/dist/faker-BGKYFoCT.mjs.map +1 -1
- package/dist/{faker-DvxiCtxc.d.cts → faker-Cg76aFNO.d.cts} +3 -3
- package/dist/faker-Cg76aFNO.d.cts.map +1 -0
- package/dist/faker-DHh7xs4u.d.mts.map +1 -0
- package/dist/faker.d.cts +1 -1
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +1 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs.map +1 -1
- package/dist/kysely.cjs +3 -3
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +8 -7
- package/dist/kysely.d.cts.map +1 -0
- package/dist/kysely.d.mts +7 -6
- package/dist/kysely.d.mts.map +1 -0
- package/dist/kysely.mjs +3 -3
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +1 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs.map +1 -1
- package/dist/objection.cjs +3 -3
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +8 -7
- package/dist/objection.d.cts.map +1 -0
- package/dist/objection.d.mts +7 -6
- package/dist/objection.d.mts.map +1 -0
- package/dist/objection.mjs +3 -3
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +2 -0
- package/dist/timer.d.cts.map +1 -0
- package/dist/timer.d.mts +2 -0
- package/dist/timer.d.mts.map +1 -0
- package/dist/timer.mjs.map +1 -1
- package/package.json +5 -5
- package/src/Factory.ts +72 -72
- package/src/KyselyFactory.ts +330 -330
- package/src/ObjectionFactory.ts +354 -355
- package/src/PostgresKyselyMigrator.ts +37 -37
- package/src/PostgresMigrator.ts +107 -107
- package/src/PostgresObjectionMigrator.ts +91 -91
- package/src/VitestKyselyTransactionIsolator.ts +27 -27
- package/src/VitestObjectionTransactionIsolator.ts +39 -39
- package/src/VitestTransactionIsolator.ts +196 -195
- package/src/__tests__/Factory.spec.ts +163 -155
- package/src/__tests__/KyselyFactory.spec.ts +443 -439
- package/src/__tests__/ObjectionFactory.spec.ts +563 -557
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +641 -641
- package/src/__tests__/PostgresMigrator.spec.ts +341 -341
- package/src/__tests__/PostgresObjectionMigrator.spec.ts +578 -578
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +114 -114
- package/src/__tests__/benchmark.spec.ts +140 -0
- package/src/__tests__/better-auth.spec.ts +15 -15
- package/src/__tests__/faker.spec.ts +226 -137
- package/src/__tests__/integration.spec.ts +597 -597
- package/src/__tests__/utilities.spec.ts +211 -0
- package/src/aws.ts +104 -104
- package/src/benchmark.ts +12 -12
- package/src/better-auth.ts +286 -301
- package/src/faker.ts +153 -153
- package/src/helpers.ts +6 -6
- package/src/kysely.ts +33 -33
- package/src/logger.ts +10 -10
- package/src/objection.ts +31 -31
- package/src/os/directory.ts +11 -10
- package/src/timer.ts +1 -1
- package/test/globalSetup.ts +45 -45
- package/test/helpers.ts +189 -189
- package/test/migrations/1749664623372_user.ts +13 -13
- package/tsconfig.json +9 -0
- package/vitest.config.ts +4 -4
- package/dist/PostgresKyselyMigrator-CfytARcA.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-upT-hmrz.mjs.map +0 -1
- package/dist/PostgresMigrator-DFcNdCvD.cjs.map +0 -1
- package/dist/PostgresMigrator-DbuJGAVy.mjs.map +0 -1
- package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DPj2pOpX.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CNURW8y6.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map +0 -1
|
@@ -43,45 +43,45 @@ const logger = console;
|
|
|
43
43
|
* ```
|
|
44
44
|
*/
|
|
45
45
|
export class PostgresKyselyMigrator extends PostgresMigrator {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Creates a new PostgresKyselyMigrator instance.
|
|
48
|
+
*
|
|
49
|
+
* @param options - Configuration options
|
|
50
|
+
* @param options.uri - PostgreSQL connection URI
|
|
51
|
+
* @param options.db - Kysely database instance
|
|
52
|
+
* @param options.provider - Migration provider for locating migration files
|
|
53
|
+
*/
|
|
54
|
+
constructor(
|
|
55
|
+
private options: {
|
|
56
|
+
uri: string;
|
|
57
|
+
db: Kysely<any>;
|
|
58
|
+
provider: MigrationProvider;
|
|
59
|
+
},
|
|
60
|
+
) {
|
|
61
|
+
super(options.uri);
|
|
62
|
+
}
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Executes Kysely migrations to the latest version.
|
|
66
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
67
|
+
*
|
|
68
|
+
* @throws Error if migrations fail to apply
|
|
69
|
+
* @returns Promise that resolves when all migrations are applied
|
|
70
|
+
*/
|
|
71
|
+
async migrate(): Promise<void> {
|
|
72
|
+
const migrator = new Migrator({
|
|
73
|
+
db: this.options.db,
|
|
74
|
+
provider: this.options.provider,
|
|
75
|
+
});
|
|
76
|
+
const migrations = await migrator.migrateToLatest();
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
78
|
+
if (migrations.error) {
|
|
79
|
+
logger.error(migrations.error, `Failed to apply migrations`);
|
|
80
|
+
throw migrations.error;
|
|
81
|
+
}
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
await this.options.db.destroy();
|
|
84
84
|
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
logger.log(`Applied ${migrations.results?.length} migrations successfully`);
|
|
86
|
+
}
|
|
87
87
|
}
|
package/src/PostgresMigrator.ts
CHANGED
|
@@ -17,21 +17,21 @@ const { Client } = pg;
|
|
|
17
17
|
* ```
|
|
18
18
|
*/
|
|
19
19
|
async function setupClient(uri: string) {
|
|
20
|
-
|
|
20
|
+
const url = new URL(uri);
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
const db = new Client({
|
|
23
|
+
user: url.username,
|
|
24
|
+
password: url.password,
|
|
25
|
+
host: url.hostname,
|
|
26
|
+
port: parseInt(url.port, 10),
|
|
27
|
+
database: 'postgres',
|
|
28
|
+
});
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
let database = url.pathname.slice(1);
|
|
31
|
+
if (database.includes('?')) {
|
|
32
|
+
database = database.substring(0, database.indexOf('?'));
|
|
33
|
+
}
|
|
34
|
+
return { database, db };
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -64,105 +64,105 @@ const logger = console;
|
|
|
64
64
|
* ```
|
|
65
65
|
*/
|
|
66
66
|
export abstract class PostgresMigrator {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Creates a new PostgresMigrator instance.
|
|
69
|
+
*
|
|
70
|
+
* @param uri - PostgreSQL connection URI
|
|
71
|
+
*/
|
|
72
|
+
constructor(private uri: string) {}
|
|
73
73
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Abstract method to be implemented by subclasses.
|
|
76
|
+
* Should contain the migration logic for setting up database schema.
|
|
77
|
+
*
|
|
78
|
+
* @returns Promise that resolves when migrations are complete
|
|
79
|
+
*/
|
|
80
|
+
abstract migrate(): Promise<void>;
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Creates a PostgreSQL database if it doesn't already exist.
|
|
84
|
+
* Connects to the 'postgres' database to check and create the target database.
|
|
85
|
+
*
|
|
86
|
+
* @param uri - PostgreSQL connection URI
|
|
87
|
+
* @returns Object indicating whether the database already existed
|
|
88
|
+
* @private
|
|
89
|
+
*/
|
|
90
|
+
private static async create(
|
|
91
|
+
uri: string,
|
|
92
|
+
): Promise<{ alreadyExisted: boolean }> {
|
|
93
|
+
const { database, db } = await setupClient(uri);
|
|
94
|
+
try {
|
|
95
|
+
await db.connect();
|
|
96
|
+
const result = await db.query(
|
|
97
|
+
`SELECT * FROM pg_catalog.pg_database WHERE datname = '${database}'`,
|
|
98
|
+
);
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
if (result.rowCount === 0) {
|
|
101
|
+
await db.query(`CREATE DATABASE "${database}"`);
|
|
102
|
+
}
|
|
103
103
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
104
|
+
return {
|
|
105
|
+
alreadyExisted: result.rowCount ? result.rowCount > 0 : false,
|
|
106
|
+
};
|
|
107
|
+
} finally {
|
|
108
|
+
await db.end();
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
111
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Drops a PostgreSQL database.
|
|
114
|
+
* Used for cleanup after tests are complete.
|
|
115
|
+
*
|
|
116
|
+
* @param uri - PostgreSQL connection URI
|
|
117
|
+
* @throws Error if database cannot be dropped
|
|
118
|
+
* @private
|
|
119
|
+
*/
|
|
120
|
+
private static async drop(uri: string): Promise<void> {
|
|
121
|
+
const { database, db } = await setupClient(uri);
|
|
122
|
+
try {
|
|
123
|
+
await db.connect();
|
|
124
|
+
await db.query(`DROP DATABASE "${database}"`);
|
|
125
|
+
} finally {
|
|
126
|
+
await db.end();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Starts the migration process by creating the database and running migrations.
|
|
132
|
+
* Returns a cleanup function that will drop the database when called.
|
|
133
|
+
*
|
|
134
|
+
* @returns Async cleanup function that drops the created database
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const migrator = new MyMigrator('postgresql://localhost:5432/test_db');
|
|
139
|
+
*
|
|
140
|
+
* // Start migrations and get cleanup function
|
|
141
|
+
* const cleanup = await migrator.start();
|
|
142
|
+
*
|
|
143
|
+
* try {
|
|
144
|
+
* // Run your tests here
|
|
145
|
+
* await runTests();
|
|
146
|
+
* } finally {
|
|
147
|
+
* // Always clean up
|
|
148
|
+
* await cleanup();
|
|
149
|
+
* }
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
async start() {
|
|
153
|
+
const { database, db } = await setupClient(this.uri);
|
|
154
|
+
try {
|
|
155
|
+
await PostgresMigrator.create(this.uri);
|
|
156
|
+
// Implement migration logic here
|
|
157
|
+
await this.migrate();
|
|
158
|
+
logger.log(`Migrating database: ${database}`);
|
|
159
|
+
// Example: await db.query('CREATE TABLE example (id SERIAL PRIMARY KEY)');
|
|
160
|
+
} finally {
|
|
161
|
+
await db.end();
|
|
162
|
+
}
|
|
163
163
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
164
|
+
return async () => {
|
|
165
|
+
await PostgresMigrator.drop(this.uri);
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
168
|
}
|
|
@@ -38,101 +38,101 @@ const logger = console;
|
|
|
38
38
|
* ```
|
|
39
39
|
*/
|
|
40
40
|
export class PostgresObjectionMigrator extends PostgresMigrator {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
41
|
+
/**
|
|
42
|
+
* Creates a new PostgresObjectionMigrator instance.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Configuration options
|
|
45
|
+
* @param options.uri - PostgreSQL connection URI
|
|
46
|
+
* @param options.knex - Knex database instance configured with migrations
|
|
47
|
+
*/
|
|
48
|
+
constructor(
|
|
49
|
+
private options: {
|
|
50
|
+
uri: string;
|
|
51
|
+
knex: Knex;
|
|
52
|
+
},
|
|
53
|
+
) {
|
|
54
|
+
super(options.uri);
|
|
55
|
+
}
|
|
56
56
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
57
|
+
/**
|
|
58
|
+
* Executes Knex migrations to the latest version.
|
|
59
|
+
* Implements the abstract migrate() method from PostgresMigrator.
|
|
60
|
+
*
|
|
61
|
+
* @throws Error if migrations fail to apply
|
|
62
|
+
* @returns Promise that resolves when all migrations are applied
|
|
63
|
+
*/
|
|
64
|
+
async migrate(): Promise<void> {
|
|
65
|
+
try {
|
|
66
|
+
// Run migrations to latest
|
|
67
|
+
const [batchNo, migrations] = await this.options.knex.migrate.latest();
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
69
|
+
if (migrations.length > 0) {
|
|
70
|
+
logger.log(
|
|
71
|
+
`Applied batch ${batchNo} with ${migrations.length} migrations:`,
|
|
72
|
+
);
|
|
73
|
+
migrations.forEach((migration: string) => {
|
|
74
|
+
logger.log(` - ${migration}`);
|
|
75
|
+
});
|
|
76
|
+
} else {
|
|
77
|
+
logger.log('No pending migrations to apply');
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
logger.error('Failed to apply migrations:', error);
|
|
81
|
+
throw error;
|
|
82
|
+
} finally {
|
|
83
|
+
// Always destroy the connection pool
|
|
84
|
+
await this.options.knex.destroy();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
87
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Rolls back the last batch of migrations.
|
|
90
|
+
* Useful for testing migration rollback scenarios.
|
|
91
|
+
*
|
|
92
|
+
* @returns Promise that resolves when rollback is complete
|
|
93
|
+
*/
|
|
94
|
+
async rollback(): Promise<void> {
|
|
95
|
+
try {
|
|
96
|
+
const [batchNo, migrations] = await this.options.knex.migrate.rollback();
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
98
|
+
if (migrations.length > 0) {
|
|
99
|
+
logger.log(
|
|
100
|
+
`Rolled back batch ${batchNo} with ${migrations.length} migrations:`,
|
|
101
|
+
);
|
|
102
|
+
migrations.forEach((migration: string) => {
|
|
103
|
+
logger.log(` - ${migration}`);
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
logger.log('No migrations to rollback');
|
|
107
|
+
}
|
|
108
|
+
} catch (error) {
|
|
109
|
+
logger.error('Failed to rollback migrations:', error);
|
|
110
|
+
throw error;
|
|
111
|
+
} finally {
|
|
112
|
+
await this.options.knex.destroy();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
115
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Gets the current migration status.
|
|
118
|
+
* Returns information about completed and pending migrations.
|
|
119
|
+
*
|
|
120
|
+
* @returns Promise with migration status information
|
|
121
|
+
*/
|
|
122
|
+
async status(): Promise<{
|
|
123
|
+
completed: string[];
|
|
124
|
+
pending: string[];
|
|
125
|
+
}> {
|
|
126
|
+
try {
|
|
127
|
+
const completed = await this.options.knex.migrate.list();
|
|
128
|
+
const [, pending] = await this.options.knex.migrate.currentVersion();
|
|
129
129
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
130
|
+
return {
|
|
131
|
+
completed: Array.isArray(completed[0]) ? completed[0] : [],
|
|
132
|
+
pending: Array.isArray(pending) ? pending : [],
|
|
133
|
+
};
|
|
134
|
+
} finally {
|
|
135
|
+
await this.options.knex.destroy();
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
138
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Kysely, Transaction } from 'kysely';
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
type IsolationLevel,
|
|
4
|
+
VitestPostgresTransactionIsolator,
|
|
5
5
|
} from './VitestTransactionIsolator';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -42,31 +42,31 @@ import {
|
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
44
|
export class VitestKyselyTransactionIsolator<
|
|
45
|
-
|
|
45
|
+
Database,
|
|
46
46
|
> extends VitestPostgresTransactionIsolator<
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
Kysely<Database>,
|
|
48
|
+
Transaction<Database>
|
|
49
49
|
> {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
50
|
+
async destroy(_conn: Kysely<Database>): Promise<void> {
|
|
51
|
+
// return conn.destroy();
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Creates a Kysely transaction with the specified isolation level.
|
|
55
|
+
* Implements the abstract transact method from VitestPostgresTransactionIsolator.
|
|
56
|
+
*
|
|
57
|
+
* @param conn - The Kysely database connection
|
|
58
|
+
* @param level - The transaction isolation level
|
|
59
|
+
* @param fn - The function to execute within the transaction
|
|
60
|
+
* @returns Promise that resolves when the transaction completes
|
|
61
|
+
*/
|
|
62
|
+
async transact(
|
|
63
|
+
conn: Kysely<Database>,
|
|
64
|
+
level: IsolationLevel,
|
|
65
|
+
fn: (trx: Transaction<Database>) => Promise<void>,
|
|
66
|
+
): Promise<void> {
|
|
67
|
+
const isolationLevel =
|
|
68
|
+
level.toLocaleLowerCase() as Lowercase<IsolationLevel>;
|
|
69
|
+
await conn.transaction().setIsolationLevel(isolationLevel).execute(fn);
|
|
70
|
+
}
|
|
71
|
+
// Implement any Kysely-specific transaction logic here
|
|
72
72
|
}
|