Enable Dark Mode!
odoo-migration-manager.jpg
By: Sreelakshmi PM

Odoo Migration Manager

Technical

In this article, we are going to discuss the MigrationManager class in Odoo. This class manages the migration of modules. Data migration is the process of keeping the correct data in the database after updating to a new module version. For example, In the new module version, we changed some existing field types, and to keep that existing field data from being lost, we need to write migration scripts to preserve that data with the new field changes. So Odoo migration manager will manage these migration scripts.

Migration files are arranged in a directory tree structure. There need to be the main folder  "migrations" with subfolders for each module version. The version is either the module version or the server.module version. Each subfolder has multiple Python files. Python filenames must start with "pre-", "post", or "end-". The execution order of Python files is determined by their names. All Python files with names starting with 'pre-' are executed before the module is initialized, and filenames starting with 'post-' are executed after the module is initialized. The "end-" scripts are executed after all modules have been updated. A special folder called "0.0.0" contains a script that runs on any version change. The scripts that need to be executed in every version change can be kept under this folder. In the pre-stage, these folders' scripts are run first, while in the post and end-stage, these scripts will run last.

odoo-migration-manager-cybrosys

The python files should contain a function called migrate. This function has two arguments, the database cursor and the currently installed version.

Execution:

Migration scripts do not need to be registered anywhere. Odoo searches for migration scripts based on the updated version of the module. When updating a module, if a migrations folder exists, Odoo will look inside the migrations folder for folders with versions equal to or in between the version to be updated. This happens before any other files are processed. If this folder exists, the "pre-" migrations scripts are executed. These scripts will run before any other files are processed, so there are no changes in the database at this stage.

After executing all pre-migration scripts, Odoo will update the module, and the changes will be applied to the database. After the module upgrade, the database may differ from previous versions. Once the module was updated, Odoo will search for the post-migration scripts based on the version, and all the post-migration scripts of the related version will be executed.

The end of the migration script will be executed after all modules have been updated.

Migration updates are not rolled back if an error occurs later during the engine update process. So always try to update your module with a copy migration script first.

Let’s check with an example.

In my custom module, I am having a model ‘material.requisition,’ and there will be a percentage field. Currently, the field type is ‘integer’. I want to change the field type to float. It will be an easy task, only I need to change the field definition in the python file, but currently, there is some data in the percentage field, and I want to preserve that data, so I need to change the field type without losing the current value in that field. Currently, when I change the field type, all the existing data of that field will be lost. By writing a migration script, we can preserve that data. Here we only need integer data to float the data type.

odoo-migration-manager-cybrosys

This will be my module structure,

odoo-migration-manager-cybrosys

I have added a migrations folder and created a subfolder with the corresponding module version (the module version I am updating for).

In the pre-migration file,

There should be a ‘migrate’ function. Inside the migrate function, we are implementing the logic.

# -*- coding: utf-8 -*-

def migrate(cr, version):
   """
   Creating a temporary field to store the percentage data
   """
   cr.execute('ALTER TABLE material_requisition ADD COLUMN temp_percentage float')
   cr.execute('UPDATE material_requisition SET temp_percentage=percentage')

In the pre-migration script, we are storing the current values of the percentage field to a temporary field. After updating the module, the data of the percentage field will be lost.

In the post-migration file,

# -*- coding: utf-8 -*-
def migrate(cr, version):
   """
   percentage data from temporary field stored to the original percentage field
   temporary field removed from the table
   """
   cr.execute('UPDATE material_requisition SET percentage=temp_percentage')
   cr.execute('ALTER TABLE material_requisition DROP COLUMN temp_percentage')

Here we are storing temporary field data back to the updated percentage field, and the temporary field will be removed from the database.

So no data will be lost when we change the percentage field type.

In the manifest file, I have updated the new module version. Based on this version, the migration scripts will be searched inside the migrations folder.

odoo-migration-manager-cybrosys

This is all about the Odoo MigrationManager, and it will be helpful to prevent data loss while upgrading modules to a new version.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment

 


whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message