Skip to content

Commit 37c6784

Browse files
committed
Add concatColumns to OnConflictActionDoUpdate for allowing incremental updates in upset.
1 parent cbff707 commit 37c6784

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
## 2.0.0-rc.3
1+
## 2.0.0-rc.4
22

33
- Migrate to `postgres` v3.
4+
- Add `concatColumns` to `OnConflictActionDoUpdate` for allowing incremental updates in `upset`.
45

56
## 1.0.0
67

lib/src/database_access.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:clock/clock.dart';
22
import 'package:logging/logging.dart';
33
import 'package:meta/meta.dart';
44
import 'package:postgres/postgres.dart';
5+
56
// ignore: implementation_imports
67
import 'package:postgres/src/types/type_registry.dart' show TypeRegistryExt;
78
import 'package:postgres_utils/src/config.dart';
@@ -16,9 +17,16 @@ const whereIsNull = Object();
1617
const whereIsNotNull = Object();
1718

1819
class OnConflictActionDoUpdate extends OnConflictAction {
19-
OnConflictActionDoUpdate({required this.indexColumns});
20+
OnConflictActionDoUpdate({
21+
required this.indexColumns,
22+
this.concatColumns = const {},
23+
});
2024

25+
/// Columns which are used to identify uniqueness
2126
final List<String> indexColumns;
27+
28+
/// Columns which should not be overwritten, but concatenated.
29+
final Set<String> concatColumns;
2230
}
2331

2432
abstract class OnConflictAction {
@@ -56,8 +64,15 @@ class DatabaseTransactionBase<TABLES extends TablesBase> {
5664
if (onConflict is OnConflictActionDoUpdate) {
5765
final set = entries
5866
.where((element) => !onConflict.indexColumns.contains(element.key))
59-
.map((e) => '${e.key} = EXCLUDED.${e.key}')
60-
.join(', ');
67+
.map((e) {
68+
if (onConflict.concatColumns.contains(e.key)) {
69+
return '''
70+
${e.key} = CASE WHEN $table.${e.key} IS NULL THEN EXCLUDED.${e.key}
71+
ELSE $table.${e.key} || EXCLUDED.${e.key}
72+
END''';
73+
}
74+
return '${e.key} = EXCLUDED.${e.key}';
75+
}).join(', ');
6176
final where = entries
6277
.where((element) => onConflict.indexColumns.contains(element.key))
6378
.map((e) => '$table.${e.key} = EXCLUDED.${e.key}')
@@ -219,6 +234,7 @@ class CustomTypeBind extends CustomBind {
219234
type,
220235
);
221236
}
237+
222238
CustomTypeBind._(String bind, Object value, Type type)
223239
: super(bind, value, type: type);
224240

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres_utils
22
description: A few utils for working with postgresql databases.
3-
version: 2.0.0-rc.3
3+
version: 2.0.0-rc.4
44
homepage: https://github.com/authpass/postgres_utils.dart
55
issue_tracker: https://github.com/authpass/postgres_utils.dart/issues
66

0 commit comments

Comments
 (0)