From 586d45982f33ce4977f85323ba9fe3fe25f50f59 Mon Sep 17 00:00:00 2001 From: Eliana Rosselli Date: Tue, 14 Jan 2025 15:07:37 -0300 Subject: [PATCH 1/2] Update updated_at field of cache entry even if value doesn't change --- src/fides/api/models/db_cache.py | 4 ++++ tests/ops/models/test_dbcache.py | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/fides/api/models/db_cache.py b/src/fides/api/models/db_cache.py index 7d19023d4b..ff9c89da81 100644 --- a/src/fides/api/models/db_cache.py +++ b/src/fides/api/models/db_cache.py @@ -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 @@ -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 diff --git a/tests/ops/models/test_dbcache.py b/tests/ops/models/test_dbcache.py index 7fe9062fab..f20e68df86 100644 --- a/tests/ops/models/test_dbcache.py +++ b/tests/ops/models/test_dbcache.py @@ -47,12 +47,16 @@ 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( @@ -60,6 +64,15 @@ def test_update_cache_value(self, db): ) 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( From ebcc49fff10d5c82f2076b35ed7be82d31a6f1d7 Mon Sep 17 00:00:00 2001 From: Eliana Rosselli Date: Tue, 14 Jan 2025 15:11:23 -0300 Subject: [PATCH 2/2] Add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9eb9994d6d..a31b791e82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)