Update the data Migration scripts - odoo-ps/psbe-process GitHub Wiki

Update the data - Migration scripts

Migration scripts allow you to modify the data in the database. You can use them in three different moments of the module updated:

  • BEFORE the module is updated (pre)
  • AFTER the module is updated (post)
  • AT THE END of the update of all the modules (end)

  1. Custom migration scripts
  2. Util and Custom-util
  3. Upgrade Specific scripts
  4. Fake Install a module

Custom migration scripts

Steps to use migrations scripts on your module:

  • Create a migrations folder inside your module.
  • Add a folder per version of update. The version on this folder should be higher than the current version on the database and lower or equal to the manifest version of the module.
    Recommendation: use the same version as the manifest.
    The folder version has to contain the Odoo version. For example: 15.0.1.0.0 for Odoo version 15.0 and manifest version 15.0.1.2.0.
  • Add Python files prefixed by pre, post, end according to your need.
  • Python files are executed in alphabetical order. (E.g.: pre-10-account-move.py, pre-20-account-move.py, post-10-partner.py, end-sale.py).

IMPORTANT: Migration scripts only run when the module is being updated to the manifest version.

  • If the module is already in that version, updating it won't run migration scripts.
  • If the module is being installed, migration scripts won't run. To run migration scripts on a new module, the suggested way is to Fake Install the module.

For performance concerns it's recommended to update data using SQL queries (when possible) instead of Odoo framework.
Clean sample page.
Doc for RD scripts.

Util and Custom-util

Util is used by Odoo standard upgrade scripts, it contains useful functions to alter data related to fields, models, records. Custom-util was created by the Upgrade Team to deal with frequent issues or repetitive tasks found through different upgrades. It contains functions related to views manpulation, fields, models, modules, and more.

To use util and custom-util in your migration scripts you have to:

Locally:

  • Clone both repos in your computer
  • When running Odoo add --upgrade-path=<PATH_TO_UTIL_REPO>/migrations,<PATH_TO_CUSTOM_UTIL_REPO>/migrations

SH:
We have created util_package to be used for upgrades on SH. You can add it to your project in two different ways:

  • Odev:
    • Run odev command prepare-util, it'll do all the job for you.:
      cd <psbe-client-repo>
      odev prepare-util <PSBE-SH-PROJECT-NAME>
  • Manually:
    • Add util_package as a submodule of the project.
      git submodule add [email protected]:odoo-ps/util_package.git
    • Add a requirements.txt file on the project that has a file:/home/odoo/src/user/util_package line in it.
    • Commit and push to the branch.
    • Add the util_package repo as a submodule on the project SH settings
    • Add deploy keys for the SH project. The name should be the name of the customer repo.

Upgrade Specific scripts

An upgrade-specific script is a script that runs together with Odoo standard upgrade scripts.
Upgrade Specific scripts are located at https://github.com/odoo/upgrade-specific/

The steps to create a specific script are:

  • Create a folder named as the customer subscription code inside the scripts folder of the repo.
    Inside that customer specific folder, it works as a normal standard migration script folder.
  • In the customer folder, create a folder for the module you want to create the script.
  • In the module folder, create a folder for the version where you want the upgrade specific script to run.
  • Inside the version folder, create the upgrade specific script (pre-post-end according to your need).

The structure is something like: /scripts/<subscription_code>/<module>/<version>/<script>.py
For example: /scripts/M0123456789/account/15.0.0.0/pre--10-fix-account.py
NOTE: Use pre-- and post-~|end-~ prefixes to sequence correctly with the generic scripts.

Special scripts:

  • Scripts in a folder version named 0.0.0 run every time the module is upgraded.
  • Scripts named init- run before everything else and are able to execute (only) SQL statements. You need to define 'prepare_migration' instead of 'migrate'. They are useful when standard upgrade scripts fail before any of scripts of the (standard) modules run.

IMPORTANT: Only the scripts on the master branch of the upgrade specific repo run when upgrading the customer's database.

More information about specific scripts here

Fake Install a module

To fake install a module, create an Upgrade Specific script to execute the following SQL query on the database:

INSERT INTO ir_module_module(name, latest_version, state)
VALUES ('your_module_name', 'module_version', 'to upgrade')

The module_version has to be lower than the manifest version, for migration scripts to run. For example, if the manifest version is 15.0.1.0.0, the latest_version on the insert query could be 15.0.0.0.1.

The Upgrade Specific script is recommended to be run on the base module at the end of the upgrade. For example, if you're dealing with an upgrade to v15, create an end script on a directory structure like /<subscription_code>/base/15.0.1.3/<end-script>.py

⚠️ **GitHub.com Fallback** ⚠️