Skip to content

Add missing index_predicate #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions psqlextra/manager/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def upsert(self, conflict_target: List, fields: Dict, index_predicate: str=None)
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
return self.insert(**fields)

def upsert_and_get(self, conflict_target: List, fields: Dict):
def upsert_and_get(self, conflict_target: List, fields: Dict, index_predicate: str=None):
"""Creates a new record or updates the existing one
with the specified data and then gets the row.

Expand All @@ -259,15 +259,19 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
fields:
Fields to insert/update.

index_predicate:
The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
conflicts)

Returns:
The model instance representing the row
that was created/updated.
"""

self.on_conflict(conflict_target, ConflictAction.UPDATE)
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
return self.insert_and_get(**fields)

def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
def bulk_upsert(self, conflict_target: List, rows: List[Dict], index_predicate: str=None):
"""Creates a set of new records or updates the existing
ones with the specified data.

Expand All @@ -277,9 +281,13 @@ def bulk_upsert(self, conflict_target: List, rows: List[Dict]):

rows:
Rows to upsert.

index_predicate:
The index predicate to satisfy an arbiter partial index (i.e. what partial index to use for checking
conflicts)
"""

self.on_conflict(conflict_target, ConflictAction.UPDATE)
self.on_conflict(conflict_target, ConflictAction.UPDATE, index_predicate)
return self.bulk_insert(rows)

def _build_insert_compiler(self, rows: List[Dict]):
Expand Down Expand Up @@ -468,7 +476,7 @@ def get_queryset(self):

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

def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):
def on_conflict(self, fields: List[Union[str, Tuple[str]]], action, index_predicate: str=None):
"""Sets the action to take when conflicts arise when attempting
to insert/create a new row.

Expand All @@ -478,8 +486,11 @@ def on_conflict(self, fields: List[Union[str, Tuple[str]]], action):

action:
The action to take when the conflict occurs.

index_predicate:
The index predicate to satisfy an arbiter partial index.
"""
return self.get_queryset().on_conflict(fields, action)
return self.get_queryset().on_conflict(fields, action, index_predicate)

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

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

def upsert_and_get(self, conflict_target: List, fields: Dict):
def upsert_and_get(self, conflict_target: List, fields: Dict, index_predicate: str=None):
"""Creates a new record or updates the existing one
with the specified data and then gets the row.

Expand All @@ -512,26 +523,32 @@ def upsert_and_get(self, conflict_target: List, fields: Dict):
fields:
Fields to insert/update.

index_predicate:
The index predicate to satisfy an arbiter partial index.

Returns:
The model instance representing the row
that was created/updated.
"""

return self.get_queryset().upsert_and_get(conflict_target, fields)
return self.get_queryset().upsert_and_get(conflict_target, fields, index_predicate)

def bulk_upsert(self, conflict_target: List, rows: List[Dict]):
def bulk_upsert(self, conflict_target: List, rows: List[Dict], index_predicate: str=None):
"""Creates a set of new records or updates the existing
ones with the specified data.

Arguments:
conflict_target:
Fields to pass into the ON CONFLICT clause.

index_predicate:
The index predicate to satisfy an arbiter partial index.

rows:
Rows to upsert.
"""

return self.get_queryset().bulk_upsert(conflict_target, rows)
return self.get_queryset().bulk_upsert(conflict_target, rows, index_predicate)

@staticmethod
def _on_model_save(sender, **kwargs):
Expand Down