-
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathhelpers.ts
More file actions
328 lines (301 loc) · 10 KB
/
helpers.ts
File metadata and controls
328 lines (301 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
jest.unmock("pg");
import "mock-fs"; // MUST BE BEFORE EVERYTHING
import { exec } from "child_process";
import { createHash } from "crypto";
import mockFs from "mock-fs";
import { Pool } from "pg";
import { parse } from "pg-connection-string";
import { _migrateMigrationSchema } from "../src/migration";
import { clearAllPools, escapeIdentifier, withClient } from "../src/pgReal";
import { ParsedSettings, parseSettings, Settings } from "../src/settings";
export const TEST_DATABASE_URL: string =
process.env.TEST_DATABASE_URL ||
"postgres://gmtestuser:gmtestpass@localhost/graphile_migrate_test";
export const TEST_SHADOW_DATABASE_URL = TEST_DATABASE_URL + "_shadow";
const parsedTestDatabaseUrl = parse(TEST_DATABASE_URL);
export const TEST_DATABASE_NAME =
parsedTestDatabaseUrl.database || "graphile_migrate_test";
export const TEST_SHADOW_DATABASE_NAME =
parse(TEST_SHADOW_DATABASE_URL).database || "graphile_migrate_test_shadow";
if (!/^[a-zA-Z0-9_-]+$/.test(TEST_DATABASE_NAME)) {
throw new Error("Invalid database name " + TEST_DATABASE_NAME);
}
export const TEST_ROOT_DATABASE_URL: string =
process.env.TEST_ROOT_DATABASE_URL || "postgres:///postgres";
export const settings: Settings = {
connectionString: TEST_DATABASE_URL,
shadowConnectionString: TEST_SHADOW_DATABASE_URL,
rootConnectionString: TEST_ROOT_DATABASE_URL,
};
beforeAll(() => {
// eslint-disable-next-line no-console
console.log("[mock-fs callsites hack]"); // Without this, jest fails due to 'callsites'
mockFs({});
});
afterAll(() => {
mockFs.restore();
});
let rootPgPool: Pool | null = null;
afterAll(() => {
if (rootPgPool) {
rootPgPool.end();
}
rootPgPool = null;
});
afterAll(() => {
clearAllPools();
});
const parsedSettingsPromise = parseSettings(settings);
const ROOT_DB = TEST_ROOT_DATABASE_URL + "?max=1&idleTimeoutMillis=1";
async function createDatabases() {
const { user, password } = parsedTestDatabaseUrl;
if (!user || !password) {
throw new Error(
"TEST_DATABASE_URL does not contain a username and password",
);
}
const parsedSettings = await parsedSettingsPromise;
await withClient(ROOT_DB, parsedSettings, async (client) => {
const result = await client.query(
`select
exists(select 1 from pg_database where datname = $1) as "hasMain",
exists(select 1 from pg_database where datname = $2) as "hasShadow",
exists(select 1 from pg_roles where rolname = $3) as "hasRole"
`,
[TEST_DATABASE_NAME, TEST_SHADOW_DATABASE_NAME, user],
);
if (!result) {
// eslint-disable-next-line no-console
console.dir(client.query);
// eslint-disable-next-line no-console
console.dir(result);
throw new Error("No result?!");
}
const {
rows: [{ hasMain, hasShadow, hasRole }],
} = result;
if (!hasRole) {
await client.query(
`CREATE ROLE ${escapeIdentifier(
user,
)} WITH LOGIN PASSWORD '${password.replace(/'/g, "''")}';`,
);
}
if (!hasMain) {
await client.query(
`CREATE DATABASE ${escapeIdentifier(
TEST_DATABASE_NAME,
)} OWNER ${escapeIdentifier(user)};`,
);
}
if (!hasShadow) {
await client.query(
`CREATE DATABASE ${escapeIdentifier(
TEST_SHADOW_DATABASE_NAME,
)} OWNER ${escapeIdentifier(user)};`,
);
}
});
}
beforeAll(createDatabases);
export async function resetDb() {
const parsedSettings = await parsedSettingsPromise;
await withClient(TEST_DATABASE_URL, parsedSettings, async (client) => {
await client.query("drop schema if exists graphile_migrate cascade;");
{
const { rows } = await client.query(
`select relname from pg_class where relkind = 'r' and relnamespace = 'public'::regnamespace`,
);
for (const row of rows) {
await client.query(
`drop table if exists ${escapeIdentifier(row.relname)} cascade;`,
);
}
}
{
const { rows } = await client.query(
`select typname from pg_type where typtype = 'e' and typnamespace = 'public'::regnamespace`,
);
for (const row of rows) {
await client.query(
`drop type if exists ${escapeIdentifier(row.typname)} cascade;`,
);
}
}
});
}
interface ActionSpies {
getActionCalls: () => string[];
settings: Pick<
Settings,
| "beforeReset"
| "afterReset"
| "beforeAllMigrations"
| "afterAllMigrations"
| "beforeCurrent"
| "afterCurrent"
>;
}
export function makeActionSpies(shadow = false): ActionSpies {
const mockedExec = exec as unknown as jest.Mock<typeof exec>;
if (!mockedExec.mock) {
throw new Error("Must mock child_process");
}
mockedExec.mockReset();
const calls: string[] = [];
mockedExec.mockImplementation((_cmd, _opts, cb): any => {
expect(_opts.env.PATH).toBe(process.env.PATH);
expect(typeof _opts.env.GM_DBURL).toBe("string");
if (shadow) {
expect(_opts.env.GM_SHADOW).toBe("1");
} else {
expect(typeof _opts.env.GM_SHADOW).toBe("undefined");
}
calls.push(_cmd.replace(/^touch /, ""));
cb(null, {
error: null,
stdout: "",
stderr: "",
});
});
function getActionCalls() {
return calls;
}
return {
getActionCalls,
settings: {
beforeAllMigrations: [
{ _: "command", command: "touch beforeAllMigrations" },
],
afterAllMigrations: [
{ _: "command", command: "touch afterAllMigrations" },
],
beforeReset: [{ _: "command", command: "touch beforeReset" }],
afterReset: [{ _: "command", command: "touch afterReset" }],
beforeCurrent: [{ _: "command", command: "touch beforeCurrent" }],
afterCurrent: [{ _: "command", command: "touch afterCurrent" }],
},
};
}
function makePgClientMock() {
return {
__isMockClient: true,
query: jest.fn(async () => {
return { rows: [] };
}),
};
}
export const mockPgClient = makePgClientMock();
export function mockCurrentSqlContentOnce(
parsedSettings: ParsedSettings,
content: string,
) {
mockFs({
[parsedSettings.migrationsFolder + "/current.sql"]: content,
});
}
export async function setup(parsedSettings: ParsedSettings) {
const pool = new Pool({
connectionString: parsedSettings.connectionString,
max: 1,
});
try {
const client = await pool.connect();
try {
await _migrateMigrationSchema(client, parsedSettings);
} finally {
client.release();
}
} finally {
pool.end();
}
}
export const makeMigrations = (commitMessage?: string) => {
const MIGRATION_1_TEXT =
"create table if not exists foo (id serial primary key);";
const MIGRATION_1_HASH = "e00ec93314a423ee5cc68d1182ad52f16442d7df";
const MIGRATION_1_COMMITTED = `--! Previous: -\n--! Hash: sha1:${MIGRATION_1_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_1_TEXT.trim()}\n`;
const MIGRATION_2_TEXT =
"\n\n\ncreate table if not exists bar (id serial primary key);\n\n\n";
const MIGRATION_2_HASH = createHash("sha1")
.update(`sha1:${MIGRATION_1_HASH}\n${MIGRATION_2_TEXT.trim()}` + "\n")
.digest("hex");
const MIGRATION_2_COMMITTED = `--! Previous: sha1:${MIGRATION_1_HASH}\n--! Hash: sha1:${MIGRATION_2_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_2_TEXT.trim()}\n`;
const MIGRATION_ENUM_TEXT =
"drop type if exists user_role;\ncreate type user_role as enum ('User');";
const MIGRATION_ENUM_HASH = createHash("sha1")
.update(`sha1:${MIGRATION_1_HASH}\n${MIGRATION_ENUM_TEXT.trim()}` + "\n")
.digest("hex");
const MIGRATION_ENUM_COMMITTED = `--! Previous: sha1:${MIGRATION_1_HASH}\n--! Hash: sha1:${MIGRATION_ENUM_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_ENUM_TEXT.trim()}\n`;
const MIGRATION_NOTRX_TEXT =
"--! no-transaction\nALTER TYPE user_role ADD VALUE IF NOT EXISTS 'Admin';";
const MIGRATION_NOTRX_HASH = createHash("sha1")
.update(
`sha1:${MIGRATION_ENUM_HASH}\n${MIGRATION_NOTRX_TEXT.trim()}` + "\n",
)
.digest("hex");
const MIGRATION_NOTRX_COMMITTED = `--! Previous: sha1:${MIGRATION_ENUM_HASH}\n--! Hash: sha1:${MIGRATION_NOTRX_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_NOTRX_TEXT.trim()}\n`;
const MIGRATION_INCLUDE_TEXT = `--!include foo.sql`;
const MIGRATION_INCLUDE_COMPILED = `${MIGRATION_INCLUDE_TEXT}\n${MIGRATION_1_TEXT}\n${MIGRATION_INCLUDE_TEXT}`;
const MIGRATION_INCLUDE_HASH = createHash("sha1")
.update(`${MIGRATION_INCLUDE_COMPILED.trim()}` + "\n")
.digest("hex");
const MIGRATION_INCLUDE_COMMITTED = `--! Previous: -\n--! Hash: sha1:${MIGRATION_INCLUDE_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_INCLUDE_COMPILED}\n`;
const MIGRATION_MULTIFILE_FILES = {
"migrations/links/two.sql": "select 2;",
"migrations/current": {
"001.sql": "select 1;",
"002-two.sql": mockFs.symlink({
path: "../links/two.sql",
}),
"003.sql": "select 3;",
},
};
const MIGRATION_MULTIFILE_TEXT = `\
--! split: 001.sql
select 1;
--! split: 002-two.sql
select 2;
--! split: 003.sql
select 3;
`;
const MIGRATION_MULTIFILE_HASH = createHash("sha1")
.update(
`sha1:${MIGRATION_1_HASH}\n${MIGRATION_MULTIFILE_TEXT.trim()}` + "\n",
)
.digest("hex");
const MIGRATION_MULTIFILE_COMMITTED = `--! Previous: sha1:${MIGRATION_1_HASH}\n--! Hash: sha1:${MIGRATION_MULTIFILE_HASH}${
commitMessage ? `\n--! Message: ${commitMessage}` : ``
}\n\n${MIGRATION_MULTIFILE_TEXT.trim()}\n`;
return {
MIGRATION_1_TEXT,
MIGRATION_1_HASH,
MIGRATION_1_COMMITTED,
MIGRATION_2_TEXT,
MIGRATION_2_HASH,
MIGRATION_2_COMMITTED,
MIGRATION_ENUM_TEXT,
MIGRATION_ENUM_HASH,
MIGRATION_ENUM_COMMITTED,
MIGRATION_NOTRX_TEXT,
MIGRATION_NOTRX_HASH,
MIGRATION_NOTRX_COMMITTED,
MIGRATION_INCLUDE_TEXT,
MIGRATION_INCLUDE_HASH,
MIGRATION_INCLUDE_COMMITTED,
MIGRATION_MULTIFILE_TEXT,
MIGRATION_MULTIFILE_HASH,
MIGRATION_MULTIFILE_COMMITTED,
MIGRATION_MULTIFILE_FILES,
};
};