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