Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 193 additions & 37 deletions server_environment/README.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.. image:: https://odoo-community.org/readme-banner-image
:target: https://odoo-community.org/get-involved?utm_source=readme
:alt: Odoo Community Association

======================================
server configuration environment files
======================================
Expand All @@ -17,7 +13,7 @@ server configuration environment files
.. |badge1| image:: https://img.shields.io/badge/maturity-Production%2FStable-green.png
:target: https://odoo-community.org/page/development-status
:alt: Production/Stable
.. |badge2| image:: https://img.shields.io/badge/license-LGPL--3-blue.png
.. |badge2| image:: https://img.shields.io/badge/licence-LGPL--3-blue.png
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--env-lightgray.png?logo=github
Expand Down Expand Up @@ -100,19 +96,19 @@ You can edit the settings you need in the ``server_environment_files``
addon. The ``server_environment_files_sample`` can be used as an
example:

- values common to all / most environments can be stored in the
``default/`` directory using the .ini file syntax;
- each environment you need to define is stored in its own directory and
can override or extend default values;
- you can override or extend values in the main configuration file of
your instance;
- In some platforms (like odoo.sh where production config file is copied
to staging) it can be useful to overwrite options written in the
``[options]`` section. You must allow the override by adding
``server_environment_allow_overwrite_options_section = True`` to the
former ``odoo.cfg`` config file or through the environment variable:
``export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True`` (if
both are set config file takes precedence).
- values common to all / most environments can be stored in the
``default/`` directory using the .ini file syntax;
- each environment you need to define is stored in its own directory
and can override or extend default values;
- you can override or extend values in the main configuration file of
your instance;
- In some platforms (like odoo.sh where production config file is
copied to staging) it can be useful to overwrite options written in
the ``[options]`` section. You must allow the override by adding
``server_environment_allow_overwrite_options_section = True`` to the
former ``odoo.cfg`` config file or through the environment variable:
``export SERVER_ENVIRONMENT_ALLOW_OVERWRITE_OPTIONS_SECTION=True``
(if both are set config file takes precedence).

Environment variable
--------------------
Expand Down Expand Up @@ -221,16 +217,176 @@ If you want to have a technical name to reference:

[...]

Restoring columns on uninstall
------------------------------

When ``server.env.mixin`` is bound to an existing model, the ORM drops
the original stored columns for all env-managed fields. If the binding
addon is later uninstalled, those columns must be recreated so the
database remains usable.

Add an ``uninstall_hook`` to your addon and delegate to
``restore_env_managed_columns``:

::

# your_addon/__init__.py
from . import models

def uninstall_hook(env):
env["server.env.mixin"].restore_env_managed_columns(
"storage.backend",
["directory_path", "other_field"],
)

# your_addon/__manifest__.py
{
...
"uninstall_hook": "uninstall_hook",
}

The helper creates any missing columns (idempotent: safe to call
multiple times) and repopulates them with each record's current
effective value — whether that value came from an environment
configuration file or from the stored default field
(``x_<field>_env_default``).

The hook must run *before* the ORM extensions are removed, which is
guaranteed by Odoo's uninstall sequence (hooks execute before
``Module.module_uninstall()``).

Handling required fields
~~~~~~~~~~~~~~~~~~~~~~~~

If a restored column is **required** (has a ``NOT NULL`` constraint) but
has no effective value (missing from environment config and no default
field set), the restoration will fail with a ``UserError``.

**Solution:** pass a ``field_defaults`` dictionary with fallback values:

::

def uninstall_hook(env):
env["server.env.mixin"].restore_env_managed_columns(
"ir.mail_server",
["smtp_host", "smtp_authentication"],
field_defaults={
"smtp_authentication": "login", # fallback for required field
},
)

The helper will use the fallback value if provided and the computed
field value is empty. If no fallback is provided but a required field
has no value, a ``UserError`` is raised with instructions on how to
provide a ``field_defaults`` parameter.

Migrating when dropping server_environment dependency
-----------------------------------------------------

When refactoring an existing addon that embeds a ``server.env.mixin``
binding, you may want to extract the binding into a separate *glue*
addon and drop the ``server_environment`` dependency from the original.
This keeps the base addon lightweight while preserving
server-environment features for those who install the glue addon.

