-
- Downloads
[IMP] core: better way to set/update magic fields
The goal of this change is to simplify the code managing `create_date`
and `write_date` in methods `create()` and `write()`, and also to remove
weird behaviors caused by the way those fields were updated.
Assume we update a simple field on a record. This adds pending updates
for the field and `write_date`. However, the value of `write_date` is
not known yet: it will be updated as `NOW() AT TIME ZONE 'UTC'` in SQL.
So `write_date` is actually given a dummy value in pending updates, and
it is invalidated from cache, until its value is flushed to the database
and fetched again.
Now assume we access another field on the record, and that field is not
in cache. The prefetching mechanism will read all column fields,
including `write_date`, and flush them first.
# this adds pending updates foo: 42, write_uid: 1, write_date: False
record.foo = 42
# assume 'bar' is not in cache; this prefetches all column fields,
# which flushes the pending updates above before reading them back
result = record.bar
We can avoid flushing pending updates if the values read from database
do not overwrite existing values in cache. If you assume that the value
of a pending update is in cache (in the example, `foo: 42`), you don't
need to flush the corresponding field. Indeed, the value of `foo` will
remain 42 in cache, whatever its value in the database. This assumption
(pending updates are in cache) is true for all fields *except* for
`write_date`: it is invalidated from cache, and given a dummy value in
pending updates. This branch actually makes this assumption true for
all fields. The avoidance of flushing pending updates will be done in
another commit.
In order to directly assign `write_date` its value, we use a cache for
the value `NOW() AT TIME ZONE 'UTC'` from the database. This costs at
most one query per transaction, and potentially saves a few queries.
Co-authored-by:
Victor Feyens <vfe@odoo.com>
Showing
- addons/hr_holidays/tests/test_company_leave.py 1 addition, 1 deletionaddons/hr_holidays/tests/test_company_leave.py
- addons/test_mail/tests/test_performance.py 16 additions, 16 deletionsaddons/test_mail/tests/test_performance.py
- addons/test_mass_mailing/tests/test_performance.py 2 additions, 2 deletionsaddons/test_mass_mailing/tests/test_performance.py
- odoo/addons/test_assetsbundle/tests/test_assetsbundle.py 2 additions, 1 deletionodoo/addons/test_assetsbundle/tests/test_assetsbundle.py
- odoo/addons/test_new_api/tests/test_new_fields.py 2 additions, 2 deletionsodoo/addons/test_new_api/tests/test_new_fields.py
- odoo/addons/test_performance/tests/test_performance.py 7 additions, 7 deletionsodoo/addons/test_performance/tests/test_performance.py
- odoo/models.py 22 additions, 32 deletionsodoo/models.py
- odoo/sql_db.py 10 additions, 0 deletionsodoo/sql_db.py
Loading
Please register or sign in to comment