Files
odoo_source/odoo/addons/test_uninstall/models.py
T
Alvaro Fuentes f7cd412725 [FIX] core: fix remove constraints at uninstall
This patch aims to fix multiple issues with the removal of table
constraints at module uninstall.

1. We cannot remove `ir.model.constraint` records before calling
   `_module_data_uninstall` on them. Otherwise we either won't find them
   when performing the search
   `self.env['ir.model.constraint'].search([('module', 'in',
   modules.ids)]` or, if we somehow keep the ids and use `browse`
   instead, would get an error because `_module_data_uninstall` tries to
   access field values of records already removed. Note, although not an
   issue, the removal is redundant for non FK constraints since
   `_model_data_uninstall` already unlinks the record.
2. When a constraint has a name longer than 63 characters (Postgres
   default) we would fail the check for the existence of the constraint
   since the names are truncated.
3. When checking for the presence of a constraint we assumed its type
   would be `u` in `pg_constraint` because for us that means non FK
   (i.e. not `f` type). That's incorrect since there are many more
   types. Here we propose to handle `c,u,x` types.

For bullet 2 we use `tools.make_identifier` that hashes the name and
ensures it fits in the 63 chars limit.

Revert "[IMP] models: warn if constraint key len exceed 63"

The check from commit 823d9e10dc is no
longer needed since the name is ensured to fit length limit.

[IMP] code: improve uninstall tests

Perform extra checks for removal of SQL constraints. Note the test is
commented out in `__init__.py`. It can be uncommented locally for
testing. It's kept commented out to avoid random errors in runbot.

closes odoo/odoo#129084

X-original-commit: af288b7178c25261329dd85a2e64b9dd635cd9e1
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-08-16 16:04:47 +02:00

31 lines
1.2 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import models, fields
class test_uninstall_model(models.Model):
"""
This model uses different types of columns to make it possible to test
the uninstall feature of Odoo.
"""
_name = 'test_uninstall.model'
_description = 'Testing Uninstall Model'
name = fields.Char('Name')
ref = fields.Many2one('res.users', string='User')
rel = fields.Many2many('res.users', string='Users')
_sql_constraints = [
('name_uniq', 'unique (name)', 'Each name must be unique.'),
]
class ResUsers(models.Model):
_inherit = 'res.users'
_sql_constraints = [
('test_uninstall_res_user_unique_constraint', 'unique (password)', 'Test uninstall unique constraint'),
('test_uninstall_res_user_check_constraint', 'check (true)', 'Test uninstall check constraint'),
('test_uninstall_res_user_exclude_constraint', 'exclude (password with =)', 'Test uninstall exclude constraint'),
('test_uninstall_res_user_exclude_constraint_looooooooooooong_name', 'exclude (password with =)', 'Test uninstall exclude constraint'),
]