tracktolib 0.49.1__tar.gz → 0.50.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tracktolib
3
- Version: 0.49.1
3
+ Version: 0.50.0
4
4
  Summary: Utility library for python
5
5
  Home-page: https://github.com/tracktor/tracktolib
6
6
  License: MIT
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "tracktolib"
3
- version = "0.49.1"
3
+ version = "0.50.0"
4
4
  description = "Utility library for python"
5
5
  authors = ["Julien Brayere <julien.brayere@tracktor.fr>"]
6
6
  license = "MIT"
@@ -81,7 +81,7 @@ pythonPlatform = "Linux"
81
81
 
82
82
  [tool.commitizen]
83
83
  name = "cz_conventional_commits"
84
- version = "0.49.1"
84
+ version = "0.50.0"
85
85
  tag_format = "$version"
86
86
  version_files = [
87
87
  "pyproject.toml:version"
@@ -11,5 +11,6 @@ from .query import (
11
11
  update_returning,
12
12
  update_one,
13
13
  insert_pg,
14
+ OnConflict,
14
15
  )
15
16
  from .utils import iterate_pg, upsert_csv, safe_pg, safe_pg_context, PGError, PGException
@@ -1,6 +1,7 @@
1
1
  import typing
2
2
  from dataclasses import dataclass, field
3
3
  from typing import TypeVar, Iterable, Callable, Generic, Iterator, TypeAlias, overload, Any, Literal
4
+
4
5
  from ..pg_utils import get_conflict_query
5
6
 
6
7
  try:
@@ -32,6 +33,7 @@ def _get_on_conflict_query(
32
33
  constraint: K | None,
33
34
  on_conflict: K | None,
34
35
  where: K | None,
36
+ merge_columns: Iterable[K] | None,
35
37
  ) -> str:
36
38
  _on_conflict = get_conflict_query(
37
39
  columns=columns,
@@ -40,6 +42,7 @@ def _get_on_conflict_query(
40
42
  constraint=constraint,
41
43
  on_conflict=on_conflict,
42
44
  where=where,
45
+ merge_columns=merge_columns,
43
46
  )
44
47
  return f"{query} {_on_conflict}"
45
48
 
@@ -72,6 +75,7 @@ class PGConflictQuery(Generic[K]):
72
75
  query: str | None = None
73
76
  constraint: str | None = None
74
77
  where: str | None = None
78
+ merge_keys: Iterable[K] | None = None
75
79
 
76
80
  def __post_init__(self):
77
81
  _has_keys = 1 if (self.keys or self.ignore_keys) else 0
@@ -172,6 +176,7 @@ class PGInsertQuery(PGQuery):
172
176
  self.on_conflict.constraint,
173
177
  self.on_conflict.query,
174
178
  self.on_conflict.where,
179
+ self.on_conflict.merge_keys,
175
180
  )
176
181
 
177
182
  # Returning
@@ -1,7 +1,8 @@
1
- from typing_extensions import LiteralString
2
1
  from typing import Iterable
3
2
  from typing import cast
4
3
 
4
+ from typing_extensions import LiteralString
5
+
5
6
 
6
7
  def get_tmp_table_query(
7
8
  schema: LiteralString,
@@ -41,6 +42,7 @@ def get_conflict_query(
41
42
  constraint: str | None = None,
42
43
  on_conflict: str | None = None,
43
44
  where: str | None = None,
45
+ merge_columns: Iterable[str] | None = None,
44
46
  ) -> LiteralString:
45
47
  if on_conflict:
46
48
  return cast(LiteralString, on_conflict)
@@ -55,8 +57,19 @@ def get_conflict_query(
55
57
  else:
56
58
  raise NotImplementedError("update_keys or constraint must be set")
57
59
 
58
- _ignore_columns = [*(update_columns or []), *(ignore_columns or [])]
60
+ _update_columns = update_columns or []
61
+ _ignore_columns = ignore_columns or []
62
+ _merge_columns = merge_columns or []
63
+
64
+ if set(_merge_columns) & set(_update_columns):
65
+ raise ValueError("Duplicate keys found between merge and update")
66
+ if set(_merge_columns) & set(_ignore_columns):
67
+ raise ValueError("Merge column cannot be ignored")
68
+
69
+ _ignore_columns = [*_update_columns, *_ignore_columns, *_merge_columns]
59
70
  fields = ", ".join(f"{x} = COALESCE(EXCLUDED.{x}, t.{x})" for x in columns if x not in _ignore_columns)
71
+ if merge_columns:
72
+ fields += ", ".join(f"{x} = COALESCE(t.{x}, jsonb_build_object()) || EXCLUDED.{x}" for x in merge_columns)
60
73
  if not fields:
61
74
  raise ValueError("No fields set")
62
75
 
File without changes
File without changes