Skip to content

Commit b252009

Browse files
committed
[FIX] util/pg: get common column name with type or typcategory
1. This commit Add param ``type_check`` get the common columns info with same typcategory or same data type and skipp the column with different datatype and different type 2. all column name should be returned ``` 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 b252009

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/util/pg.py

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,37 +1220,65 @@ def get_columns(cr, table, ignore=("id",)):
12201220
return ColumnList(*cr.fetchone())
12211221

12221222

1223-
def get_common_columns(cr, table1, table2, ignore=("id",)):
1223+
def get_common_columns(cr, table1, table2, ignore=("id",), type_check=False):
12241224
"""
12251225
Return a list of columns present in both tables.
12261226
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+
:param bool type_check: only returns common columns with compatible data type
1231+
:return: a list of column names present in both table
12311232
:rtype: :class:`~odoo.upgrade.util.pg.ColumnList`
12321233
"""
12331234
_validate_table(table1)
12341235
_validate_table(table2)
12351236

12361237
cr.execute(
12371238
"""
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
1239+
SELECT c.column_name,
1240+
quote_ident(c.column_name),
1241+
count(DISTINCT t.oid) = 1 AS same_type,
1242+
count(DISTINCT t.typcategory) = 1 AS compatible
1243+
FROM information_schema.columns c
1244+
JOIN pg_type t
1245+
ON c.udt_name = t.typname
1246+
WHERE c.table_schema = 'public'
1247+
AND c.table_name IN %s
1248+
AND c.column_name != ALL(%s)
1249+
GROUP BY c.column_name
1250+
HAVING count(c.table_name) = 2
1251+
ORDER BY c.column_name
12501252
""",
12511253
[(table1, table2), list(ignore)],
12521254
)
1253-
return ColumnList(*cr.fetchone())
1255+
cols_info = cr.fetchall()
1256+
1257+
if type_check:
1258+
incompatible_cols = [qname for _, qname, _, compatible in cols_info if not compatible]
1259+
if incompatible_cols:
1260+
_logger.warning(
1261+
"Common columns with incompatible types between %s and %s: %s",
1262+
table1,
1263+
table2,
1264+
", ".join(incompatible_cols),
1265+
)
1266+
compatible_cols = [qname for _, qname, same_type, compatible in cols_info if not same_type and compatible]
1267+
if compatible_cols:
1268+
_logger.warning(
1269+
"Common columns with types that can be implicitly converted between %s and %s: %s",
1270+
table1,
1271+
table2,
1272+
", ".join(compatible_cols),
1273+
)
1274+
1275+
common_columns = [
1276+
name for name, _, same_type, compatible in cols_info if compatible or (type_check and not same_type)
1277+
]
1278+
quoted_common_columns = [
1279+
qname for _, qname, same_type, compatible in cols_info if compatible or (type_check and not same_type)
1280+
]
1281+
return ColumnList(common_columns, quoted_common_columns)
12541282

12551283

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

0 commit comments

Comments
 (0)