Skip to content
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

HJ-319 Updated updated_at field of DBCache even when value doesn't change #5670

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Changes can also be flagged with a GitHub label for tracking purposes. The URL o
- Updated brand link url [#5656](https://github.com/ethyca/fides/pull/5656)
- Changed "Reclassify" D&D button to show in an overflow menu when row actions are overcrowded [#5655](https://github.com/ethyca/fides/pull/5655)
- Removed primary key requirements for BigQuery and Postgres erasures [#5591](https://github.com/ethyca/fides/pull/5591)
- Updated `DBCache` model so setting cache value always updates the updated_at field [#5669](https://github.com/ethyca/fides/pull/5669)

### Fixed
- Fixed issue where the custom report "reset" button was not working as expected [#5649](https://github.com/ethyca/fides/pull/5649)
Expand Down
4 changes: 4 additions & 0 deletions src/fides/api/models/db_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from sqlalchemy import Column, Index, String
from sqlalchemy.dialects.postgresql import BYTEA
from sqlalchemy.orm import Session
from sqlalchemy.orm.attributes import flag_modified

from fides.api.db.base_class import Base

Expand Down Expand Up @@ -78,6 +79,9 @@ def set_cache_value(
db_cache_entry = cls.get_cache_entry(db, namespace, cache_key)
if db_cache_entry:
db_cache_entry.cache_value = cache_value
# We manually flag it as modified so that the update runs even if the cache_value hasn't changed
# so the updated_at field of the cache entry gets updated.
flag_modified(db_cache_entry, "cache_value")
else:
db_cache_entry = cls(
namespace=namespace.value, cache_key=cache_key, cache_value=cache_value
Expand Down
13 changes: 13 additions & 0 deletions tests/ops/models/test_dbcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,32 @@ def test_update_cache_value(self, db):
).decode()
== "value 1"
)
assert cache_value.updated_at is not None

original_timestamp = cache_value.updated_at

# Update the cache value
cache_value = DBCache.set_cache_value(
db, DBCacheNamespace.LIST_PRIVACY_EXPERIENCE, "some-key", "value 2".encode()
)
assert cache_value.cache_value.decode() == "value 2"
assert cache_value.updated_at > original_timestamp

# Check the value was actually updated
updated_value = DBCache.get_cache_value(
db, DBCacheNamespace.LIST_PRIVACY_EXPERIENCE, "some-key"
)
assert updated_value.decode() == "value 2"

previous_timestamp = cache_value.updated_at

# Updating the value with the same value should still update the timestamp
cache_value = DBCache.set_cache_value(
db, DBCacheNamespace.LIST_PRIVACY_EXPERIENCE, "some-key", "value 2".encode()
)
assert cache_value.cache_value.decode() == "value 2"
assert cache_value.updated_at > previous_timestamp

def test_delete_cache_entry(self, db):
# Add two entries
DBCache.set_cache_value(
Expand Down
Loading