Skip to content
Odoo Migration

Odoo Migration: Handling Custom Database Views and Stored Procedures

A technical guide for CIOs and ERP consultants on migrating custom PostgreSQL views and stored procedures in Odoo. Learn how to assess dependencies, refactor deprecated SQL, and ensure data integrity during version upgrades.

7 min readSysGenPro Editorial
Odoo Migration: Handling Custom Database Views and Stored Procedures

Direct Answer: How to Handle Custom Database Views and Stored Procedures in Odoo Migration

Odoo does not automatically migrate custom database views or stored procedures created outside the standard ORM framework. These objects are treated as raw database artifacts and are not part of the module upgrade mechanism. The recommended approach is to inventory all custom SQL objects, analyze their dependencies on Odoo models, refactor them to use the ORM where possible, or manually recreate them in the target database after the core upgrade. The primary risks include broken reporting, data inconsistency, and security vulnerabilities if these objects are not properly managed. Failure to address these customizations can lead to application errors or silent data corruption during and after the migration.

Why Custom SQL Objects Are High-Risk in Odoo Upgrades

Odoo is built on a Python ORM that abstracts database interactions. When developers bypass this layer to create direct PostgreSQL views or stored procedures, they introduce technical debt that is invisible to the standard upgrade process. The Odoo upgrade tool focuses on Python code, XML views, and data files. It does not scan for or migrate raw SQL objects defined in the database schema. Consequently, if a custom view references a table or column that is renamed, dropped, or altered in the new Odoo version, the view will break. Similarly, stored procedures that rely on specific internal table structures or deprecated PostgreSQL functions may fail to execute or produce incorrect results.

The risk is compounded by the fact that these objects are often used in critical business processes, such as financial reporting, inventory calculations, or external integrations. If a view used by a BI tool breaks, the business may not notice until a report is generated, leading to delayed detection of data issues. Furthermore, custom SQL objects often lack the security checks and access control mechanisms provided by the Odoo ORM, potentially exposing sensitive data if not properly secured at the database level.

Step 1: Inventory and Dependency Analysis

The first step in handling custom database views and stored procedures is to create a comprehensive inventory of all such objects in the source database. This requires database administrator access and the use of PostgreSQL system catalogs. You must identify all views, materialized views, functions, and procedures that are not part of the standard Odoo schema. This can be done by querying the pg_catalog tables, filtering out objects owned by the standard Odoo modules, and listing the remaining custom objects.

Object TypeIdentification MethodKey Information to Capture
ViewsQuery pg_viewsView name, definition SQL, dependent tables, owner
Materialized ViewsQuery pg_matviewsView name, refresh schedule, dependent tables, owner
FunctionsQuery pg_procFunction name, language, arguments, return type, owner
ProceduresQuery pg_procProcedure name, language, arguments, owner

For each identified object, you must analyze its dependencies. This includes determining which Odoo models or tables it references, which other custom objects depend on it, and which application code or external systems use it. This dependency mapping is crucial for understanding the impact of changes and for planning the migration sequence. You should also document the business purpose of each object to determine if it can be refactored or if it is essential to the current business process.

Step 2: Refactoring and Modernization

Once the inventory is complete, the next step is to evaluate each object for refactoring opportunities. The goal is to reduce reliance on raw SQL and move logic into the Odoo ORM where possible. For example, a custom view used for reporting can often be replaced by an Odoo report or a computed field in a model. A stored procedure used for data transformation can be replaced by a Python method in a custom module. This refactoring not only improves maintainability but also ensures that the logic is properly integrated into the Odoo upgrade process.

If refactoring is not feasible, you must ensure that the custom SQL objects are compatible with the target PostgreSQL version and the new Odoo schema. This involves reviewing the SQL code for deprecated functions, changed data types, or altered table structures. You should also consider adding error handling and logging to these objects to improve debuggability. For objects that cannot be refactored, you must plan to manually recreate them in the target database after the core upgrade.

Step 3: Testing in a Staging Environment

Before executing the migration in production, you must test the handling of custom database views and stored procedures in a staging environment that mirrors the production setup. This includes upgrading the staging database to the target Odoo version, recreating the custom SQL objects, and running comprehensive tests to verify their functionality. You should test not only the objects themselves but also the business processes that depend on them. This includes generating reports, running integrations, and validating data consistency.

