Skip to content

Commit

Permalink
Insert new attrs into the DB with the Cache.update function
Browse files Browse the repository at this point in the history
  • Loading branch information
Balazs Bohar committed Sep 17, 2024
1 parent 1409a9d commit d347beb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions cache_manager/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,7 @@ def update(
key=key,
attrs=attrs,
)

print(items)
update = update or {}
main_fields = self._table_fields()
main = ', '.join(
Expand Down Expand Up @@ -1121,10 +1121,18 @@ def update(
# Adding new attr
q = f'SELECT id FROM attr_{actual_typ} {name_where}'
self._execute(q)
_ids = [i[0] for i in self.cur.fetchall()]
print(_ids)
existing_attr_ids = set(i[0] for i in self.cur.fetchall())
print(existing_attr_ids)

new_attr_ids = set(ids) - existing_attr_ids

if not new_attr_ids:
continue

# Insert new attr to DB
new_values = ",".join(f'id = {i}, {k} = {val}' for i in new_attr_ids)
new_q = f'INSERT INTO attr_{actual_typ} ({new_values})'
self._execute(new_q)

_log(f'Finished updating attributes')

Expand Down
2 changes: 1 addition & 1 deletion tests/test_attr_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def test_attr_update_new(test_cache):
it = test_cache.best_or_new('testattrupdate', attrs = attrs0)

attrs1 = {'foonew': 'barnew'}
test_cache.update(it.key, update = {'attrs': attrs1})
test_cache.update(it.uri, update = {'attrs': attrs1})

it1 = test_cache.search('testattrupdate')[0]

Expand Down

0 comments on commit d347beb

Please sign in to comment.