Skip to content

Commit 6e0d631

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 6e0d631

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

src/util/pg.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,30 +1227,52 @@ 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 same 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
1238+
WITH _cols AS (
1239+
SELECT c.table_name, c.column_name, t.typcategory, c.udt_name AS data_type
1240+
FROM information_schema.columns c
1241+
JOIN pg_type t ON c.udt_name = t.typname
1242+
WHERE c.table_schema = 'public'
1243+
AND c.table_name IN %s
1244+
),
1245+
_common AS (
1246+
SELECT c1.column_name, c1.data_type AS type_t1, c2.data_type AS type_t2, c1.typcategory AS cat_t1,
1247+
c2.typcategory AS cat_t2, (c1.typcategory = c2.typcategory) AS is_compatible,
1248+
(c1.typcategory = c2.typcategory AND c1.data_type != c2.data_type) AS is_managable
1249+
FROM _cols c1
1250+
JOIN _cols c2
1251+
ON c1.column_name = c2.column_name
1252+
AND c1.table_name = %s
1253+
AND c2.table_name = %s
12461254
)
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
1255+
SELECT coalesce(array_agg(column_name ORDER BY column_name) FILTER (WHERE is_compatible or is_managable), ARRAY[]::varchar[]) AS column_names,
1256+
coalesce(array_agg(quote_ident(column_name) ORDER BY column_name) FILTER (WHERE is_compatible or is_managable), ARRAY[]::varchar[]) AS quoted_column_names,
1257+
coalesce(array_agg(column_name) FILTER (WHERE is_managable), ARRAY[]::varchar[]) AS same_typcatg_columns,
1258+
coalesce(array_agg(column_name) FILTER (WHERE not is_compatible), ARRAY[]::varchar[]) AS diff_typcatg_columns
1259+
FROM _common
12501260
""",
1251-
[(table1, table2), list(ignore)],
1261+
[(table1, table2), table1, table2, list(ignore)],
12521262
)
1253-
return ColumnList(*cr.fetchone())
1263+
common_columns, quoted_common_columns, same_typcatg_columns, diff_typcatg_columns = cr.fetchone()
1264+
if same_typcatg_columns:
1265+
logger.warning(
1266+
"Columns with same type category but different types between %s and %s: %s",
1267+
table1, table2, ", ".join(same_typcatg_columns)
1268+
)
1269+
if diff_typcatg_columns:
1270+
logger.warning(
1271+
"Columns with incompatible type categories between %s and %s: %s",
1272+
table1, table2, ", ".join(diff_typcatg_columns)
1273+
)
1274+
1275+
return ColumnList(common_columns, quoted_common_columns)
12541276

12551277

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

0 commit comments

Comments
 (0)