**Pattern:**

- **Original addon (v1)**: depends on ``server_environment`` and binds
the mixin directly in model code.
- **Refactored addon (v2)**: removes ``server_environment`` from
dependencies, removes the mixin binding and the related ORM model
inheritance.
- **New glue addon** (optional, same version): depends on both
``server_environment`` and the original addon v2; re-adds the mixin
binding in a separate module file.

**Migration checklist:**

1. In the **original addon's v2 ``__manifest__.py``**:

- Remove ``"server_environment"`` from ``depends``.
- Remove the model file(s) that contained the mixin binding.
- Update ``depends`` to add the new glue addon *if* the base addon
still needs it (otherwise, make the glue addon optional for users
who want env-binding).

2. In the **original addon's v2 model code**:

- Delete or simplify the model class that inherited from
``server.env.mixin``.
- If the model was only there for the binding, remove it entirely.
- Restore the original field definitions (not as computed fields).

3. **Create a migration script** (if needed) to restore columns *during
the addon upgrade*, before the ORM model extensions are unloaded. Use
a ``@post_load`` hook or a dedicated migration script:

::

# migrations/18.0.1.0.0/post-restore-columns.py
def migrate(cr, version):
# Call the restoration logic while the v1 model is still active
env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
# If any field is required and may have no value in the environment,
# provide a fallback via field_defaults
env["server.env.mixin"].restore_env_managed_columns(
"storage.backend",
["directory_path", "other_field"],
field_defaults={
"directory_path": "/tmp", # fallback for required field
},
)

4. **Create the glue addon** with the model re-inheritance:

::

# your_addon_env/__init__.py
from . import models

# your_addon_env/models/__init__.py
from . import storage_backend

# your_addon_env/models/storage_backend.py
class StorageBackend(models.Model):
_name = "storage.backend"
_inherit = ["storage.backend", "server.env.mixin"]

@property
def _server_env_fields(self):
return {"directory_path": {}}

# your_addon_env/__manifest__.py
{
"name": "Storage Backend – Server Environment",
"version": "18.0.1.0.0",
"depends": ["server_environment", "storage_backend"],
"installable": True,
}

**Key points:**

- Column restoration must happen *during the addon upgrade* (step 3),
not as an uninstall hook, because the original model binding is still
active.
- The ``restore_env_managed_columns`` helper is idempotent and safe to
call even if columns already exist.
- Users who do not need server environment features simply do *not*
install the glue addon—the base addon continues to work with plain
database columns.
- Users who do need server environment can install both the base addon
(v2+) and the glue addon (same version) to get the binding back.

Known issues / Roadmap
======================

- it is not possible to set the environment from the command line. A
configuration file must be used.
- the module does not allow to set low level attributes such as database
server, etc.
- server.env.techname.mixin's tech_name field could leverage the new
option for computable / writable fields and get rid of some onchange /
read / write code.
- it is not possible to set the environment from the command line. A
configuration file must be used.
- the module does not allow to set low level attributes such as
database server, etc.
- server.env.techname.mixin's tech_name field could leverage the new
option for computable / writable fields and get rid of some onchange
/ read / write code.

Bug Tracker
===========
Expand All @@ -253,18 +409,18 @@ Authors
Contributors
------------

- Florent Xicluna (Wingo) <florent.xicluna@gmail.com>
- Nicolas Bessi <nicolas.bessi@camptocamp.com>
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
- Daniel Reis <dgreis@sapo.pt>
- Holger Brunn <hbrunn@therp.nl>
- Leonardo Pistone <leonardo.pistone@camptocamp.com>
- Adrien Peiffer <adrien.peiffer@acsone.com>
- Thierry Ducrest <thierry.ducrest@camptocamp.com>
- Guewen Baconnier <guewen.baconnier@camptocamp.com>
- Thomas Binfeld <thomas.binsfeld@acsone.eu>
- Stéphane Bidoul <stefane.bidoul@acsone.com>
- Simone Orsi <simahawk@gmail.com>
- Florent Xicluna (Wingo) <florent.xicluna@gmail.com>
- Nicolas Bessi <nicolas.bessi@camptocamp.com>
- Alexandre Fayolle <alexandre.fayolle@camptocamp.com>
- Daniel Reis <dgreis@sapo.pt>
- Holger Brunn <hbrunn@therp.nl>
- Leonardo Pistone <leonardo.pistone@camptocamp.com>
- Adrien Peiffer <adrien.peiffer@acsone.com>
- Thierry Ducrest <thierry.ducrest@camptocamp.com>
- Guewen Baconnier <guewen.baconnier@camptocamp.com>
- Thomas Binfeld <thomas.binsfeld@acsone.eu>
- Stéphane Bidoul <stefane.bidoul@acsone.com>
- Simone Orsi <simahawk@gmail.com>

