Skip to content

Commit

Permalink
[IMP] rename_models: Avoid mogrify 'IndexError: tuple index out of ra…
Browse files Browse the repository at this point in the history
…nge' for % LIKE parameter
  • Loading branch information
rousseldenis committed Nov 28, 2022
1 parent f0b809f commit 8673101
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions openupgradelib/openupgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
# Copyright Odoo Community Association (OCA)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).

import sys
import os
import inspect
import uuid
import logging as _logging_module
import os
import sys
import uuid
from datetime import datetime
from functools import wraps

try:
from StringIO import StringIO
except ImportError:
from io import StringIO

from contextlib import contextmanager

try:
from psycopg2 import errorcodes, ProgrammingError, IntegrityError
from psycopg2 import IntegrityError, ProgrammingError, errorcodes
except ImportError:
from psycopg2cffi import errorcodes, ProgrammingError, IntegrityError
from psycopg2cffi import IntegrityError, ProgrammingError, errorcodes
try:
from contextlib import ExitStack
except ImportError:
Expand All @@ -40,9 +43,10 @@ def __exit__(self, exc_type, exc_value, traceback):
while self._cms:
self._cms.pop().__exit__(exc_type, exc_value, traceback)

from lxml import etree
from psycopg2 import sql
from psycopg2.extensions import AsIs
from lxml import etree

from . import openupgrade_tools

core = None
Expand Down Expand Up @@ -98,9 +102,9 @@ def __exit__(self, exc_type, exc_value, traceback):
# version < 6.1
import tools
SUPERUSER_ID = 1
from tools.yaml_import import yaml_import
from osv.osv import except_osv as except_orm
from osv.fields import many2many, one2many
from osv.osv import except_osv as except_orm
from tools.yaml_import import yaml_import


def do_raise(error):
Expand Down Expand Up @@ -905,18 +909,25 @@ def rename_models(cr, model_spec):
column = row[1]
if not column_exists(cr, table, column):
continue
query = """
UPDATE {table}
SET {column} = replace(
{column}, %(old)s, %(new)s)
WHERE {column} LIKE %(old_like)s
"""
sql_query = sql.SQL(query).format(
table=sql.Identifier(table),
column=sql.Identifier(column)
)
logged_query(
cr, """
UPDATE %(table)s
SET %(column)s = replace(
%(column)s, '%(old)s,', '%(new)s,')
WHERE %(column)s LIKE '%(old)s,%%'
""" % {
"table": table,
"column": column,
"old": old,
"new": new,
}, skip_no_result=True,
cr,
sql_query,
{
"old": old + ',',
"old_like": old + ',%%',
"new": new + ',',
},
skip_no_result=True,
)
# Update export profiles references
logged_query(
Expand Down Expand Up @@ -2407,6 +2418,7 @@ def format_message(f):
from odoo.tools.safe_eval import dateutil
except ImportError:
import datetime as safe_eval_datetime

import dateutil
globaldict.update({
'datetime': safe_eval_datetime,
Expand Down

0 comments on commit 8673101

Please sign in to comment.