Skip to content

Commit da0528b

Browse files
authored
Merge pull request #49 from SectorLabs/add-index-predicate
Add missing index_predicate
2 parents 5fb92c2 + b1ecb74 commit da0528b

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

psqlextra/manager/manager.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def upsert(self, conflict_target: List, fields: Dict, index_predicate: str=None)
248248
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
249249
return self.insert(**fields)
250250

251-
def upsert_and_get(self, conflict_target: List, fields: Dict):
251+
def upsert_and_get(self, conflict_target: List, fields: Dict, index_predicate: str=None):
252252
"""Creates a new record or updates the existing one
253253
with the specified data and then gets the row.
254254
@@ -259,15 +259,19 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
259259
fields:
260260
Fields to insert/update.
261261
262+
index_predicate:
263+
The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
264+
conflicts)
265+
262266
Returns:
263267
The model instance representing the row
264268
that was created/updated.
265269
"""
266270

267-
self.on_conflict(conflict_target, ConflictAction.UPDATE)
271+
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
268272
return self.insert_and_get(**fields)
269273

270-
def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
274+
def bulk_upsert(self, conflict_target: List, rows: List[Dict], index_predicate: str=None):
271275
"""Creates a set of new records or updates the existing
272276
ones with the specified data.
273277
@@ -277,9 +281,13 @@ def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
277281
278282
rows:
279283
Rows to upsert.
284+
285+
index_predicate:
286+
The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
287+
conflicts)
280288
"""
281289

282-
self.on_conflict(conflict_target, ConflictAction.UPDATE)
290+
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
283291
return self.bulk_insert(rows)
284292

285293
def _build_insert_compiler(self, rows: List[Dict]):
@@ -468,7 +476,7 @@ def get_queryset(self):
468476

469477
return PostgresQuerySet(self.model, using=self._db)
470478

471-
def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):
479+
def on_conflict(self, fields: List[Union[str, Tuple[str]]], action, index_predicate: str=None):
472480
"""Sets the action to take when conflicts arise when attempting
473481
to insert/create a new row.
474482
@@ -478,8 +486,11 @@ def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):
478486
479487
action:
480488
The action to take when the conflict occurs.
489+
490+
index_predicate:
491+
The index predicate to satisfy an arbiter partial index.
481492
"""
482-
return self.get_queryset().on_conflict(fields, action)
493+
return self.get_queryset().on_conflict(fields, action, index_predicate)
483494

484495
def upsert(self, conflict_target: List, fields: Dict, index_predicate: str=None) -> int:
485496
"""Creates a new record or updates the existing one
@@ -501,7 +512,7 @@ def upsert(self, conflict_target: List, fields: Dict, index_predicate: str=None)
501512

502513
return self.get_queryset().upsert(conflict_target, fields, index_predicate)
503514

504-
def upsert_and_get(self, conflict_target: List, fields: Dict):
515+
def upsert_and_get(self, conflict_target: List, fields: Dict, index_predicate: str=None):
505516
"""Creates a new record or updates the existing one
506517
with the specified data and then gets the row.
507518
@@ -512,26 +523,32 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
512523
fields:
513524
Fields to insert/update.
514525
526+
index_predicate:
527+
The index predicate to satisfy an arbiter partial index.
528+
515529
Returns:
516530
The model instance representing the row
517531
that was created/updated.
518532
"""
519533

520-
return self.get_queryset().upsert_and_get(conflict_target, fields)
534+
return self.get_queryset().upsert_and_get(conflict_target, fields, index_predicate)
521535

522-
def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
536+
def bulk_upsert(self, conflict_target: List, rows: List[Dict], index_predicate: str=None):
523537
"""Creates a set of new records or updates the existing
524538
ones with the specified data.
525539
526540
Arguments:
527541
conflict_target:
528542
Fields to pass into the ON CONFLICT clause.
529543
544+
index_predicate:
545+
The index predicate to satisfy an arbiter partial index.
546+
530547
rows:
531548
Rows to upsert.
532549
"""
533550

534-
return self.get_queryset().bulk_upsert(conflict_target, rows)
551+
return self.get_queryset().bulk_upsert(conflict_target, rows, index_predicate)
535552

536553
@staticmethod
537554
def _on_model_save(sender, **kwargs):

0 commit comments

Comments
 (0)