Skip to content
This repository was archived by the owner on Jun 14, 2026. It is now read-only.

Commit e0c00ec

Browse files
cliedemankoskimas
authored andcommitted
Add schema and database error properties
1 parent b7f0aa2 commit e0c00ec

11 files changed

Lines changed: 63 additions & 31 deletions

File tree

.travis.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ addons:
1313
- postgresql-client-10
1414

1515
node_js:
16-
- "6"
17-
- "7"
18-
- "8"
19-
- "9"
20-
- "10"
21-
- "11"
16+
- '6'
17+
- '7'
18+
- '8'
19+
- '9'
20+
- '10'
21+
- '11'
2222

2323
env:
2424
- POSTGRESQL_VERSION=9.4 MYSQL_VERSION=5.5

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,21 @@ declare namespace DbTypes {
1717
class ForeignKeyViolationError extends ConstraintViolationError {
1818
table: string;
1919
constraint: string;
20+
schema?: string;
2021
}
2122

2223
class NotNullViolationError extends ConstraintViolationError {
2324
table: string;
2425
column: string;
26+
database?: string;
27+
schema?: string;
2528
}
2629

2730
class UniqueViolationError extends ConstraintViolationError {
2831
table: string;
2932
columns: string[];
3033
constraint: string;
34+
schema?: string;
3135
}
3236

3337
function wrapError(err: Error): DBError

lib/errors/ForeignKeyViolationError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class ForeignKeyViolationError extends ConstraintViolationError {
99

1010
this.table = args.table;
1111
this.constraint = args.constraint;
12+
this.schema = args.schema;
1213
}
1314
}
1415

lib/errors/NotNullViolationError.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class NotNullViolationError extends ConstraintViolationError {
99

1010
this.table = args.table;
1111
this.column = args.column;
12+
this.schema = args.schema;
13+
this.database = args.database;
1214
}
1315
}
1416

lib/errors/UniqueViolationError.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class UniqueViolationError extends ConstraintViolationError {
1010
this.table = args.table;
1111
this.columns = args.columns;
1212
this.constraint = args.constraint;
13+
this.schema = args.schema;
1314
}
1415
}
1516

lib/parsers/mssql/DBError/ForeignKeyViolationError/parser.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const ForeignKeyViolationError = require('../../../../errors/ForeignKeyViolationError');
22
const { isCode } = require('../util');
33

4-
const INSERT_UPDATE_REGEX = /The (?:INSERT|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./;
5-
const DELETE_REGEX = /The DELETE statement conflicted with the REFERENCE constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)", column '(.+)'./;
4+
const INSERT_UPDATE_REGEX = /The (?:INSERT|UPDATE) statement conflicted with the FOREIGN KEY constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)\.(.+)", column '(.+)'./;
5+
const DELETE_REGEX = /The DELETE statement conflicted with the REFERENCE constraint "(.+)". The conflict occurred in database "(.+)", table "(.+)\.(.+)", column '(.+)'./;
66

77
module.exports = {
88
error: ForeignKeyViolationError,
@@ -13,7 +13,8 @@ module.exports = {
1313

1414
if (insertUpdateMatch) {
1515
return {
16-
table: insertUpdateMatch[3],
16+
table: insertUpdateMatch[4],
17+
schema: insertUpdateMatch[3],
1718
constraint: insertUpdateMatch[1],
1819
};
1920
}
@@ -22,7 +23,8 @@ module.exports = {
2223

2324
if (deleteMatch) {
2425
return {
25-
table: deleteMatch[3],
26+
table: deleteMatch[4],
27+
schema: deleteMatch[3],
2628
constraint: deleteMatch[1],
2729
};
2830
}

lib/parsers/mssql/DBError/NotNullViolationError/parser.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const NotNullViolationError = require('../../../../errors/NotNullViolationError');
22
const { isCode } = require('../util');
33

4-
const REGEX = /Cannot insert the value NULL into column '(.+)', table '(.+)'; column does not allow nulls. (?:INSERT|UPDATE) fails./;
4+
const REGEX = /Cannot insert the value NULL into column '(.+)', table '(.+)\.(.+)\.(.+)'; column does not allow nulls. (?:INSERT|UPDATE) fails./;
55

66
module.exports = {
77
error: NotNullViolationError,
@@ -15,8 +15,10 @@ module.exports = {
1515
}
1616

1717
return {
18-
table: match[2],
1918
column: match[1],
19+
database: match[2],
20+
schema: match[3],
21+
table: match[4],
2022
};
2123
} else {
2224
return null;

lib/parsers/mssql/DBError/UniqueViolationError/parser.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const UniqueViolationError = require('../../../../errors/UniqueViolationError');
22
const { isCode } = require('../util');
33

4-
const UNIQUE_INDEX_REGEX = /Cannot insert duplicate key row in object '(.+)' with unique index '(.+)'. The duplicate key value is (.+)./;
5-
const UNIQUE_CONSTRAINT_REGEX = /Violation of UNIQUE KEY constraint '(.+)'. Cannot insert duplicate key in object '(.+)'. The duplicate key value is \((.+)\)/;
4+
const UNIQUE_INDEX_REGEX = /Cannot insert duplicate key row in object '(.+)\.(.+)' with unique index '(.+)'. The duplicate key value is (.+)./;
5+
const UNIQUE_CONSTRAINT_REGEX = /Violation of UNIQUE KEY constraint '(.+)'. Cannot insert duplicate key in object '(.+)\.(.+)'. The duplicate key value is \((.+)\)/;
66

77
// 2601 - Violation in unique index
88
// 2627 - Violation in unique constraint (although it is implemented using unique index)
@@ -19,7 +19,8 @@ module.exports = {
1919
}
2020

2121
return {
22-
table: constraintMatch[2],
22+
table: constraintMatch[3],
23+
schema: constraintMatch[2],
2324
constraint: constraintMatch[1]
2425
};
2526
}
@@ -33,8 +34,9 @@ module.exports = {
3334
}
3435

3536
return {
36-
table: indexMatch[1],
37-
constraint: indexMatch[2]
37+
table: indexMatch[2],
38+
constraint: indexMatch[3],
39+
schema: indexMatch[1],
3840
};
3941
}
4042

tests/ForeignKeyViolationError.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ module.exports = (session) => {
5252
}
5353

5454
if (session.isMssql()) {
55-
expect(error.table).to.equal('dbo.target');
55+
expect(error.schema).to.equal('dbo');
56+
expect(error.table).to.equal('target');
5657
expect(error.constraint).to.equal('source_foreign_key_foreign');
5758
}
5859
});
@@ -76,7 +77,8 @@ module.exports = (session) => {
7677
}
7778

