Skip to content

Commit 65f05d6

Browse files
committed
[IMP] server_environment: Preserve not env managed data
Helper function typically used for hooks and migration scripts. Restores database values for fields transitioning to 'server env managed'. When a field is defined as managed by the server environment, Odoo ignores the value stored in the database, prioritizing the environment configuration instead. If no environment configuration exists, the field may effectively lose its previous value. This method forces to 'persist' these values if they are not explicitly overridden by the current environment configuration.
1 parent 5a2f8a2 commit 65f05d6

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

server_environment/models/server_env_mixin.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,33 @@ def _setup_base(self):
428428
self._server_env_transform_field_to_read_from_env(field)
429429
self._server_env_add_is_editable_field(field)
430430
return
431+
432+
@api.model
433+
def preserve_not_env_managed_data(self):
434+
"""
435+
Helper function typically used for hooks and migration scripts.
436+
Restores database values for fields transitioning to 'server env managed'.
437+
438+
When a field is defined as managed by the server environment, Odoo
439+
ignores the value stored in the database, prioritizing the environment
440+
configuration instead. If no environment configuration exists, the field
441+
may effectively lose its previous value.
442+
443+
This method forces to 'persist' these values if they are not
444+
explicitly overridden by the current environment configuration.
445+
"""
446+
self.env.cr.execute(f"SELECT * FROM {self._table}")
447+
for row in self.env.cr.dictfetchall():
448+
record = (
449+
self.env[self._name]
450+
.with_context(active_test=False)
451+
.search([("id", "=", row["id"])])
452+
)
453+
if record:
454+
record_values = {}
455+
for field_name, _options in self.env[
456+
self._name.replace(".", "_")
457+
]._server_env_fields.items():
458+
if field_name in row:
459+
record_values[field_name] = row[field_name]
460+
record.update(record_values)

0 commit comments

Comments
 (0)