Skip to content

Commit 54bf0e6

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 c2590fb commit 54bf0e6

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

src/util/pg.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,37 +1251,62 @@ def get_columns(cr, table, ignore=("id",)):
12511251
return ColumnList(*cr.fetchone())
12521252

12531253

1254-
def get_common_columns(cr, table1, table2, ignore=("id",)):
1254+
def get_common_columns(cr, table1, table2, ignore=("id",), type_check=True):
12551255
"""
12561256
Return a list of columns present in both tables.
12571257
12581258
:param str table1: first table name whose columns are retrieved
12591259
:param str table2: second table name whose columns are retrieved
12601260
:param list(str) ignore: list of column names to ignore in the returning list
1261-
:return: a list of column names present in both tables
1261+
:param bool type_check: only returns common columns with compatible data type
1262+
:return: a list of column names present in both table
12621263
:rtype: :class:`~odoo.upgrade.util.pg.ColumnList`
12631264
"""
12641265
_validate_table(table1)
12651266
_validate_table(table2)
12661267

12671268
cr.execute(
12681269
"""
1269-
WITH _common AS (
1270-
SELECT column_name
1271-
FROM information_schema.columns
1272-
WHERE table_schema = 'public'
1273-
AND table_name IN %s
1274-
AND column_name != ALL(%s)
1275-
GROUP BY column_name
1276-
HAVING count(table_name) = 2
1277-
)
1278-
SELECT coalesce(array_agg(column_name::varchar ORDER BY column_name), ARRAY[]::varchar[]),
1279-
coalesce(array_agg(quote_ident(column_name) ORDER BY column_name), ARRAY[]::varchar[])
1280-
FROM _common
1270+
SELECT c.column_name,
1271+
quote_ident(c.column_name),
1272+
count(DISTINCT t.oid) = 1 AS same_type,
1273+
count(DISTINCT t.typcategory) = 1 AS compatible
1274+
FROM information_schema.columns c
1275+
JOIN pg_type t
1276+
ON c.udt_name = t.typname
1277+
WHERE c.table_schema = 'public'
1278+
AND c.table_name IN %s
1279+
AND c.column_name != ALL(%s)
1280+
GROUP BY c.column_name
1281+
HAVING count(c.table_name) = 2
1282+
ORDER BY c.column_name
12811283
""",
12821284
[(table1, table2), list(ignore)],
12831285
)
1284-
return ColumnList(*cr.fetchone())
1286+
cols_info = cr.fetchall()
1287+
1288+
if type_check:
1289+
incompatible_cols = [qname for _, qname, _, compatible in cols_info if not compatible]
1290+
if incompatible_cols:
1291+
_logger.warning(
1292+
"Common columns with incompatible types between %s and %s: %s",
1293+
table1,
1294+
table2,
1295+
", ".join(incompatible_cols),
1296+
)
1297+
compatible_cols = [qname for _, qname, same_type, compatible in cols_info if not same_type and compatible]
1298+
if compatible_cols:
1299+
_logger.warning(
1300+
"Common columns with types that can be implicitly converted between %s and %s: %s",
1301+
table1,
1302+
table2,
1303+
", ".join(compatible_cols),
1304+
)
1305+
1306+
common_columns, quoted_common_columns = zip(
1307+
*((name, qname) for name, qname, _, compatible in cols_info if not type_check or compatible)
1308+
)
1309+
return ColumnList(common_columns, quoted_common_columns)
12851310

12861311

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

0 commit comments

Comments
 (0)