During testing, you should pay close attention to performance and security. Custom SQL objects can sometimes introduce performance bottlenecks, especially if they are not properly indexed or optimized. You should run performance tests to ensure that the objects do not degrade system performance. You should also verify that the objects are properly secured and that they do not expose sensitive data to unauthorized users. This includes checking database permissions and ensuring that the objects are owned by the correct database user.

Step 4: Migration Execution and Validation

During the production migration, you must follow a carefully planned sequence to handle custom database views and stored procedures. First, you should back up the production database to ensure that you can roll back if necessary. Next, you should upgrade the core Odoo application to the target version. After the core upgrade is complete, you should manually recreate the custom SQL objects in the target database. This can be done by executing the SQL scripts that define the objects, which should have been prepared and tested in the staging environment.

After recreating the objects, you must validate their functionality in the production environment. This includes running the same tests that were performed in the staging environment, but with a focus on data integrity and business process continuity. You should verify that the objects are producing the correct results and that they are not causing any errors or performance issues. You should also monitor the system closely for any unexpected behavior or errors that may indicate a problem with the custom SQL objects.

Risk Management and Rollback Strategy

Handling custom database views and stored procedures introduces significant risks to the migration process. The primary risks include data loss, broken reporting, and security vulnerabilities. To mitigate these risks, you must have a well-defined rollback strategy in place. This includes having a verified backup of the production database that can be restored if the migration fails. You should also have a documented plan for how to handle specific failure scenarios, such as a broken view or a failed stored procedure.

In addition to the rollback strategy, you should implement monitoring and alerting to detect issues early. This includes monitoring database performance, application logs, and business process metrics. You should also establish a communication plan to keep stakeholders informed of the migration progress and any issues that arise. By proactively managing risks and having a clear rollback strategy, you can minimize the impact of any issues that may occur during the migration.

Post-Migration Stabilization and Maintenance

After the migration is complete, you must enter a stabilization phase to ensure that the custom database views and stored procedures are functioning correctly in the production environment. This includes monitoring the system for any issues, addressing any bugs or performance problems, and providing support to users. You should also document the custom SQL objects and their dependencies to ensure that they are properly maintained in the future.

In the long term, you should consider reducing the reliance on custom SQL objects by refactoring them into the Odoo ORM where possible. This will improve the maintainability of the system and reduce the risk of issues during future upgrades. You should also establish a process for reviewing and approving any new custom SQL objects to ensure that they are properly designed, tested, and documented. By taking a proactive approach to managing custom SQL objects, you can ensure the long-term health and stability of your Odoo system.

Written by
SysGenPro Editorial

SysGenPro Editorial

Platform & solutions team

Practical guidance on ERP, AI automation, cloud deployment and enterprise transformation from the SysGenPro team.

Share this article

Frequently asked questions

Common questions on this topic. Talk to our team for anything specific to your business.

01Can Odoo automatically migrate custom database views?

No, Odoo does not automatically migrate custom database views. These objects must be manually inventoried, refactored, or recreated in the target database after the core upgrade.

02What happens to stored procedures during an Odoo upgrade?

Stored procedures are not automatically migrated. They may break if they reference tables or functions that have changed in the new version. They must be manually recreated and tested.

03How do I find all custom SQL in my Odoo database?

You can query the PostgreSQL system catalogs (pg_views, pg_proc, etc.) to identify objects that are not part of the standard Odoo schema. This requires database administrator access.

04Is it safe to use raw SQL in Odoo custom modules?

Using raw SQL is generally discouraged as it bypasses the ORM's security and abstraction layers. It should only be used when necessary and must be carefully managed and tested.

05How do I test database changes before an Odoo upgrade?

You should test in a staging environment that mirrors production. This includes upgrading the staging database, recreating custom SQL objects, and running comprehensive functional and performance tests.

06What are the risks of bypassing the Odoo ORM?

Bypassing the ORM can lead to security vulnerabilities, data inconsistency, and maintenance challenges. It also makes the system more vulnerable to changes in the underlying database schema during upgrades.

Continue learning
View all articles
Explore the platform

Where to go next

Browse the knowledge hub

See how SysGenPro fits your business

Discuss your workflows, modules and deployment options with our team.

Back to resources