Odoo OpenUpgrade Library

3.12.0 · active · verified Thu Apr 16

The `openupgradelib` is a Python library providing support functions specifically designed to be called from Odoo migration scripts. It is a fundamental component of the Odoo Community Association (OCA) OpenUpgrade project, which aims to offer an open-source upgrade path for Odoo Community Edition instances between major versions. The library is actively maintained, releasing new versions periodically to ensure compatibility with the latest Odoo versions and to address common data migration challenges.

Common errors

Warnings

Install

Imports

Quickstart

To use `openupgradelib`, create a Python file within your Odoo module's `migrations/<ODOO_VERSION>/` directory (e.g., `my_module/migrations/16.0.1.0/pre-migrate.py`). Decorate your `migrate` function with `@openupgrade.migrate()` to ensure it runs correctly within the OpenUpgrade framework, providing access to the `env` (Odoo environment) and `version` (migration version) objects. This example demonstrates running a logged SQL query and adding a new column, two common migration tasks.

import logging
from openupgradelib import openupgrade

_logger = logging.getLogger(__name__)

@openupgrade.migrate()
def migrate(env, version):
    _logger.info("Starting migration script for version %s", version)

    # Example 1: Execute a raw SQL query with logging
    openupgrade.logged_query(
        env.cr,
        "UPDATE res_users SET company_id = 1 WHERE company_id IS NULL",
        "Setting default company for users without one"
    )

    # Example 2: Add a new column to an existing model
    # This is typically done in a 'pre-migrate' script to prepare the database schema
    openupgrade.add_columns(
        env,
        [
            ('res.partner', 'x_migrated_status', 'char', None, 'res_partner', 'varchar(64)', False),
        ]
    )

    # Note: After adding columns or fields, Odoo's ORM cache often needs to be cleared or re-initialized
    # For schema changes, _auto_init() or _fields.clear() might be needed for ORM awareness in later steps.
    # env['res.partner']._fields.clear()
    # env.cr.commit() # Commit changes if needed before further ORM operations

    _logger.info("Migration script completed.")

view raw JSON →