Skip to content

Commit f2b2f95

Browse files
committed
Support ON CONFLICT clause in Postgres and SQLite
1 parent eb692ef commit f2b2f95

File tree

6 files changed

+24
-10
lines changed

6 files changed

+24
-10
lines changed

src/languages/postgresql/postgresql.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ const onelineClauses = expandPhrases([
3636
// - update:
3737
'UPDATE [ONLY]',
3838
'WHERE CURRENT OF',
39+
// - insert:
40+
'ON CONFLICT',
3941
// - delete:
4042
'DELETE FROM [ONLY]',
4143
// - drop table:

src/languages/sqlite/sqlite.formatter.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ const reservedClauses = expandPhrases([
3232
const onelineClauses = expandPhrases([
3333
// - update:
3434
'UPDATE [OR ABORT | OR FAIL | OR IGNORE | OR REPLACE | OR ROLLBACK]',
35+
// - insert:
36+
'ON CONFLICT',
3537
// - delete:
3638
'DELETE FROM',
3739
// - drop table:

test/behavesLikeMariaDbFormatter.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,4 @@ export default function behavesLikeMariaDbFormatter(format: FormatFn) {
116116
(2, 'Dog');
117117
`);
118118
});
119-
120-
// Regression test for issue #535
121-
it('gracefully handles ON CONFLICT .. DO syntax', () => {
122-
expect(format(`INSERT INTO tbl VALUES (1,'Blah') ON CONFLICT DO NOTHING;`)).toBe(dedent`
123-
INSERT INTO
124-
tbl
125-
VALUES
126-
(1, 'Blah') ON CONFLICT DO NOTHING;
127-
`);
128-
});
129119
}

test/features/onConflict.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import dedent from 'dedent-js';
2+
3+
import { FormatFn } from '../../src/sqlFormatter.js';
4+
5+
export default function supportsOnConflict(format: FormatFn) {
6+
// Regression test for issue #535
7+
it('supports INSERT .. ON CONFLICT syntax', () => {
8+
expect(format(`INSERT INTO tbl VALUES (1,'Blah') ON CONFLICT DO NOTHING;`)).toBe(dedent`
9+
INSERT INTO
10+
tbl
11+
VALUES
12+
(1, 'Blah')
13+
ON CONFLICT DO NOTHING;
14+
`);
15+
});
16+
}

test/postgresql.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import supportsInsertInto from './features/insertInto.js';
2525
import supportsUpdate from './features/update.js';
2626
import supportsTruncateTable from './features/truncateTable.js';
2727
import supportsCreateView from './features/createView.js';
28+
import supportsOnConflict from './features/onConflict.js';
2829

2930
describe('PostgreSqlFormatter', () => {
3031
const language = 'postgresql';
@@ -45,6 +46,7 @@ describe('PostgreSqlFormatter', () => {
4546
});
4647
supportsDeleteFrom(format);
4748
supportsInsertInto(format);
49+
supportsOnConflict(format);
4850
supportsUpdate(format, { whereCurrentOf: true });
4951
supportsTruncateTable(format, { withoutTable: true });
5052
supportsStrings(format, ["''-qq", "U&''", "X''", "B''"]);

test/sqlite.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import supportsLimiting from './features/limiting.js';
2121
import supportsInsertInto from './features/insertInto.js';
2222
import supportsUpdate from './features/update.js';
2323
import supportsCreateView from './features/createView.js';
24+
import supportsOnConflict from './features/onConflict.js';
2425

2526
describe('SqliteFormatter', () => {
2627
const language = 'sqlite';
@@ -40,6 +41,7 @@ describe('SqliteFormatter', () => {
4041
});
4142
supportsDeleteFrom(format);
4243
supportsInsertInto(format);
44+
supportsOnConflict(format);
4345
supportsUpdate(format);
4446
supportsStrings(format, ["''-qq", "X''"]);
4547
supportsIdentifiers(format, [`""-qq`, '``', '[]']);

0 commit comments

Comments
 (0)