Skip to content

Commit d51340a

Browse files
committed
[ADD] Upgrade documentation: Upgrade scripts and Util package
1 parent f3b40b5 commit d51340a

File tree

4 files changed

+488
-19
lines changed

4 files changed

+488
-19
lines changed

content/developer/howtos/upgrade_custom_db.rst

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1+
:show-content:
2+
13
=============================
24
Upgrade a customized database
35
=============================
46

7+
.. toctree::
8+
:titlesonly:
9+
:glob:
10+
11+
upgrade_custom_db/*
12+
513
Upgrading to a new version of Odoo can be challenging, especially if the database you work on
614
contains custom modules. This page intent is to explain the technical process of upgrading a
715
database with customized modules. Refer to :doc:`Upgrade documentation </administration/upgrade>`
@@ -33,7 +41,6 @@ These are the steps to follow to upgrade customized databases:
3341
#. :ref:`Test extensively and do a rehearsal <upgrade_custom/testing_rehearsal>`.
3442
#. :ref:`Upgrade the production database <upgrade_custom/production>`.
3543

36-
3744
.. _upgrade_custom/stop_developments:
3845

3946
Step 1: Stop the developments
@@ -47,14 +54,13 @@ Needless to say, bug fixing is exempt from this recommendation.
4754
Once you have stopped development, it is a good practice to assess the developments made and compare
4855
them with the features introduced between your current version and the version you are targeting.
4956
Challenge the developments as much as possible and find functional workarounds. Removing redundancy
50-
between your developments and the standard version of Odoo will lead to an eased
51-
upgrade process and reduce technical debt.
57+
between your developments and the standard version of Odoo will lead to an eased upgrade process
58+
and reduce technical debt.
5259

5360
.. note::
5461
You can find information on the changes between versions in the `Release Notes
5562
<https:/odoo.com/page/release-notes>`_.
5663

57-
5864
.. _upgrade_custom/request_upgrade:
5965

6066
Step 2: Request an upgraded database
@@ -71,7 +77,6 @@ properly. If that's not the case, and the upgrade request fails, request the ass
7177
the `support page <https://odoo.com/help?stage=migration>`_ by selecting the option related to
7278
testing the upgrade.
7379

74-
7580
.. _upgrade_custom/empty_database:
7681

7782
Step 3: Empty database
@@ -85,7 +90,7 @@ features, and guarantees that they will not cause any issues when upgrading the
8590
Making the custom modules work in an empty database also helps avoid changes and wrong
8691
configurations that might be present in the production database (like studio customization,
8792
customized website pages, email templates or translations). They are not intrinsically related to
88-
the custom modules and that can raise unwanted issues in this stage of the upgraded process.
93+
the custom modules and that can raise unwanted issues in this stage of the upgrade process.
8994

9095
To make custom modules work on an empty database we advise to follow these steps:
9196

@@ -190,22 +195,48 @@ To make sure the custom code is working flawlessly in the new version, follow th
190195
Migrate the data
191196
----------------
192197

193-
During the upgrade of the custom modules, you might have to use migration scripts to reflect changes
194-
from the source code to their corresponding data.
198+
During the upgrade of the custom modules, you might have to use
199+
:doc:`upgrade scripts <upgrade_custom_db/upgrade_scripts>` to reflect changes from the source code
200+
to their corresponding data. Together with the upgrade scripts, you can also make use of the
201+
:doc:`../reference/upgrade_util` and its helper functions.
195202

196203
- Any technical data that was renamed during the upgrade of the custom code (models, fields,
197-
external identifiers) should be renamed using migration scripts to avoid data loss during the
198-
module upgrade.
204+
external identifiers) should be renamed using upgrade scripts to avoid data loss during the
205+
module upgrade. See also: :meth:`rename_field`, :meth:`rename_model`, :meth:`rename_xmlid`.
199206
- Data from standard models removed from the source code of the newer Odoo version and from the
200207
database during the standard upgrade process might need to be recovered from the old model table
201208
if it is still present.
202209

203-
Migration scripts can also be used to:
210+
.. example::
211+
Custom fields for model ``sale.subscription`` are not automatically migrated from Odoo 15 to
212+
Odoo 16 (when the model was merged into ``sale.order``). In this case, a SQL query can be
213+
executed on an upgrade script to move the data from one table to the other. Take into account
214+
that all columns/fields must already exist, so consider doing this in a ``post-`` script (See
215+
:ref:`upgrade/upgrade-scripts/phases`).
216+
217+
.. spoiler::
218+
219+
.. code-block:: python
220+
221+
def migrate(cr, version):
222+
cr.execute(
223+
"""
224+
UPDATE sale_order so
225+
SET custom_field = ss.custom_field
226+
FROM sale_subscription ss
227+
WHERE ss.new_sale_order_id = so.id
228+
"""
229+
)
230+
231+
Check the documentation for more information on :doc:`upgrade_custom_db/upgrade_scripts`.
232+
233+
Upgrade scripts can also be used to:
204234

205235
- Ease the processing time of an upgrade. For example, to store the value of computed stored fields
206236
on models with an excessive number of records by using SQL queries.
207-
- Recompute fields in case the computation of their value has changed.
208-
- Uninstall unwanted custom modules.
237+
- Recompute fields in case the computation of their value has changed. See also
238+
:meth:`recompute_fields`.
239+
- Uninstall unwanted custom modules. See also :meth:`remove_module`.
209240
- Correct faulty data or wrong configurations.
210241

211242
.. _upgrade_custom/upgraded_database/test_custom:
@@ -221,13 +252,12 @@ Things to pay attention to:
221252

222253
- Views not working: During the upgrade, if a view causes issues because of its content, it gets
223254
disabled. You can find the information on disabled views on the :ref:`Upgrade report
224-
<upgrade/upgrade_report>`. This view needs to be activated again. To achieve this, we recommend
225-
the use of migration scripts.
255+
<upgrade/upgrade_report>`. This view needs to be activated again (or removed if not useful anymore).
256+
To achieve this, we recommend the use of upgrade scripts.
226257
- :doc:`Module data <../tutorials/define_module_data>` not updated: Custom records that have the
227258
``noupdate`` flag are not updated when upgrading the module in the new database. For the custom
228-
data that needs to be updated due to changes in the new version, we recommend to use migration
229-
scripts to do so.
230-
259+
data that needs to be updated due to changes in the new version, we recommend to use upgrade
260+
scripts to do so. See also: :meth:`update_record_from_xml`.
231261

232262
.. _upgrade_custom/testing_rehearsal:
233263

@@ -247,7 +277,6 @@ In addition to that, make a full rehearsal of the upgrade process the day before
247277
production database to avoid undesired behavior during the upgrade and to detect any issue that
248278
might have occurred with the migrated data.
249279

250-
251280
.. _upgrade_custom/production:
252281

253282
Step 6: Production upgrade
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
===============
2+
Upgrade scripts
3+
===============
4+
5+
An upgrade script is a Python file containing a function called :meth:`migrate`, which the upgrade
6+
process invokes during the update of a module.
7+
8+
.. method:: migrate(cr, version)
9+
10+
:param cr: current database cursor
11+
:type cr: :class:`~odoo.sql_db.Cursor`
12+
:param str version: installed version of the module
13+
14+
Typically, this function executes one or multiple SQL queries and can also access Odoo's ORM, as
15+
well as the :doc:`../../reference/upgrade_util`.
16+
17+
As described in :ref:`upgrade_custom/upgraded_database/migrate_data`, you might have to use upgrade
18+
scripts to reflect changes from the source code to their corresponding data.
19+
20+
21+
Writing upgrade scripts
22+
=======================
23+
24+
To write what we refer as a **custom upgrade script**, the path of the file should follow this
25+
template: :file:`<module_name>/migrations/<major_version>.<minor_version>/{pre|post|end}-*.py`.
26+
27+
Follow this steps to create the directory's structure and the Python file:
28+
29+
#. Create a :file:`migrations` directory inside the custom module.
30+
#. Create a :file:`<version>` directory with the format: :file:`<major_version>.<minor_version>`
31+
inside the :file:`migrations` directory.
32+
33+
- :file:`<major_version>` corresponds to the major version of Odoo (e.g., :file:`16.0` for
34+
Odoo 16).
35+
- :file:`<minor_version>` corresponds to the updated version declared on the module's
36+
manifest.
37+
38+
Upgrade scripts are only executed when the module is being updated. Therefore, the
39+
:file:`minor_version` set in the directory needs to be higher than the module's installed
40+
version and equal or lower to the updated version of the module.
41+
42+
.. example::
43+
44+
In an Odoo 16 database, with a custom module installed in version `1.1` that has been
45+
updated to version `1.2`; the version directory should be `16.0.1.2`.
46+
47+
#. Create a Python file inside the :file:`<version>` directory named according to the
48+
:ref:`Phase <upgrade/upgrade-scripts/phases>` on which it needs to be executed. It should
49+
follow the template `{pre|post|end}-*.py`. The file name will determine the order in which it
50+
is executed for that module, version and phase.
51+
#. Create a :meth:`migrate` function in the Python file.
52+
#. Add the logic needed inside the function.
53+
54+
.. example::
55+
56+
Directory structure of an upgrade script for a custom module named `awesome_partner` upgraded
57+
to version `2.0.0` on Odoo 17
58+
59+
.. code-block:: text
60+
61+
awesome_partner/
62+
|-- migrations/
63+
| |-- 17.0.2.0.0/
64+
| | |-- pre-exclamation.py
65+
66+
Two upgrade scripts examples with the content of the :file:`pre-exclamation.py`, file adding
67+
"!" at the end of partners' names:
68+
69+
.. code-block:: python
70+
71+
import logging
72+
73+
_logger = logging.getLogger(__name__)
74+
75+
76+
def migrate(cr, version):
77+
cr.execute("UPDATE res_partner SET name = name || '!'")
78+
_logger.info("Updated %s partners", cr.rowcount)
79+
80+
.. code-block:: python
81+
82+
import logging
83+
from odoo.upgrade import util
84+
85+
_logger = logging.getLogger(__name__)
86+
87+
88+
def migrate(cr, version):
89+
env = util.env(cr)
90+
91+
partners = env["res.partner"].search([])
92+
for partner in partners:
93+
partner.name += "!"
94+
95+
_logger.info("Updated %s partners", len(partners))
96+
97+
Note that in the second example, the script takes advantage of the
98+
:doc:`../../reference/upgrade_util` to access the ORM. Check the documentation to find out
99+
more about this library.
100+
101+
102+
Running and testing upgrade scripts
103+
===================================
104+
105+
.. tabs::
106+
107+
.. group-tab:: Odoo Online
108+
109+
As the instalation of custom modules containing Python files is not allowed on Odoo Online
110+
databases, it is not possible to run upgrade scripts on this platform.
111+
112+
.. group-tab:: Odoo.sh
113+
114+
As explained on the `Odoo.sh` tab of :ref:`upgrade/request-test-database`, Odoo.sh is
115+
integrated with the upgrade platform.
116+
117+
Once the upgrade of a staging branch is on "Update on commit" mode, each time a commit is
118+
pushed on the branch, the upgraded backup is restored and all the custom modules are updated.
119+
This update includes the execution of the upgrade scripts.
120+
121+
When upgrading the production database, the execution of the upgrade scripts is also part of
122+
the update of the custom modules done by the platform when the upgraded database is restored.
123+
124+
.. group-tab:: On-premise
125+
126+
Once you receive the upgraded dump of the database from the `Upgrade platform
127+
<https://upgrade.odoo.com>`_, deploy the database and update all the custom modules by
128+
invoking the command :doc:`odoo-bin </developer/reference/cli>` in the shell.
129+
To update the custom modules, use the option: `-u <modules>,
130+
--update <modules>`.
131+
132+
.. important::
133+
As mentioned in the :doc:`CLI documentation </developer/reference/cli>`, the command used
134+
to call the CLI depends on how you installed Odoo.
135+
136+
137+
.. _upgrade/upgrade-scripts/phases:
138+
139+
Phases of upgrade scripts
140+
=========================
141+
142+
The upgrade process consists of three phases for each version of each module:
143+
144+
#. The pre-phase, before the module is loaded.
145+
#. The post-phase, after the module and its dependencies are loaded and upgraded.
146+
#. The end-phase, after all modules have been loaded and upgraded for that version.
147+
148+
Upgrade scripts are grouped according to the first part of their filenames into the corresponding
149+
phase. Within each phase, the files are executed according to their lexical order.
150+
151+
.. tip::
152+
If you are unsure which phase to use, use the end-phase.
153+
154+
.. admonition:: Execution order of example scripts for one module in one version
155+
156+
#. :file:`pre-10-do_something.py`
157+
#. :file:`pre-20-something_else.py`
158+
#. :file:`post-do_something.py`
159+
#. :file:`post-something.py`
160+
#. :file:`end-01-migrate.py`
161+
#. :file:`end-migrate.py`

content/developer/reference.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ Reference
1212
reference/user_interface
1313
reference/standard_modules
1414
reference/cli
15+
reference/upgrade_util
1516
reference/external_api
1617
reference/extract_api

0 commit comments

Comments
 (0)