Skip to content

Commit

Permalink
fix: don't share default values in ArchiveField
Browse files Browse the repository at this point in the history
Passing an object as the default value to `ArchiveField` (such as `{}`)
means that we always return the same reference to multiple objects. So they're shared.
(thanks @scott-codecov for pointing that out)

So instead we will pass a class and call the class, creating a new object if we have to use it.
Noticed I hacked it a little for the default of the defaults to return `None`.
We can do the same if we ever need more complex values, or just create a new class.

I also moved the log of dbfield and archive_field None to be DEBUG and not INFO.
We are getting that quite a lot and it's making support's work hard.
  • Loading branch information
giovanni-guidini committed Aug 16, 2023
1 parent ad044e4 commit bb82834
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 10 deletions.
4 changes: 2 additions & 2 deletions database/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def should_write_to_storage(self) -> bool:
_report_json_storage_path = Column("report_storage_path", types.Text, nullable=True)
report_json = ArchiveField(
should_write_to_storage_fn=should_write_to_storage,
default_value={},
default_value_class=dict,
)


Expand Down Expand Up @@ -358,7 +358,7 @@ def should_write_to_storage(self) -> bool:
_flare = Column("flare", postgresql.JSON)
_flare_storage_path = Column("flare_storage_path", types.Text, nullable=True)
flare = ArchiveField(
should_write_to_storage_fn=should_write_to_storage, default_value={}
should_write_to_storage_fn=should_write_to_storage, default_value_class=dict
)


Expand Down
2 changes: 1 addition & 1 deletion database/models/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _should_write_to_storage(self) -> bool:
files_array = ArchiveField(
should_write_to_storage_fn=_should_write_to_storage,
rehydrate_fn=rehydrate_encoded_data,
default_value=[],
default_value_class=list,
)


Expand Down
4 changes: 1 addition & 3 deletions database/tests/unit/test_model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ def __init__(
self._archive_field_storage_path = archive_value
self.should_write_to_gcs = should_write_to_gcs

archive_field = ArchiveField(
should_write_to_storage_fn=should_write_to_storage, default_value=None
)
archive_field = ArchiveField(should_write_to_storage_fn=should_write_to_storage)

class ClassWithArchiveFieldMissingMethods:
commit: Commit
Expand Down
8 changes: 4 additions & 4 deletions database/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def __init__(
should_write_to_storage_fn: Callable[[object], bool],
rehydrate_fn: Callable[[object, object], Any] = lambda self, x: x,
json_encoder=ReportEncoder,
default_value=None,
default_value_class=lambda: None,
):
self.default_value = default_value
self.default_value_class = default_value_class
self.rehydrate_fn = rehydrate_fn
self.should_write_to_storage_fn = should_write_to_storage_fn
self.json_encoder = json_encoder
Expand Down Expand Up @@ -103,14 +103,14 @@ def _get_value_from_archive(self, obj):
),
)
else:
log.info(
log.debug(
"Both db_field and archive_field are None",
extra=dict(
object_id=obj.id,
commit=obj.get_commitid(),
),
)
return self.default_value
return self.default_value_class()

def __get__(self, obj, objtype=None):
cached_value = getattr(obj, self.cached_value_property_name, None)
Expand Down

0 comments on commit bb82834

Please sign in to comment.