Skip to content

Commit ba392b2

Browse files
committed
[FIX] auditlog: Fix invalid reading of stale database values.
Due to the introduction of the ThrowAwayCache, in full log mode when attempting to log a write, the reading of the 'new' values effectively reads the 'old' values because prior changes were not flushed to the database yet at that point. This resulted in missing log lines in the created log entry since the old vs. new value comparison did not indicate any changes.
1 parent d6bbe22 commit ba392b2

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

auditlog/models/rule.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ def write_full(self, vals, **kwargs):
485485
if self.env.user in users_to_exclude:
486486
return result
487487

488+
self.flush_recordset([field_name for field_name in vals.keys()])
489+
488490
with ThrowAwayCache(self.env):
489491
new_values = {d["id"]: d for d in records_write.read(fields_list)}
490492

auditlog/tests/test_auditlog.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,30 @@ def tearDown(self):
321321
self.groups_rule.unlink()
322322
super(TestAuditlogFull, self).tearDown()
323323

324+
def test_update_logs_have_lines_with_changes(self):
325+
self.groups_rule.subscribe()
326+
initial_value = "test-group-1"
327+
new_value = "test-group-2"
328+
group = self.env["res.groups"].create({"name": initial_value})
329+
group.write(
330+
{
331+
"name": new_value,
332+
}
333+
)
334+
log = self.env["auditlog.log"].search(
335+
[
336+
("model_id", "=", self.groups_model_id),
337+
("method", "=", "write"),
338+
("res_id", "=", group.id),
339+
]
340+
)
341+
self.assertEqual(len(log), 1)
342+
self.assertEqual(len(log.line_ids), 1)
343+
log_line = log.line_ids[0]
344+
self.assertEqual(log_line.field_name, "name")
345+
self.assertEqual(log_line.old_value, initial_value)
346+
self.assertEqual(log_line.new_value, new_value)
347+
324348

325349
class TestAuditlogFast(AuditLogRuleCommon, AuditlogCommon):
326350
def setUp(self):

0 commit comments

Comments
 (0)