Skip to content

Commit 5b29ed6

Browse files
authored
Fix error logging errors (#200)
2 parents 958a28d + 48512a6 commit 5b29ed6

File tree

9 files changed

+105
-30
lines changed

9 files changed

+105
-30
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
parserOptions: {
1414
ecmaVersion: 2018,
1515
sourceType: "module",
16-
project: "tsconfig.lint.json",
16+
project: "tsconfig.json",
1717
},
1818
env: {
1919
node: true,

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ jobs:
4545
- run: psql "${TEST_ROOT_DATABASE_URL}" -c "CREATE USER someone WITH LOGIN PASSWORD 'something';"
4646
- run: yarn clean
4747
- run: yarn prepack
48-
- run: yarn jest -i --ci
48+
- run: FORCE_COLOR=1 yarn jest -i --ci

__tests__/__snapshots__/migrate.test.ts.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`handles errors during migration gracefully 1`] = `[error: division by zero]`;
4+
5+
exports[`handles errors during migration gracefully 2`] = `
6+
"
7+
🛑 Error occurred whilst processing migration
8+
error: division by zero
9+
at ~/node_modules/pg/lib/client.js:[LINE]:[COL]
10+
at processTicksAndRejections (node:internal/process/task_queues:[LINE]:[COL])
11+
at runQueryWithErrorInstrumentation (~/src/instrumentation.ts:[LINE]:[COL])
12+
at ~/src/migration.ts:[LINE]:[COL]
13+
at withAdvisoryLock (~/src/pgReal.ts:[LINE]:[COL])
14+
at runCommittedMigration (~/src/migration.ts:[LINE]:[COL])
15+
at ~/src/commands/migrate.ts:[LINE]:[COL]
16+
at withAdvisoryLock (~/src/pgReal.ts:[LINE]:[COL])
17+
at ~/src/commands/migrate.ts:[LINE]:[COL]
18+
at withClient (~/src/pgReal.ts:[LINE]:[COL])
19+
at _migrate (~/src/commands/migrate.ts:[LINE]:[COL])
20+
at Object.<anonymous> (~/__tests__/migrate.test.ts:[LINE]:[COL])
21+
22+
Severity: ERROR
23+
Code: 22012
24+
,[object Object]"
25+
`;
26+
327
exports[`refuses to run migration with invalid hash 1`] = `"Hash for 000002.sql does not match - sha1:cbed240dda7dfa510ff785783bbe6af7743b3a11 !== sha1:bddc1ead3310dc1c42cdc7f63537ebdff2e9fd7b; has the file been tampered with?"`;
428
529
exports[`runs migrations 1`] = `

__tests__/migrate.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import "./helpers"; // Has side-effects; must come first
33
import mockFs from "mock-fs";
44

55
import { migrate } from "../src";
6+
import { logDbError } from "../src/instrumentation";
67
import { withClient } from "../src/pg";
78
import { ParsedSettings, parseSettings } from "../src/settings";
89
import { makeMigrations, resetDb, settings } from "./helpers";
@@ -137,3 +138,49 @@ it("will run a migration with invalid hash if told to do so", async () => {
137138
expect(enums).toMatchSnapshot();
138139
}
139140
});
141+
142+
it("handles errors during migration gracefully", async () => {
143+
mockFs({
144+
"migrations/current.sql": ``,
145+
"migrations/committed/000001.sql": `\
146+
--! Previous: -
147+
--! Hash: sha1:2fd4e1c67a2d28fced849ee1bb76e7391b93eb12
148+
--! AllowInvalidHash
149+
150+
drop table if exists frogs;
151+
152+
create table frogs (
153+
id serial primary key,
154+
name text not null,
155+
speckled bool not null
156+
);
157+
158+
select 1/0;
159+
160+
comment on table frogs is 'Ribbit';
161+
`,
162+
});
163+
164+
let err: any;
165+
try {
166+
await migrate(settings);
167+
} catch (e) {
168+
err = e;
169+
}
170+
expect(err).toBeTruthy();
171+
expect(err.message).toMatch(/division by zero/);
172+
expect(err).toMatchSnapshot();
173+
174+
const parsedSettings = await parseSettings(settings);
175+
const mock = jest.fn();
176+
parsedSettings.logger.error = mock;
177+
178+
logDbError(parsedSettings, err);
179+
expect(mock).toHaveBeenCalledTimes(1);
180+
const call = mock.mock.calls[0];
181+
expect(
182+
String(call)
183+
.replaceAll(process.cwd(), "~")
184+
.replace(/:[0-9]+:[0-9]+($|\))/gm, ":[LINE]:[COL]$1"),
185+
).toMatchSnapshot();
186+
});

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
"lint:deps": "depcheck --ignores @types/jest,@types/json5,@types/node,@types/mock-fs,@types/pg,@types/yargs,eslint_d,mock-fs,tslib",
99
"lint:fix": "eslint --ext .js,.jsx,.ts,.tsx,.graphql . --fix; prettier --ignore-path .eslintignore --write '**/*.{js,jsx,ts,tsx,graphql,md,json}'",
1010
"prettier:check": "prettier --ignore-path .eslintignore --check '**/*.{js,jsx,ts,tsx,graphql,md,json}'",
11-
"prepack": "tsc && chmod +x dist/cli.js",
11+
"tsc": "tsc -p tsconfig.build.json",
12+
"prepack": "npm run tsc && chmod +x dist/cli.js",
1213
"clean": "rm -Rf dist",
13-
"test": "yarn lint && yarn run lint:deps && yarn run test:only --ci",
14+
"test": "yarn lint && yarn run lint:deps && FORCE_COLOR=1 yarn run test:only --ci",
1415
"test:only": "jest -i",
1516
"version": "yarn prepack && ./scripts/update-docs.js && node ./scripts/version.mjs && git add README.md src/version.ts",
16-
"watch": "mkdir -p dist && touch dist/cli.js && chmod +x dist/cli.js && tsc --watch"
17+
"watch": "mkdir -p dist && touch dist/cli.js && chmod +x dist/cli.js && npm run tsc --watch"
1718
},
1819
"bin": {
1920
"graphile-migrate": "./dist/cli.js"

src/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as chalk from "chalk";
1+
import chalk from "chalk";
22
import { QueryResultRow } from "pg";
33

44
import indent from "./indent";

tsconfig.build.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"extends": "@tsconfig/node18/tsconfig.json",
3+
"compilerOptions": {
4+
"rootDir": "src",
5+
"declarationDir": "./dist",
6+
"outDir": "./dist",
7+
"declaration": true,
8+
"allowJs": false,
9+
"sourceMap": true,
10+
"pretty": true,
11+
"importHelpers": true,
12+
"experimentalDecorators": true,
13+
"noImplicitAny": true,
14+
"strictNullChecks": true,
15+
"noFallthroughCasesInSwitch": true,
16+
"noUnusedParameters": false,
17+
"noUnusedLocals": false,
18+
"preserveWatchOutput": true,
19+
"sourceMap": true
20+
},
21+
"include": ["src/**/*"],
22+
"exclude": ["**/__mocks__/*"]
23+
}

tsconfig.json

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,8 @@
11
{
2-
"extends": "@tsconfig/node18/tsconfig.json",
2+
"extends": "./tsconfig.build.json",
3+
"include": ["src/**/*", "__tests__/**/*", "*.js", "./.*.js"],
4+
"exclude": [],
35
"compilerOptions": {
4-
"rootDir": "src",
5-
"declarationDir": "./dist",
6-
"outDir": "./dist",
7-
"declaration": true,
8-
"allowJs": false,
9-
"sourceMap": true,
10-
"pretty": true,
11-
"importHelpers": true,
12-
"experimentalDecorators": true,
13-
"noImplicitAny": true,
14-
"strictNullChecks": true,
15-
"noFallthroughCasesInSwitch": true,
16-
"noUnusedParameters": false,
17-
"noUnusedLocals": false,
18-
"preserveWatchOutput": true,
19-
"sourceMap": true,
6+
"noEmit": true,
207
},
21-
"include": ["src/**/*"],
22-
"exclude": ["**/__mocks__/*"],
238
}

tsconfig.lint.json

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)