@geekmidas/testkit 0.0.14 → 0.0.16
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/{Factory-D7P3bKKb.d.mts → Factory-BZ8uMoXl.d.cts} +3 -2
- package/dist/{Factory-pNV7ZQ7-.d.cts → Factory-Cmr3s3-s.d.mts} +3 -2
- package/dist/Factory-WMhTNZ9S.cjs +2 -1
- package/dist/Factory-WMhTNZ9S.cjs.map +1 -0
- package/dist/Factory-z2m01hMj.mjs +2 -1
- package/dist/Factory-z2m01hMj.mjs.map +1 -0
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +2 -2
- package/dist/KyselyFactory-BcYkC0t2.mjs +2 -1
- package/dist/KyselyFactory-BcYkC0t2.mjs.map +1 -0
- package/dist/KyselyFactory-Cf0o2YxO.cjs +2 -1
- package/dist/KyselyFactory-Cf0o2YxO.cjs.map +1 -0
- package/dist/{KyselyFactory-72P98y5I.d.mts → KyselyFactory-Cx3sezwH.d.mts} +4 -3
- package/dist/{KyselyFactory-DLBrYWxU.d.cts → KyselyFactory-DRQ83r0o.d.cts} +4 -3
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +3 -3
- package/dist/ObjectionFactory-8hebmnai.mjs +2 -1
- package/dist/ObjectionFactory-8hebmnai.mjs.map +1 -0
- package/dist/{ObjectionFactory-B40NQWSe.d.mts → ObjectionFactory-C-59Hjwj.d.mts} +4 -3
- package/dist/{ObjectionFactory-D3l1VuyX.d.cts → ObjectionFactory-C4X78k0B.d.cts} +4 -3
- package/dist/ObjectionFactory-CDriunkS.cjs +2 -1
- package/dist/ObjectionFactory-CDriunkS.cjs.map +1 -0
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +3 -3
- package/dist/{PostgresKyselyMigrator-_6yHZigp.d.mts → PostgresKyselyMigrator-CIx3AFSR.d.mts} +3 -2
- package/dist/PostgresKyselyMigrator-CQ3aUoy_.d.cts +2 -1
- package/dist/PostgresKyselyMigrator-CfytARcA.cjs +2 -1
- package/dist/PostgresKyselyMigrator-CfytARcA.cjs.map +1 -0
- package/dist/{PostgresKyselyMigrator-Bdhl251C.mjs → PostgresKyselyMigrator-upT-hmrz.mjs} +3 -2
- package/dist/PostgresKyselyMigrator-upT-hmrz.mjs.map +1 -0
- package/dist/PostgresKyselyMigrator.d.mts +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/PostgresMigrator-D5UkK1_K.d.cts +2 -1
- package/dist/PostgresMigrator-DFcNdCvD.cjs +2 -1
- package/dist/PostgresMigrator-DFcNdCvD.cjs.map +1 -0
- package/dist/{PostgresMigrator-BlvuQl7d.d.mts → PostgresMigrator-DQaRxoaY.d.mts} +2 -1
- package/dist/{PostgresMigrator-DxPC_gGu.mjs → PostgresMigrator-DbuJGAVy.mjs} +4 -3
- package/dist/PostgresMigrator-DbuJGAVy.mjs.map +1 -0
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs +2 -1
- package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs.map +1 -0
- package/dist/PostgresObjectionMigrator-CZHHcCOv.d.cts +2 -1
- package/dist/{PostgresObjectionMigrator-G4h5FLvU.mjs → PostgresObjectionMigrator-DPj2pOpX.mjs} +3 -2
- package/dist/PostgresObjectionMigrator-DPj2pOpX.mjs.map +1 -0
- package/dist/{PostgresObjectionMigrator-C69n7vzr.d.mts → PostgresObjectionMigrator-D_hCcrQu.d.mts} +3 -2
- package/dist/PostgresObjectionMigrator.d.mts +2 -2
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/{VitestKyselyTransactionIsolator-Dq4Oeh-Y.mjs → VitestKyselyTransactionIsolator-BxjlD1YM.mjs} +3 -4
- package/dist/VitestKyselyTransactionIsolator-BxjlD1YM.mjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-CnxpE9cH.d.mts → VitestKyselyTransactionIsolator-COCVfvfr.d.mts} +3 -2
- package/dist/{VitestKyselyTransactionIsolator-Dqy3qNoJ.cjs → VitestKyselyTransactionIsolator-Cst3vFjb.cjs} +3 -4
- package/dist/VitestKyselyTransactionIsolator-Cst3vFjb.cjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator-DYUYVEh9.d.cts +2 -1
- package/dist/VitestKyselyTransactionIsolator.cjs +1 -1
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +1 -1
- package/dist/{VitestObjectionTransactionIsolator-DVtJG2F3.mjs → VitestObjectionTransactionIsolator-BU-jXEhz.mjs} +3 -4
- package/dist/VitestObjectionTransactionIsolator-BU-jXEhz.mjs.map +1 -0
- package/dist/VitestObjectionTransactionIsolator-CJ4ds5Qv.d.cts +2 -1
- package/dist/{VitestObjectionTransactionIsolator-LP4B0cqW.cjs → VitestObjectionTransactionIsolator-DzeF4UAq.cjs} +3 -4
- package/dist/VitestObjectionTransactionIsolator-DzeF4UAq.cjs.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-DIM79dCq.d.mts → VitestObjectionTransactionIsolator-b973r9O1.d.mts} +3 -2
- package/dist/VitestObjectionTransactionIsolator.cjs +1 -1
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +1 -1
- package/dist/VitestTransactionIsolator-BQ5FpLtC.cjs +2 -1
- package/dist/VitestTransactionIsolator-BQ5FpLtC.cjs.map +1 -0
- package/dist/{VitestTransactionIsolator-zdyiS_SY.d.mts → VitestTransactionIsolator-CsfJBxcb.d.mts} +2 -1
- package/dist/VitestTransactionIsolator-CskiiJbW.mjs +2 -1
- package/dist/VitestTransactionIsolator-CskiiJbW.mjs.map +1 -0
- package/dist/VitestTransactionIsolator-DdLNODZg.d.cts +2 -1
- package/dist/VitestTransactionIsolator.d.mts +1 -1
- package/dist/aws.cjs +126 -0
- package/dist/aws.cjs.map +1 -0
- package/dist/aws.d.cts +22 -0
- package/dist/aws.d.mts +22 -0
- package/dist/aws.mjs +123 -0
- package/dist/aws.mjs.map +1 -0
- package/dist/directory-B-Ozljzk.mjs +22 -0
- package/dist/directory-B-Ozljzk.mjs.map +1 -0
- package/dist/directory-B4oYx02C.d.mts +12 -0
- package/dist/directory-BUcnztHI.d.cts +12 -0
- package/dist/directory-BVC8g7cX.cjs +28 -0
- package/dist/directory-BVC8g7cX.cjs.map +1 -0
- package/dist/faker-B14IEMIN.cjs +2 -1
- package/dist/faker-B14IEMIN.cjs.map +1 -0
- package/dist/faker-BGKYFoCT.mjs +2 -1
- package/dist/faker-BGKYFoCT.mjs.map +1 -0
- package/dist/{faker-BSH1EMtg.d.cts → faker-Cg76aFNO.d.cts} +4 -3
- package/dist/{faker-C-Iuk_R1.d.mts → faker-DHh7xs4u.d.mts} +4 -3
- package/dist/faker.d.cts +1 -1
- package/dist/faker.d.mts +1 -1
- package/dist/helpers.cjs +48 -2
- package/dist/helpers.cjs.map +1 -0
- package/dist/helpers.d.cts +2 -1
- package/dist/helpers.d.mts +2 -1
- package/dist/helpers.mjs +47 -2
- package/dist/helpers.mjs.map +1 -0
- package/dist/kysely.cjs +66 -3
- package/dist/kysely.cjs.map +1 -0
- package/dist/kysely.d.cts +5 -4
- package/dist/kysely.d.mts +9 -8
- package/dist/kysely.mjs +68 -5
- package/dist/kysely.mjs.map +1 -0
- package/dist/logger.cjs +23 -0
- package/dist/logger.cjs.map +1 -0
- package/dist/logger.d.cts +11 -0
- package/dist/logger.d.mts +11 -0
- package/dist/logger.mjs +22 -0
- package/dist/logger.mjs.map +1 -0
- package/dist/objection.cjs +81 -3
- package/dist/objection.cjs.map +1 -0
- package/dist/objection.d.cts +5 -4
- package/dist/objection.d.mts +9 -8
- package/dist/objection.mjs +83 -5
- package/dist/objection.mjs.map +1 -0
- package/dist/os/directory.cjs +3 -0
- package/dist/os/directory.d.cts +2 -0
- package/dist/os/directory.d.mts +2 -0
- package/dist/os/directory.mjs +3 -0
- package/dist/os/index.cjs +3 -0
- package/dist/os/index.d.cts +2 -0
- package/dist/os/index.d.mts +2 -0
- package/dist/os/index.mjs +3 -0
- package/dist/timer.cjs +9 -0
- package/dist/timer.cjs.map +1 -0
- package/dist/timer.d.cts +5 -0
- package/dist/timer.d.mts +5 -0
- package/dist/timer.mjs +8 -0
- package/dist/timer.mjs.map +1 -0
- package/package.json +25 -3
- package/src/VitestKyselyTransactionIsolator.ts +2 -2
- package/src/VitestObjectionTransactionIsolator.ts +1 -3
- package/src/aws.ts +131 -0
- package/src/logger.ts +18 -0
- package/src/os/directory.ts +21 -0
- package/src/os/index.ts +1 -0
- package/src/timer.ts +3 -0
- package/dist/__tests__/Factory.spec.cjs +0 -139
- package/dist/__tests__/Factory.spec.d.cts +0 -1
- package/dist/__tests__/Factory.spec.d.mts +0 -1
- package/dist/__tests__/Factory.spec.mjs +0 -138
- package/dist/__tests__/KyselyFactory.spec.cjs +0 -235
- package/dist/__tests__/KyselyFactory.spec.d.cts +0 -1
- package/dist/__tests__/KyselyFactory.spec.d.mts +0 -1
- package/dist/__tests__/KyselyFactory.spec.mjs +0 -234
- package/dist/__tests__/ObjectionFactory.spec.cjs +0 -340
- package/dist/__tests__/ObjectionFactory.spec.d.cts +0 -1
- package/dist/__tests__/ObjectionFactory.spec.d.mts +0 -1
- package/dist/__tests__/ObjectionFactory.spec.mjs +0 -339
- package/dist/__tests__/PostgresKyselyMigrator.spec.cjs +0 -397
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.cts +0 -1
- package/dist/__tests__/PostgresKyselyMigrator.spec.d.mts +0 -1
- package/dist/__tests__/PostgresKyselyMigrator.spec.mjs +0 -396
- package/dist/__tests__/PostgresMigrator.spec.cjs +0 -256
- package/dist/__tests__/PostgresMigrator.spec.d.cts +0 -1
- package/dist/__tests__/PostgresMigrator.spec.d.mts +0 -1
- package/dist/__tests__/PostgresMigrator.spec.mjs +0 -255
- package/dist/__tests__/PostgresObjectionMigrator.spec.cjs +0 -432
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.cts +0 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.d.mts +0 -1
- package/dist/__tests__/PostgresObjectionMigrator.spec.mjs +0 -431
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.cjs +0 -122
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.cts +0 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.d.mts +0 -1
- package/dist/__tests__/VitestObjectionTransactionIsolator.spec.mjs +0 -121
- package/dist/__tests__/faker.spec.cjs +0 -115
- package/dist/__tests__/faker.spec.d.cts +0 -1
- package/dist/__tests__/faker.spec.d.mts +0 -1
- package/dist/__tests__/faker.spec.mjs +0 -114
- package/dist/__tests__/integration.spec.cjs +0 -282
- package/dist/__tests__/integration.spec.d.cts +0 -1
- package/dist/__tests__/integration.spec.d.mts +0 -1
- package/dist/__tests__/integration.spec.mjs +0 -281
- package/dist/helpers-B4TXg3Wp.mjs +0 -86
- package/dist/helpers-Bf0nXhbu.cjs +0 -116
- package/dist/helpers-BuPmgzyQ.mjs +0 -47
- package/dist/helpers-nEUtQ7eo.cjs +0 -53
- package/dist/kysely-BsDbvw3r.mjs +0 -67
- package/dist/kysely-CP1iJMvq.cjs +0 -72
- package/dist/objection-BEPk9h-g.mjs +0 -82
- package/dist/objection-Di7JSist.cjs +0 -87
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
import { PostgresMigrator } from "../PostgresMigrator-DxPC_gGu.mjs";
|
|
2
|
-
import { PostgresKyselyMigrator } from "../PostgresKyselyMigrator-Bdhl251C.mjs";
|
|
3
|
-
import { createTestDatabase } from "../helpers-B4TXg3Wp.mjs";
|
|
4
|
-
import { Kysely, PostgresDialect, sql } from "kysely";
|
|
5
|
-
import { Client, Pool } from "pg";
|
|
6
|
-
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
7
|
-
|
|
8
|
-
//#region src/__tests__/PostgresKyselyMigrator.spec.ts
|
|
9
|
-
var TestMigrationProvider = class {
|
|
10
|
-
migrations = {};
|
|
11
|
-
shouldError = false;
|
|
12
|
-
addMigration(name, migration) {
|
|
13
|
-
this.migrations[name] = migration;
|
|
14
|
-
}
|
|
15
|
-
setError(shouldError) {
|
|
16
|
-
this.shouldError = shouldError;
|
|
17
|
-
}
|
|
18
|
-
async getMigrations() {
|
|
19
|
-
if (this.shouldError) throw new Error("Failed to load migrations");
|
|
20
|
-
return this.migrations;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
describe("PostgresKyselyMigrator", () => {
|
|
24
|
-
let testDbName;
|
|
25
|
-
let cleanupDb;
|
|
26
|
-
let consoleSpy;
|
|
27
|
-
let consoleErrorSpy;
|
|
28
|
-
beforeAll(async () => {
|
|
29
|
-
testDbName = `test_kysely_migrator_${Date.now()}`;
|
|
30
|
-
cleanupDb = await createTestDatabase(testDbName);
|
|
31
|
-
});
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
consoleSpy = vi.spyOn(console, "log").mockImplementation(() => {});
|
|
34
|
-
consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
35
|
-
});
|
|
36
|
-
afterEach(() => {
|
|
37
|
-
consoleSpy.mockRestore();
|
|
38
|
-
consoleErrorSpy.mockRestore();
|
|
39
|
-
});
|
|
40
|
-
afterAll(async () => {
|
|
41
|
-
await cleanupDb();
|
|
42
|
-
});
|
|
43
|
-
describe("constructor", () => {
|
|
44
|
-
it("should create a PostgresKyselyMigrator instance", () => {
|
|
45
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
46
|
-
host: "localhost",
|
|
47
|
-
port: 5432,
|
|
48
|
-
user: "geekmidas",
|
|
49
|
-
password: "geekmidas",
|
|
50
|
-
database: testDbName
|
|
51
|
-
}) }) });
|
|
52
|
-
const provider = new TestMigrationProvider();
|
|
53
|
-
const migrator = new PostgresKyselyMigrator({
|
|
54
|
-
uri: `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`,
|
|
55
|
-
db,
|
|
56
|
-
provider
|
|
57
|
-
});
|
|
58
|
-
expect(migrator).toBeInstanceOf(PostgresKyselyMigrator);
|
|
59
|
-
expect(migrator).toBeInstanceOf(PostgresMigrator);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
describe("migrate method", () => {
|
|
63
|
-
it("should apply migrations successfully", async () => {
|
|
64
|
-
const newDbName = `test_migrate_${Date.now()}`;
|
|
65
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
|
|
66
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
67
|
-
host: "localhost",
|
|
68
|
-
port: 5432,
|
|
69
|
-
user: "geekmidas",
|
|
70
|
-
password: "geekmidas",
|
|
71
|
-
database: newDbName
|
|
72
|
-
}) }) });
|
|
73
|
-
const provider = new TestMigrationProvider();
|
|
74
|
-
provider.addMigration("001_create_users", {
|
|
75
|
-
up: async (db$1) => {
|
|
76
|
-
await db$1.schema.createTable("users").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("name", "varchar", (col) => col.notNull()).addColumn("email", "varchar", (col) => col.notNull().unique()).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
77
|
-
},
|
|
78
|
-
down: async (db$1) => {
|
|
79
|
-
await db$1.schema.dropTable("users").execute();
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
provider.addMigration("002_create_posts", {
|
|
83
|
-
up: async (db$1) => {
|
|
84
|
-
await db$1.schema.createTable("posts").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("title", "varchar", (col) => col.notNull()).addColumn("content", "text", (col) => col.notNull()).addColumn("user_id", "integer", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
85
|
-
},
|
|
86
|
-
down: async (db$1) => {
|
|
87
|
-
await db$1.schema.dropTable("posts").execute();
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
const migrator = new PostgresKyselyMigrator({
|
|
91
|
-
uri,
|
|
92
|
-
db,
|
|
93
|
-
provider
|
|
94
|
-
});
|
|
95
|
-
const cleanup = await migrator.start();
|
|
96
|
-
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining("Applied 2 migrations successfully"));
|
|
97
|
-
const client = new Client({
|
|
98
|
-
host: "localhost",
|
|
99
|
-
port: 5432,
|
|
100
|
-
user: "geekmidas",
|
|
101
|
-
password: "geekmidas",
|
|
102
|
-
database: newDbName
|
|
103
|
-
});
|
|
104
|
-
try {
|
|
105
|
-
await client.connect();
|
|
106
|
-
const tablesResult = await client.query(`
|
|
107
|
-
SELECT table_name FROM information_schema.tables
|
|
108
|
-
WHERE table_schema = 'public'
|
|
109
|
-
AND table_name IN ('users', 'posts')
|
|
110
|
-
ORDER BY table_name
|
|
111
|
-
`);
|
|
112
|
-
expect(tablesResult.rowCount).toBe(2);
|
|
113
|
-
expect(tablesResult.rows).toEqual([{ table_name: "posts" }, { table_name: "users" }]);
|
|
114
|
-
} finally {
|
|
115
|
-
await client.end();
|
|
116
|
-
}
|
|
117
|
-
await cleanup();
|
|
118
|
-
});
|
|
119
|
-
it("should handle migration errors", async () => {
|
|
120
|
-
const errorDbName = `test_migrate_error_${Date.now()}`;
|
|
121
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorDbName}`;
|
|
122
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
123
|
-
host: "localhost",
|
|
124
|
-
port: 5432,
|
|
125
|
-
user: "geekmidas",
|
|
126
|
-
password: "geekmidas",
|
|
127
|
-
database: errorDbName
|
|
128
|
-
}) }) });
|
|
129
|
-
const provider = new TestMigrationProvider();
|
|
130
|
-
provider.addMigration("001_failing_migration", { up: async () => {
|
|
131
|
-
throw new Error("Migration failed intentionally");
|
|
132
|
-
} });
|
|
133
|
-
const migrator = new PostgresKyselyMigrator({
|
|
134
|
-
uri,
|
|
135
|
-
db,
|
|
136
|
-
provider
|
|
137
|
-
});
|
|
138
|
-
await expect(migrator.start()).rejects.toThrow("Migration failed intentionally");
|
|
139
|
-
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.any(Error), "Failed to apply migrations");
|
|
140
|
-
await db.destroy();
|
|
141
|
-
const cleanupClient = new Client({
|
|
142
|
-
host: "localhost",
|
|
143
|
-
port: 5432,
|
|
144
|
-
user: "geekmidas",
|
|
145
|
-
password: "geekmidas",
|
|
146
|
-
database: "postgres"
|
|
147
|
-
});
|
|
148
|
-
try {
|
|
149
|
-
await cleanupClient.connect();
|
|
150
|
-
await cleanupClient.query(`
|
|
151
|
-
SELECT pg_terminate_backend(pg_stat_activity.pid)
|
|
152
|
-
FROM pg_stat_activity
|
|
153
|
-
WHERE pg_stat_activity.datname = '${errorDbName}'
|
|
154
|
-
AND pid <> pg_backend_pid()
|
|
155
|
-
`);
|
|
156
|
-
await cleanupClient.query(`DROP DATABASE IF EXISTS "${errorDbName}"`);
|
|
157
|
-
} finally {
|
|
158
|
-
await cleanupClient.end();
|
|
159
|
-
}
|
|
160
|
-
});
|
|
161
|
-
it("should destroy database connection after migrations", async () => {
|
|
162
|
-
const destroyDbName = `test_destroy_${Date.now()}`;
|
|
163
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${destroyDbName}`;
|
|
164
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
165
|
-
host: "localhost",
|
|
166
|
-
port: 5432,
|
|
167
|
-
user: "geekmidas",
|
|
168
|
-
password: "geekmidas",
|
|
169
|
-
database: destroyDbName
|
|
170
|
-
}) }) });
|
|
171
|
-
const destroySpy = vi.spyOn(db, "destroy");
|
|
172
|
-
const provider = new TestMigrationProvider();
|
|
173
|
-
provider.addMigration("001_simple", { up: async (db$1) => {
|
|
174
|
-
await db$1.schema.createTable("test").addColumn("id", "serial", (col) => col.primaryKey()).execute();
|
|
175
|
-
} });
|
|
176
|
-
const migrator = new PostgresKyselyMigrator({
|
|
177
|
-
uri,
|
|
178
|
-
db,
|
|
179
|
-
provider
|
|
180
|
-
});
|
|
181
|
-
const cleanup = await migrator.start();
|
|
182
|
-
expect(destroySpy).toHaveBeenCalled();
|
|
183
|
-
await cleanup();
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
describe("integration with PostgresMigrator", () => {
|
|
187
|
-
it("should work with complete workflow", async () => {
|
|
188
|
-
const integrationDbName = `test_integration_${Date.now()}`;
|
|
189
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
|
|
190
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
191
|
-
host: "localhost",
|
|
192
|
-
port: 5432,
|
|
193
|
-
user: "geekmidas",
|
|
194
|
-
password: "geekmidas",
|
|
195
|
-
database: integrationDbName
|
|
196
|
-
}) }) });
|
|
197
|
-
const provider = new TestMigrationProvider();
|
|
198
|
-
provider.addMigration("001_initial_schema", { up: async (db$1) => {
|
|
199
|
-
await db$1.schema.createTable("users").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("name", "varchar", (col) => col.notNull()).addColumn("email", "varchar", (col) => col.notNull().unique()).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
200
|
-
await db$1.schema.createTable("posts").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("title", "varchar", (col) => col.notNull()).addColumn("content", "text", (col) => col.notNull()).addColumn("user_id", "integer", (col) => col.notNull().references("users.id").onDelete("cascade")).addColumn("created_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
201
|
-
await db$1.schema.createIndex("idx_posts_user_id").on("posts").column("user_id").execute();
|
|
202
|
-
} });
|
|
203
|
-
provider.addMigration("002_add_updated_at", { up: async (db$1) => {
|
|
204
|
-
await db$1.schema.alterTable("users").addColumn("updated_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
205
|
-
await db$1.schema.alterTable("posts").addColumn("updated_at", "timestamp", (col) => col.defaultTo(sql`now()`).notNull()).execute();
|
|
206
|
-
} });
|
|
207
|
-
const migrator = new PostgresKyselyMigrator({
|
|
208
|
-
uri,
|
|
209
|
-
db,
|
|
210
|
-
provider
|
|
211
|
-
});
|
|
212
|
-
const cleanup = await migrator.start();
|
|
213
|
-
expect(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${integrationDbName}`);
|
|
214
|
-
expect(consoleSpy).toHaveBeenCalledWith("Applied 2 migrations successfully");
|
|
215
|
-
const verifyClient = new Client({
|
|
216
|
-
host: "localhost",
|
|
217
|
-
port: 5432,
|
|
218
|
-
user: "geekmidas",
|
|
219
|
-
password: "geekmidas",
|
|
220
|
-
database: integrationDbName
|
|
221
|
-
});
|
|
222
|
-
try {
|
|
223
|
-
await verifyClient.connect();
|
|
224
|
-
const columnsResult = await verifyClient.query(`
|
|
225
|
-
SELECT table_name, column_name
|
|
226
|
-
FROM information_schema.columns
|
|
227
|
-
WHERE table_schema = 'public'
|
|
228
|
-
AND table_name IN ('users', 'posts')
|
|
229
|
-
AND column_name = 'updated_at'
|
|
230
|
-
ORDER BY table_name
|
|
231
|
-
`);
|
|
232
|
-
expect(columnsResult.rowCount).toBe(2);
|
|
233
|
-
const indexResult = await verifyClient.query(`
|
|
234
|
-
SELECT indexname FROM pg_indexes
|
|
235
|
-
WHERE schemaname = 'public'
|
|
236
|
-
AND indexname = 'idx_posts_user_id'
|
|
237
|
-
`);
|
|
238
|
-
expect(indexResult.rowCount).toBe(1);
|
|
239
|
-
} finally {
|
|
240
|
-
await verifyClient.end();
|
|
241
|
-
}
|
|
242
|
-
await cleanup();
|
|
243
|
-
});
|
|
244
|
-
it("should handle empty migrations", async () => {
|
|
245
|
-
const emptyDbName = `test_empty_${Date.now()}`;
|
|
246
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${emptyDbName}`;
|
|
247
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
248
|
-
host: "localhost",
|
|
249
|
-
port: 5432,
|
|
250
|
-
user: "geekmidas",
|
|
251
|
-
password: "geekmidas",
|
|
252
|
-
database: emptyDbName
|
|
253
|
-
}) }) });
|
|
254
|
-
const provider = new TestMigrationProvider();
|
|
255
|
-
const migrator = new PostgresKyselyMigrator({
|
|
256
|
-
uri,
|
|
257
|
-
db,
|
|
258
|
-
provider
|
|
259
|
-
});
|
|
260
|
-
const cleanup = await migrator.start();
|
|
261
|
-
expect(consoleSpy).toHaveBeenCalledWith("Applied 0 migrations successfully");
|
|
262
|
-
await cleanup();
|
|
263
|
-
});
|
|
264
|
-
it("should work with FileMigrationProvider pattern", async () => {
|
|
265
|
-
const fileProviderDbName = `test_file_provider_${Date.now()}`;
|
|
266
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${fileProviderDbName}`;
|
|
267
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
268
|
-
host: "localhost",
|
|
269
|
-
port: 5432,
|
|
270
|
-
user: "geekmidas",
|
|
271
|
-
password: "geekmidas",
|
|
272
|
-
database: fileProviderDbName
|
|
273
|
-
}) }) });
|
|
274
|
-
const migrations = {
|
|
275
|
-
"2024_01_01_000001_create_users": {
|
|
276
|
-
up: async (db$1) => {
|
|
277
|
-
await db$1.schema.createTable("users").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("username", "varchar", (col) => col.notNull().unique()).execute();
|
|
278
|
-
},
|
|
279
|
-
down: async (db$1) => {
|
|
280
|
-
await db$1.schema.dropTable("users").execute();
|
|
281
|
-
}
|
|
282
|
-
},
|
|
283
|
-
"2024_01_02_000001_create_sessions": {
|
|
284
|
-
up: async (db$1) => {
|
|
285
|
-
await db$1.schema.createTable("sessions").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("user_id", "integer", (col) => col.notNull().references("users.id")).addColumn("token", "varchar", (col) => col.notNull()).execute();
|
|
286
|
-
},
|
|
287
|
-
down: async (db$1) => {
|
|
288
|
-
await db$1.schema.dropTable("sessions").execute();
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
const provider = { async getMigrations() {
|
|
293
|
-
return migrations;
|
|
294
|
-
} };
|
|
295
|
-
const migrator = new PostgresKyselyMigrator({
|
|
296
|
-
uri,
|
|
297
|
-
db,
|
|
298
|
-
provider
|
|
299
|
-
});
|
|
300
|
-
const cleanup = await migrator.start();
|
|
301
|
-
expect(consoleSpy).toHaveBeenCalledWith("Applied 2 migrations successfully");
|
|
302
|
-
const verifyClient = new Client({
|
|
303
|
-
host: "localhost",
|
|
304
|
-
port: 5432,
|
|
305
|
-
user: "geekmidas",
|
|
306
|
-
password: "geekmidas",
|
|
307
|
-
database: fileProviderDbName
|
|
308
|
-
});
|
|
309
|
-
try {
|
|
310
|
-
await verifyClient.connect();
|
|
311
|
-
const tablesResult = await verifyClient.query(`
|
|
312
|
-
SELECT table_name FROM information_schema.tables
|
|
313
|
-
WHERE table_schema = 'public'
|
|
314
|
-
AND table_name IN ('users', 'sessions')
|
|
315
|
-
ORDER BY table_name
|
|
316
|
-
`);
|
|
317
|
-
expect(tablesResult.rowCount).toBe(2);
|
|
318
|
-
} finally {
|
|
319
|
-
await verifyClient.end();
|
|
320
|
-
}
|
|
321
|
-
await cleanup();
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
describe("error scenarios", () => {
|
|
325
|
-
it("should handle provider errors", async () => {
|
|
326
|
-
const providerErrorDbName = `test_provider_error_${Date.now()}`;
|
|
327
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${providerErrorDbName}`;
|
|
328
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
329
|
-
host: "localhost",
|
|
330
|
-
port: 5432,
|
|
331
|
-
user: "geekmidas",
|
|
332
|
-
password: "geekmidas",
|
|
333
|
-
database: providerErrorDbName
|
|
334
|
-
}) }) });
|
|
335
|
-
const provider = new TestMigrationProvider();
|
|
336
|
-
provider.setError(true);
|
|
337
|
-
const migrator = new PostgresKyselyMigrator({
|
|
338
|
-
uri,
|
|
339
|
-
db,
|
|
340
|
-
provider
|
|
341
|
-
});
|
|
342
|
-
await expect(migrator.start()).rejects.toThrow("Failed to load migrations");
|
|
343
|
-
await db.destroy();
|
|
344
|
-
const cleanupClient = new Client({
|
|
345
|
-
host: "localhost",
|
|
346
|
-
port: 5432,
|
|
347
|
-
user: "geekmidas",
|
|
348
|
-
password: "geekmidas",
|
|
349
|
-
database: "postgres"
|
|
350
|
-
});
|
|
351
|
-
try {
|
|
352
|
-
await cleanupClient.connect();
|
|
353
|
-
await cleanupClient.query(`DROP DATABASE IF EXISTS "${providerErrorDbName}"`);
|
|
354
|
-
} finally {
|
|
355
|
-
await cleanupClient.end();
|
|
356
|
-
}
|
|
357
|
-
}, 1e4);
|
|
358
|
-
it("should handle invalid SQL in migrations", async () => {
|
|
359
|
-
const invalidSqlDbName = `test_invalid_sql_${Date.now()}`;
|
|
360
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${invalidSqlDbName}`;
|
|
361
|
-
const db = new Kysely({ dialect: new PostgresDialect({ pool: new Pool({
|
|
362
|
-
host: "localhost",
|
|
363
|
-
port: 5432,
|
|
364
|
-
user: "geekmidas",
|
|
365
|
-
password: "geekmidas",
|
|
366
|
-
database: invalidSqlDbName
|
|
367
|
-
}) }) });
|
|
368
|
-
const provider = new TestMigrationProvider();
|
|
369
|
-
provider.addMigration("001_invalid_sql", { up: async (db$1) => {
|
|
370
|
-
await db$1.schema.createTable("posts").addColumn("id", "serial", (col) => col.primaryKey()).addColumn("user_id", "integer", (col) => col.notNull().references("non_existent_table.id")).execute();
|
|
371
|
-
} });
|
|
372
|
-
const migrator = new PostgresKyselyMigrator({
|
|
373
|
-
uri,
|
|
374
|
-
db,
|
|
375
|
-
provider
|
|
376
|
-
});
|
|
377
|
-
await expect(migrator.start()).rejects.toThrow();
|
|
378
|
-
await db.destroy();
|
|
379
|
-
const cleanupClient = new Client({
|
|
380
|
-
host: "localhost",
|
|
381
|
-
port: 5432,
|
|
382
|
-
user: "geekmidas",
|
|
383
|
-
password: "geekmidas",
|
|
384
|
-
database: "postgres"
|
|
385
|
-
});
|
|
386
|
-
try {
|
|
387
|
-
await cleanupClient.connect();
|
|
388
|
-
await cleanupClient.query(`DROP DATABASE IF EXISTS "${invalidSqlDbName}"`);
|
|
389
|
-
} finally {
|
|
390
|
-
await cleanupClient.end();
|
|
391
|
-
}
|
|
392
|
-
}, 1e4);
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
//#endregion
|
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
const require_chunk = require('../chunk-CUT6urMc.cjs');
|
|
2
|
-
const require_PostgresMigrator = require('../PostgresMigrator-DFcNdCvD.cjs');
|
|
3
|
-
const require_helpers = require('../helpers-Bf0nXhbu.cjs');
|
|
4
|
-
const pg = require_chunk.__toESM(require("pg"));
|
|
5
|
-
const vitest = require_chunk.__toESM(require("vitest"));
|
|
6
|
-
|
|
7
|
-
//#region src/__tests__/PostgresMigrator.spec.ts
|
|
8
|
-
var TestPostgresMigrator = class extends require_PostgresMigrator.PostgresMigrator {
|
|
9
|
-
migrateCalled = false;
|
|
10
|
-
migrateError;
|
|
11
|
-
customMigrations = [];
|
|
12
|
-
async migrate() {
|
|
13
|
-
this.migrateCalled = true;
|
|
14
|
-
if (this.migrateError) throw this.migrateError;
|
|
15
|
-
for (const migration of this.customMigrations) await migration();
|
|
16
|
-
}
|
|
17
|
-
addMigration(migration) {
|
|
18
|
-
this.customMigrations.push(migration);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
(0, vitest.describe)("PostgresMigrator", () => {
|
|
22
|
-
let testDbName;
|
|
23
|
-
let cleanupDb;
|
|
24
|
-
let consoleSpy;
|
|
25
|
-
(0, vitest.beforeAll)(async () => {
|
|
26
|
-
testDbName = `test_postgres_migrator_${Date.now()}`;
|
|
27
|
-
cleanupDb = await require_helpers.createTestDatabase(testDbName);
|
|
28
|
-
});
|
|
29
|
-
(0, vitest.beforeEach)(() => {
|
|
30
|
-
consoleSpy = vitest.vi.spyOn(console, "log").mockImplementation(() => {});
|
|
31
|
-
});
|
|
32
|
-
(0, vitest.afterEach)(() => {
|
|
33
|
-
consoleSpy.mockRestore();
|
|
34
|
-
});
|
|
35
|
-
(0, vitest.afterAll)(async () => {
|
|
36
|
-
await cleanupDb();
|
|
37
|
-
});
|
|
38
|
-
(0, vitest.describe)("constructor", () => {
|
|
39
|
-
(0, vitest.it)("should create a PostgresMigrator instance", () => {
|
|
40
|
-
const migrator = new TestPostgresMigrator(`postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`);
|
|
41
|
-
(0, vitest.expect)(migrator).toBeInstanceOf(require_PostgresMigrator.PostgresMigrator);
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
(0, vitest.describe)("start method", () => {
|
|
45
|
-
(0, vitest.it)("should create database, migrate, and return cleanup function", async () => {
|
|
46
|
-
const newDbName = `test_start_${Date.now()}`;
|
|
47
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${newDbName}`;
|
|
48
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
49
|
-
let migrationRan = false;
|
|
50
|
-
migrator.addMigration(async () => {
|
|
51
|
-
migrationRan = true;
|
|
52
|
-
});
|
|
53
|
-
const cleanup = await migrator.start();
|
|
54
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
55
|
-
(0, vitest.expect)(migrationRan).toBe(true);
|
|
56
|
-
(0, vitest.expect)(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${newDbName}`);
|
|
57
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
58
|
-
await cleanup();
|
|
59
|
-
});
|
|
60
|
-
(0, vitest.it)("should handle existing database", async () => {
|
|
61
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
|
|
62
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
63
|
-
let migrationRan = false;
|
|
64
|
-
migrator.addMigration(async () => {
|
|
65
|
-
migrationRan = true;
|
|
66
|
-
});
|
|
67
|
-
const cleanup = await migrator.start();
|
|
68
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
69
|
-
(0, vitest.expect)(migrationRan).toBe(true);
|
|
70
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
71
|
-
});
|
|
72
|
-
(0, vitest.it)("should handle URI with query parameters", async () => {
|
|
73
|
-
const queryDbName = `test_query_params_${Date.now()}`;
|
|
74
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${queryDbName}?ssl=false&timeout=30`;
|
|
75
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
76
|
-
const cleanup = await migrator.start();
|
|
77
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
78
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
79
|
-
await cleanup();
|
|
80
|
-
});
|
|
81
|
-
(0, vitest.it)("should clean up connections even if migration fails", async () => {
|
|
82
|
-
const failDbName = `test_fail_${Date.now()}`;
|
|
83
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${failDbName}`;
|
|
84
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
85
|
-
migrator.migrateError = new Error("Migration failed");
|
|
86
|
-
await (0, vitest.expect)(migrator.start()).rejects.toThrow("Migration failed");
|
|
87
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
88
|
-
const cleanupClient = new pg.Client({
|
|
89
|
-
host: "localhost",
|
|
90
|
-
port: 5432,
|
|
91
|
-
user: "geekmidas",
|
|
92
|
-
password: "geekmidas",
|
|
93
|
-
database: "postgres"
|
|
94
|
-
});
|
|
95
|
-
try {
|
|
96
|
-
await cleanupClient.connect();
|
|
97
|
-
await cleanupClient.query(`DROP DATABASE IF EXISTS "${failDbName}"`);
|
|
98
|
-
} finally {
|
|
99
|
-
await cleanupClient.end();
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
(0, vitest.it)("should return cleanup function that drops database", async () => {
|
|
103
|
-
const cleanupDbName = `test_cleanup_${Date.now()}`;
|
|
104
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupDbName}`;
|
|
105
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
106
|
-
const cleanup = await migrator.start();
|
|
107
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
108
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
109
|
-
const checkClient = new pg.Client({
|
|
110
|
-
host: "localhost",
|
|
111
|
-
port: 5432,
|
|
112
|
-
user: "geekmidas",
|
|
113
|
-
password: "geekmidas",
|
|
114
|
-
database: "postgres"
|
|
115
|
-
});
|
|
116
|
-
await checkClient.connect();
|
|
117
|
-
const beforeResult = await checkClient.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = $1`, [cleanupDbName]);
|
|
118
|
-
(0, vitest.expect)(beforeResult.rowCount).toBe(1);
|
|
119
|
-
await checkClient.end();
|
|
120
|
-
await cleanup();
|
|
121
|
-
const checkClient2 = new pg.Client({
|
|
122
|
-
host: "localhost",
|
|
123
|
-
port: 5432,
|
|
124
|
-
user: "geekmidas",
|
|
125
|
-
password: "geekmidas",
|
|
126
|
-
database: "postgres"
|
|
127
|
-
});
|
|
128
|
-
await checkClient2.connect();
|
|
129
|
-
const afterResult = await checkClient2.query(`SELECT * FROM pg_catalog.pg_database WHERE datname = $1`, [cleanupDbName]);
|
|
130
|
-
(0, vitest.expect)(afterResult.rowCount).toBe(0);
|
|
131
|
-
await checkClient2.end();
|
|
132
|
-
});
|
|
133
|
-
});
|
|
134
|
-
(0, vitest.describe)("database creation", () => {
|
|
135
|
-
(0, vitest.it)("should handle connection errors gracefully", async () => {
|
|
136
|
-
const badDbName = `test_bad_connection_${Date.now()}`;
|
|
137
|
-
const uri = `postgresql://invalid_user:invalid_pass@localhost:5432/${badDbName}`;
|
|
138
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
139
|
-
await (0, vitest.expect)(migrator.start()).rejects.toThrow();
|
|
140
|
-
});
|
|
141
|
-
(0, vitest.it)("should handle invalid database names", async () => {
|
|
142
|
-
const invalidDbName = "test-invalid-db-name!";
|
|
143
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${invalidDbName}`;
|
|
144
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
145
|
-
await (0, vitest.expect)(migrator.start).rejects.toThrow();
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
(0, vitest.describe)("URI parsing", () => {
|
|
149
|
-
(0, vitest.it)("should parse different URI formats correctly", async () => {
|
|
150
|
-
const testDbName$1 = `test_uri_parsing_${Date.now()}`;
|
|
151
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName$1}`;
|
|
152
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
153
|
-
const cleanup = await migrator.start();
|
|
154
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
155
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
156
|
-
await cleanup();
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
(0, vitest.describe)("error handling", () => {
|
|
160
|
-
(0, vitest.it)("should propagate migration errors", async () => {
|
|
161
|
-
const errorDbName = `test_migration_error_${Date.now()}`;
|
|
162
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${errorDbName}`;
|
|
163
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
164
|
-
const migrationError = new Error("Custom migration error");
|
|
165
|
-
migrator.migrateError = migrationError;
|
|
166
|
-
await (0, vitest.expect)(migrator.start()).rejects.toThrow("Custom migration error");
|
|
167
|
-
const cleanupClient = new pg.Client({
|
|
168
|
-
host: "localhost",
|
|
169
|
-
port: 5432,
|
|
170
|
-
user: "geekmidas",
|
|
171
|
-
password: "geekmidas",
|
|
172
|
-
database: "postgres"
|
|
173
|
-
});
|
|
174
|
-
try {
|
|
175
|
-
await cleanupClient.connect();
|
|
176
|
-
await cleanupClient.query(`DROP DATABASE IF EXISTS "${errorDbName}"`);
|
|
177
|
-
} finally {
|
|
178
|
-
await cleanupClient.end();
|
|
179
|
-
}
|
|
180
|
-
});
|
|
181
|
-
(0, vitest.it)("should handle cleanup errors gracefully", async () => {
|
|
182
|
-
const cleanupErrorDbName = `test_cleanup_error_${Date.now()}`;
|
|
183
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${cleanupErrorDbName}`;
|
|
184
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
185
|
-
const cleanup = await migrator.start();
|
|
186
|
-
const adminClient = new pg.Client({
|
|
187
|
-
host: "localhost",
|
|
188
|
-
port: 5432,
|
|
189
|
-
user: "geekmidas",
|
|
190
|
-
password: "geekmidas",
|
|
191
|
-
database: "postgres"
|
|
192
|
-
});
|
|
193
|
-
await adminClient.connect();
|
|
194
|
-
await adminClient.query(`DROP DATABASE IF EXISTS "${cleanupErrorDbName}"`);
|
|
195
|
-
await adminClient.end();
|
|
196
|
-
await (0, vitest.expect)(cleanup()).rejects.toThrow();
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
(0, vitest.describe)("abstract method", () => {
|
|
200
|
-
(0, vitest.it)("should require concrete implementation of migrate method", () => {
|
|
201
|
-
const migrator = new TestPostgresMigrator(`postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`);
|
|
202
|
-
(0, vitest.expect)(typeof migrator.migrate).toBe("function");
|
|
203
|
-
});
|
|
204
|
-
});
|
|
205
|
-
(0, vitest.describe)("integration scenarios", () => {
|
|
206
|
-
(0, vitest.it)("should handle complete workflow", async () => {
|
|
207
|
-
const integrationDbName = `test_integration_${Date.now()}`;
|
|
208
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${integrationDbName}`;
|
|
209
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
210
|
-
migrator.addMigration(async () => {
|
|
211
|
-
const client = new pg.Client({
|
|
212
|
-
host: "localhost",
|
|
213
|
-
port: 5432,
|
|
214
|
-
user: "geekmidas",
|
|
215
|
-
password: "geekmidas",
|
|
216
|
-
database: integrationDbName
|
|
217
|
-
});
|
|
218
|
-
await client.connect();
|
|
219
|
-
await client.query(`
|
|
220
|
-
CREATE TABLE IF NOT EXISTS test_table (
|
|
221
|
-
id SERIAL PRIMARY KEY,
|
|
222
|
-
name VARCHAR(255) NOT NULL
|
|
223
|
-
)
|
|
224
|
-
`);
|
|
225
|
-
await client.end();
|
|
226
|
-
});
|
|
227
|
-
const cleanup = await migrator.start();
|
|
228
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
229
|
-
(0, vitest.expect)(consoleSpy).toHaveBeenCalledWith(`Migrating database: ${integrationDbName}`);
|
|
230
|
-
const verifyClient = new pg.Client({
|
|
231
|
-
host: "localhost",
|
|
232
|
-
port: 5432,
|
|
233
|
-
user: "geekmidas",
|
|
234
|
-
password: "geekmidas",
|
|
235
|
-
database: integrationDbName
|
|
236
|
-
});
|
|
237
|
-
await verifyClient.connect();
|
|
238
|
-
const tableResult = await verifyClient.query(`
|
|
239
|
-
SELECT table_name FROM information_schema.tables
|
|
240
|
-
WHERE table_schema = 'public' AND table_name = 'test_table'
|
|
241
|
-
`);
|
|
242
|
-
(0, vitest.expect)(tableResult.rowCount).toBe(1);
|
|
243
|
-
await verifyClient.end();
|
|
244
|
-
await cleanup();
|
|
245
|
-
});
|
|
246
|
-
(0, vitest.it)("should handle database that already exists and cleanup", async () => {
|
|
247
|
-
const uri = `postgresql://geekmidas:geekmidas@localhost:5432/${testDbName}`;
|
|
248
|
-
const migrator = new TestPostgresMigrator(uri);
|
|
249
|
-
const cleanup = await migrator.start();
|
|
250
|
-
(0, vitest.expect)(migrator.migrateCalled).toBe(true);
|
|
251
|
-
(0, vitest.expect)(typeof cleanup).toBe("function");
|
|
252
|
-
});
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
|
|
256
|
-
//#endregion
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { };
|