Skip to content

Commit

Permalink
Merge pull request #44 from AnswerDotAI/test-upserts
Browse files Browse the repository at this point in the history
Test `Table.test-upserts()`
  • Loading branch information
jph00 authored Nov 21, 2024
2 parents bfdac10 + 65dfcc8 commit d038da2
Show file tree
Hide file tree
Showing 2 changed files with 474 additions and 4 deletions.
11 changes: 7 additions & 4 deletions fastlite/kw.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from sqlite_minutils.db import Database,Table,DEFAULT,ForeignKeysType,Default,Queryable,NotFoundError
from enum import Enum

class MissingPrimaryKey(Exception): pass

opt_bool = Union[bool, Default, None]

def database(path, wal=True)->Any:
Expand Down Expand Up @@ -203,13 +205,14 @@ def upsert(
columns: Union[Dict[str, Any], Default, None]=DEFAULT,
strict: Union[bool, Default]|None=DEFAULT,
**kwargs) -> Table:
record = _process_row(record)
record = {**record, **kwargs}
if not record: return {}
if pk==DEFAULT:
assert len(self.pks)==1
pk = self.pks[0]
if not record: record={}
record = _process_row(record)
record = {**record, **kwargs}
last_pk = record[pk]
try: last_pk = record[pk]
except KeyError as e: raise MissingPrimaryKey(e.args[0])
self._orig_upsert(
record=record, pk=pk, foreign_keys=foreign_keys, column_order=column_order, not_null=not_null,
defaults=defaults, hash_id=hash_id, hash_id_columns=hash_id_columns, alter=alter,
Expand Down
Loading

0 comments on commit d038da2

Please sign in to comment.