Skip to content

Commit 38a3222

Browse files
committed
[IMP] alter_column_type
Early return when the column is already in the asked type. Part-of: #226 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 5a6ff7e commit 38a3222

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

src/util/pg.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,21 @@ def column_updatable(cr, table, column):
505505
return nfo and nfo[2]
506506

507507

508+
def _normalize_pg_type(type_):
509+
aliases = {
510+
"boolean": "bool",
511+
"smallint": "int2",
512+
"integer": "int4",
513+
"bigint": "int8",
514+
"real": "float4",
515+
"double precision": "float8",
516+
"character varying": "varchar",
517+
"timestamp with time zone": "timestamptz",
518+
"timestamp without time zone": "timestamp",
519+
}
520+
return aliases.get(type_.lower(), type_)
521+
522+
508523
def create_column(cr, table, column, definition, **kwargs):
509524
"""
510525
Create a column.
@@ -546,18 +561,7 @@ def create_column(cr, table, column, definition, **kwargs):
546561
elif on_delete_action is not no_def:
547562
raise ValueError("`on_delete_action` argument can only be used if `fk_table` argument is set.")
548563

549-
aliases = {
550-
"boolean": "bool",
551-
"smallint": "int2",
552-
"integer": "int4",
553-
"bigint": "int8",
554-
"real": "float4",
555-
"double precision": "float8",
556-
"character varying": "varchar",
557-
"timestamp with time zone": "timestamptz",
558-
"timestamp without time zone": "timestamp",
559-
}
560-
definition = aliases.get(definition.lower(), definition)
564+
definition = _normalize_pg_type(definition)
561565

562566
if definition == "bool" and default is no_def:
563567
default = False
@@ -612,6 +616,13 @@ def remove_column(cr, table, column, cascade=False):
612616
def alter_column_type(cr, table, column, type, using=None, where=None, logger=_logger):
613617
if where and not using:
614618
raise ValueError("`where` parameter is only relevant with a non-default `using` parameter")
619+
620+
if not using:
621+
current_type = column_type(cr, table, column)
622+
if current_type and current_type == _normalize_pg_type(type):
623+
logger.info("Column %r of table %r is already defined as %r", column, table, type)
624+
return
625+
615626
# remove the existing linked `ir_model_fields_selection` recods in case it was a selection field
616627
if table_exists(cr, "ir_model_fields_selection"):
617628
cr.execute(

0 commit comments

Comments
 (0)