Enable Dark Mode!
how-to-migrate-owl-2-components-to-owl-3-in-odoo-20.jpg
By: Hafsana CA

How to Migrate OWL 2 Components to OWL 3 in Odoo 20

Technical Odoo 20 Owl

Odoo has provided some migration tools as well as a compatibility layer that helps in the migration process. Despite this, the use of the auto-migration tool alone is not enough to do automatic migration of the system. The developers need to check the process and get used to new patterns and native OWL 3.

Execute the OWL 3 Migration Script

First of all, it makes sense to execute the upgrade_code command in Odoo, as it automates source code changes whenever appropriate. It is better to create a separate Git branch for that as well as make a backup copy of the original files.

./odoo-bin upgrade_code \
    --addons-path=/path/to/odoo/addons,/path/to/custom/addons \
    --script owl3-migration

In order to initiate the migration process, it is necessary to clarify all the available choices with the help of the command ./odoo-bin upgrade_code --help, as well as make sure you have got the proper script for migration on hand, located in the Odoo source checkout. The script must run on a separate Git branch, leaving the existing codebase completely unaffected. If the script supports the dry-run option, then this option should be exercised, for, once again, it is extremely important to review the Git diff and check for any possible errors introduced by the upgrade process, including running tests for any upgraded modules.

Once you run the migration script, you should examine the Git diff and decide which modules need to be modified manually. It is not advisable to run the migration script on the production code base with just one copy of it.

Employing the Compatibility Layer in Migration

There are various methods provided by Odoo that help to make the migration process from Odoo 20 to Owl 3 effective, as a result of which some templates that were developed in Owl 2 become compatible with the new framework.

Developers should be prepared for the following differences:

  • t-portal: This directive is not supported in Owl 3 anymore. It is necessary to analyze the use of this directive and figure out how to substitute it.
  • t-model: This directive in the native Owl 3 framework depends on signals. Therefore, a simple replacement is not sufficient, and some changes in the component's state and related expressions might be required.
  • t-ref: In Owl 3, there could be signal references and resource references. Therefore, the templates and JavaScript using these references might require changes.
  • useEffect: The functionality and method of tracking dependencies of this hook have changed. Some parts of Odoo compatibility code use the useLayoutEffect hook to emulate the previous hook's functionality. However, this does not mean that it is an easy replacement.

Static Props and the Replacement of Default Props

In Owl 2, each component generally declares its properties using static props and its defaults using static defaultProps. However, in Owl 3, there are no such static declarations. This is because the responsibility of fetching required properties lies with the components themselves, using the useProps function and declaring types and defaults in the property schema.

The examples below show the difference between two ways of doing things.

class Greeting extends Component {
    static template = "my_module.Greeting";
    static props = {
        name: String,
        message: {
            type: String,
            optional: true,
        },
    };
    static defaultProps = {
        message: "Welcome",
    };
}

But in the new version of the Owl framework, the component would look something like the following:

import { Component, useProps, t } from "@odoo/owl";
class Greeting extends Component {
    static template = "my_module.Greeting";
    props = useProps({
        name: t.string(),
        message: t.string().optional("Welcome"),
    });
}

Transition Services to the Plugin System

Owl 3 introduces a completely new concept of using plugins as a way of getting common functionality. Depending on the current implementation, the migration will mean creating a plugin and passing it through the component tree.

This implies the fact that the process will require an architectural change and not just renaming. It is necessary to determine the purpose of the usage of services and environment variables, discover the most appropriate way of exposing them via plugins and, finally, upgrade all affected components. Where Odoo-specific services are concerned, there might be some additional work during the Owl migration.

Modifications to templates and their rendering tools

As far as migrating the templates from Owl 2 to Owl 3 is concerned, it is important to alter the scope of templates and directives. In some cases, it may be necessary to add the prefix “this” to be able to access the properties and methods of components.

The list of changes includes:

  • t-esc to t-out: Replace the deprecated directive t-esc with t-out.
  • t-slot to t-call-slot: Modify the slot as per the syntax of Owl 3.
  • t-ref: Implement what is known as the signal-based approach.
  • t-model: Alter the bindings using the signal-based approach.

In addition, one needs to compare his own mounting and rendering implementation with the official implementation provided by Odoo 20.

Finally, you should also test your templates against authentic data and user interaction.

Complete Transition to Native OWL3

Migration can be easily performed via the compatibility layer; however, it shouldn’t serve as a replacement for the native OWL 3 API for a prolonged period of time. After all modules have been migrated and tested, all unneeded compatibility code needs to be stripped out of the application.

Lastly, the last check needs to be performed in terms of the following issues:

States and signals in reactivity, as well as computed properties.

  • Props for the components and their default values.
  • Plugins' definition and dependency access.
  • Template directives used in your project, such as t-out, t-ref, t-model, t-call-slot, and similar.
  • Rendering and mounting of the components.
  • JavaScript and asset loading errors in the browser console.

Every component needs to be tested individually, and after that – test the entire flow, involving the components. It is also necessary to test lists, forms, dialogs, event handlers, and other widgets created by you in your module.

Odoo 20 has several interfaces to use, but each interface serves a different purpose, uses a different authentication mechanism, and gets different support. It is advised to choose the JSON-2 interface as a replacement for the deprecated remote procedure calls external model. A custom controller can be used when special integration is required.

Eventually they will realize that when talking about endpoints and all those things related to them (like available endpoints, API key security, least privilege policies and testing scenarios for migration), Odoo integration is easy to do in the right way.

To read more about How to Migrate Custom Modules from Odoo 19 to Odoo 20, refer to our blog, How to Migrate Custom Modules from Odoo 19 to Odoo 20.


Frequently Asked Questions

What are the reasons for the migration of Owl 2 modules in Odoo 20?

Odoo 20 is built on the Owl 3 foundation that leads to the modifications in the component properties, in their reactivity, in template directives, and in lifecycle and dependency access.

What is the use of the odoo-bin upgrade_code command?

The command performs the running of source code migration scripts; it allows applying some changes according to the specific rules, but further testing and review are required.

What option can you apply in Owl 3 to use it as an alternative to the static props and the static defaultProps?

The way to get component properties in Owl 3 is the use of useProps(), while the new schema of the property specifies data types and defaults for properties that work like static props/static defaultProps.

Why does Odoo need compatibility during the migration process?

The idea behind using a compatibility layer is maintaining compatibility with the current code that uses Owl 2 during the process of migration. It allows for gradual conversion of the modules but does not imply that it will work without changes since some of the implementation of the compatibility will have to be converted into Owl 3 code.

Is the migration process fully automatic?

Although certain types of changes can be auto-generated, not all changes will be done by the auto-generation process (like changes in reactivity, in rendering, in plugins, and in template scope).

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



0
Comments



Leave a comment



WhatsApp