Maintainers
-----------
Expand Down
94 changes: 93 additions & 1 deletion server_environment/models/server_env_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from lxml import etree

from odoo import api, fields, models
from odoo.tools import mute_logger
from odoo.tools import SQL, mute_logger, sql

from odoo.addons.base_sparse_field.models.fields import Serialized

Expand Down Expand Up @@ -428,3 +428,95 @@ def _setup_base(self):
self._server_env_transform_field_to_read_from_env(field)
self._server_env_add_is_editable_field(field)
return

@api.model
def restore_env_managed_columns(self, model_name, field_names, field_defaults=None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as it only needs a cursor I would move this to a utils.py function

"""Restore database columns for fields formerly managed via server.env.mixin.

When an addon binds ``server.env.mixin`` to an existing model, the ORM
drops the original stored columns. Call this helper from an
``uninstall_hook`` so those columns are recreated and repopulated
with their current effective values before the addon is removed.

The hook must run *while* the module's ORM extensions are still active
(guaranteed by Odoo's uninstall sequence: hooks execute before
``Module.module_uninstall()``), so the env-computed fields are still
readable and their values can be written back to freshly created columns.

The operation is idempotent: calling it multiple times will not fail.

**Defaults:** If a restored field value is NULL/empty, the helper will
use the fallback from ``field_defaults`` (if provided) or the field's
ORM-level default (if defined).

Note: ``field.required`` is set to False by the mixin, so we cannot detect
which fields are required. Provide explicit ``field_defaults`` for fields
that must have values.

:param str model_name: dotted model name, e.g. ``"ir.mail_server"``
:param field_names: iterable of field names whose columns to restore
:param dict field_defaults: optional mapping of field name to fallback
value used when restoring a column that has no effective env-computed
value, e.g. ``{"smtp_authentication": "<login>"}``
"""
model = self.env[model_name]
cr = self.env.cr
field_defaults = field_defaults or {}

for field_name in field_names:
field = model._fields.get(field_name)
if field is None:
_logger.warning(
"restore_env_managed_columns: field %r not found on %s, skipping",
field_name,
model_name,
)
continue
column_type = field.column_type
if column_type is None:
_logger.warning(
"restore_env_managed_columns: "
"field %r on %s has no SQL column type, skipping",
field_name,
model_name,
)
continue
table = model._table
if not sql.column_exists(cr, table, field_name):
sql.create_column(cr, table, field_name, column_type[1], field.string)
_logger.info(
"restore_env_managed_columns: created column %s.%s (%s)",
table,
field_name,
column_type[1],
)
# Repopulate every existing record with the current computed value.
# The hook runs while the ORM extensions are still active, so the
# env-computed field is still readable via the normal accessor.
for record in model.search([]):
value = record[field_name]
# The ORM returns False for NULL on non-boolean fields; map
# that back to None so psycopg2 writes a proper SQL NULL.
if value is False and field.type != "boolean":
value = None
elif value == "":
value = None

# Try to get a default value if we have None.
# Note: field.required is False after mixin transformation,
# so we apply defaults for all None values when available.
if value is None:
if field_name in field_defaults:
value = field_defaults[field_name]
elif field_name in model.default_get([field_name]):
value = model.default_get([field_name])[field_name]

cr.execute(
SQL(
"UPDATE %s SET %s = %s WHERE id = %s",
SQL.identifier(table),
SQL.identifier(field_name),
value,
record.id,
)
)
Loading
Loading