7879
if (session.isMssql()) {
79-
expect(error.table).to.equal('dbo.target');
80+
expect(error.schema).to.equal('dbo');
81+
expect(error.table).to.equal('target');
8082
expect(error.constraint).to.equal('source_foreignkey_foreign');
8183
}
8284
});
@@ -105,7 +107,8 @@ module.exports = (session) => {
105107
}
106108

107109
if (session.isMssql()) {
108-
expect(error.table).to.equal('dbo.target');
110+
expect(error.schema).to.equal('dbo');
111+
expect(error.table).to.equal('target');
109112
expect(error.constraint).to.equal('source_foreign_key_foreign');
110113
}
111114
});
@@ -130,7 +133,8 @@ module.exports = (session) => {
130133
}
131134

132135
if (session.isMssql()) {
133-
expect(error.table).to.equal('dbo.target');
136+
expect(error.schema).to.equal('dbo');
137+
expect(error.table).to.equal('target');
134138
expect(error.constraint).to.equal('source_foreignkey_foreign');
135139
}
136140
});
@@ -159,7 +163,8 @@ module.exports = (session) => {
159163
}
160164

161165
if (session.isMssql()) {
162-
expect(error.table).to.equal('dbo.source');
166+
expect(error.schema).to.equal('dbo');
167+
expect(error.table).to.equal('source');
163168
expect(error.constraint).to.equal('source_foreign_key_foreign');
164169
}
165170
});

tests/NotNullViolationError.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ module.exports = (session) => {
4343
}
4444

4545
if (session.isMssql()) {
46-
expect(error.table).to.equal('master.dbo.' + table);
46+
expect(error.database).to.equal('master');
47+
expect(error.schema).to.equal('dbo');
48+
expect(error.table).to.equal(table);
4749
}
4850
});
4951
});
@@ -69,7 +71,9 @@ module.exports = (session) => {
6971
}
7072

7173
if (session.isMssql()) {
72-
expect(error.table).to.equal('master.dbo.' + table);
74+
expect(error.database).to.equal('master');
75+
expect(error.schema).to.equal('dbo');
76+
expect(error.table).to.equal(table);
7377
}
7478
});
7579
});
@@ -95,7 +99,9 @@ module.exports = (session) => {
9599
}
96100

97101
if (session.isMssql()) {
98-
expect(error.table).to.equal('master.dbo.' + table);
102+
expect(error.database).to.equal('master');
103+
expect(error.schema).to.equal('dbo');
104+
expect(error.table).to.equal(table);
99105
}
100106
});
101107
});
@@ -125,7 +131,9 @@ module.exports = (session) => {
125131
}
126132

127133
if (session.isMssql()) {
128-
expect(error.table).to.equal('master.dbo.' + table);
134+
expect(error.database).to.equal('master');
135+
expect(error.schema).to.equal('dbo');
136+
expect(error.table).to.equal(table);
129137
}
130138
});
131139
});
@@ -150,7 +158,9 @@ module.exports = (session) => {
150158
}
151159

152160
if (session.isMssql()) {
153-
expect(error.table).to.equal('master.dbo.' + table);
161+
expect(error.database).to.equal('master');
162+
expect(error.schema).to.equal('dbo');
163+
expect(error.table).to.equal(table);
154164
}
155165
});
156166
});

0 commit comments

Comments
 (0)