Skip to content

Commit a05a92c

Browse files
committed
[FIX] util/pg: get common column name with type or typcategory
get the coomin columns info with same typcategory or same data type and skipp the column with different datatype and different type ``` Traceback (most recent call last): File "/home/odoo/src/odoo/19.0/odoo/service/server.py", line 1514, in preload_registries registry = Registry.new(dbname, update_module=update_module, install_modules=config['init'], upgrade_modules=config['update'], reinit_modules=config['reinit']) File "/home/odoo/src/odoo/19.0/odoo/tools/func.py", line 88, in locked return func(inst, *args, **kwargs) File "/home/odoo/src/odoo/19.0/odoo/orm/registry.py", line 186, in new load_modules( File "/home/odoo/src/odoo/19.0/odoo/modules/loading.py", line 493, in load_modules migrations.migrate_module(package, 'end') File "/home/odoo/src/odoo/19.0/odoo/modules/migration.py", line 220, in migrate_module exec_script(self.cr, installed_version, pyfile, pkg.name, stage, stageformat[stage] % version) File "/home/odoo/src/odoo/19.0/odoo/modules/migration.py", line 257, in exec_script mod.migrate(cr, installed_version) File "/tmp/tmp7yriz79e/migrations/hr/saas~18.4.1.1/end-migrate.py", line 87, in migrate cr.execute(query, [e[1] for e in required_default_values]) File "/home/odoo/src/odoo/19.0/odoo/sql_db.py", line 426, in execute self._obj.execute(query, params) psycopg2.errors.DatatypeMismatch: column "fondo_ahorro" is of type boolean but expression is of type double precision LINE 16: ..."."distance_home_work_unit", "e"."employee_type", "e"."fondo... ^ HINT: You will need to rewrite or cast the expression. ``` ``` select id,name,model,ttype,store from ir_model_fields where name='fondo_ahorro'; id | name | model | ttype | store -------+--------------+-----------------------+---------+------- 28361 | fondo_ahorro | hr.employee | float | t 28687 | fondo_ahorro | calculo.liquidaciones | float | t 28379 | fondo_ahorro | hr.version | boolean | t (3 rows) ``` upg-3444635 opw-5260147
1 parent 92efe24 commit a05a92c

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

src/util/pg.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,30 +1227,49 @@ def get_common_columns(cr, table1, table2, ignore=("id",)):
12271227
:param str table1: first table name whose columns are retrieved
12281228
:param str table2: second table name whose columns are retrieved
12291229
:param list(str) ignore: list of column names to ignore in the returning list
1230-
:return: a list of column names present in both tables
1230+
:return: a list of column names present in both tables with compatible data type
12311231
:rtype: :class:`~odoo.upgrade.util.pg.ColumnList`
12321232
"""
12331233
_validate_table(table1)
12341234
_validate_table(table2)
12351235

12361236
cr.execute(
12371237
"""
1238-
WITH _common AS (
1239-
SELECT column_name
1240-
FROM information_schema.columns
1241-
WHERE table_schema = 'public'
1242-
AND table_name IN %s
1243-
AND column_name != ALL(%s)
1244-
GROUP BY column_name
1245-
HAVING count(table_name) = 2
1246-
)
1247-
SELECT coalesce(array_agg(column_name::varchar ORDER BY column_name), ARRAY[]::varchar[]),
1248-
coalesce(array_agg(quote_ident(column_name) ORDER BY column_name), ARRAY[]::varchar[])
1249-
FROM _common
1238+
SELECT column_name,
1239+
quote_ident(column_name),
1240+
count(DISTINCT t.oid) = 1 AS same_type,
1241+
count(DISTINCT t.typcategory) = 1 AS compatible
1242+
FROM information_schema.columns c
1243+
JOIN pg_type t
1244+
ON c.udt_name = t.typname
1245+
WHERE c.table_schema = 'public'
1246+
AND c.table_name IN %s
1247+
AND c.column_name != ALL(%s)
1248+
GROUP BY c.column_name
1249+
HAVING count(c.table_name) = 2
12501250
""",
12511251
[(table1, table2), list(ignore)],
12521252
)
1253-
return ColumnList(*cr.fetchone())
1253+
cols_info = cr.fetchall()
1254+
incompatible_cols = [qname for _, qname, _, compatible in cols_info if not compatible]
1255+
if incompatible_cols:
1256+
_logger.warning(
1257+
"Common columns with incompatible types between %s and %s: %s",
1258+
table1,
1259+
table2,
1260+
", ".join(incompatible_cols),
1261+
)
1262+
compatible_cols = [qname for _, qname, same_type, compatible in cols_info if not same_type and compatible]
1263+
if compatible_cols:
1264+
_logger.warning(
1265+
"Common columns with types that can be implicitly converted between %s and %s: %s",
1266+
table1,
1267+
table2,
1268+
", ".join(compatible_cols),
1269+
)
1270+
common_columns = [name for name, _, _, compatible in cols_info if compatible]
1271+
quoted_common_columns = [qname for _, qname, _, compatible in cols_info if compatible]
1272+
return ColumnList(common_columns, quoted_common_columns)
12541273

12551274

12561275
def rename_table(cr, old_table, new_table, remove_constraints=True):

0 commit comments

Comments
 (0)