fix(db): Fix encrypted field metrics table_name tag #106957
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix encrypted field metrics to properly send the table name in metric tags.
Problem
The encrypted field metrics were not sending the model/table name correctly. The metric tag used
getattr(self, "_model_name", "unknown"), but_model_namewas never being set, so all metrics showed "unknown" for the model name. Additionally, the tag was named"model"which is too generic.Root Cause
The original code implemented
__set_name__()to capture the model name, expecting Python's descriptor protocol to call it. However, Django'sModelBasemetaclass removes fields from the class dict and stores them in_metabefore Python can call__set_name__(). Django usescontribute_to_class()instead, which the encrypted field wasn't implementing.Changes
_model_namein__init__with default value "unknown" (for standalone field usage in tests)contribute_to_class()to capture the actual model name when field is added to a Django model"model"to"table_name"for better claritytable_nameTesting
Added two new tests:
test_encrypted_char_field_metrics_on_encrypt: Verifies encryption metrics have correcttable_nametagtest_encrypted_char_field_metrics_on_decrypt: Verifies decryption metrics have correcttable_nametagAll 68 encryption field tests pass.