Skip to content

Commit 25cb373

Browse files
committed
[IMP] util._normalize_pg_type
Handle type suffixes. closes #345 Related: odoo/upgrade#8715 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 2adba89 commit 25cb373

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/base/tests/test_util.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,36 @@ def test_iter_browse_call_twice(self):
718718

719719

720720
class TestPG(UnitTestCase):
721+
@parametrize(
722+
[
723+
# explicit conversions
724+
("boolean", "bool"),
725+
("smallint", "int2"),
726+
("integer", "int4"),
727+
("bigint", "int8"),
728+
("real", "float4"),
729+
("double precision", "float8"),
730+
("character varying", "varchar"),
731+
("timestamp with time zone", "timestamptz"),
732+
("timestamp without time zone", "timestamp"),
733+
# noop for existing types
734+
("bool", "bool"),
735+
("int4", "int4"),
736+
("varchar", "varchar"),
737+
# and unspecified/unknown types
738+
("jsonb", "jsonb"),
739+
("foo", "foo"),
740+
# keep suffix (for arrays and sized limited varchar)
741+
("int4[]", "int4[]"),
742+
("varchar(2)", "varchar(2)"),
743+
# but also convert types
744+
("integer[]", "int4[]"),
745+
("character varying(16)", "varchar(16)"),
746+
]
747+
)
748+
def test__normalize_pg_type(self, type_, expected):
749+
self.assertEqual(util.pg._normalize_pg_type(type_), expected)
750+
721751
@parametrize(
722752
[
723753
("res_country", "name", False, "jsonb" if util.version_gte("16.0") else "varchar"), # translated field

src/util/pg.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,7 @@ def column_updatable(cr, table, column):
616616

617617

618618
def _normalize_pg_type(type_):
619+
main_type, suffix = re.match(r"(.+?)((?:\[\]|\([0-9]+\))*)$", type_).groups()
619620
aliases = {
620621
"boolean": "bool",
621622
"smallint": "int2",
@@ -627,7 +628,7 @@ def _normalize_pg_type(type_):
627628
"timestamp with time zone": "timestamptz",
628629
"timestamp without time zone": "timestamp",
629630
}
630-
return aliases.get(type_.lower(), type_)
631+
return aliases.get(main_type.strip().lower(), main_type) + suffix
631632

632633

633634
def create_column(cr, table, column, definition, **kwargs):

0 commit comments

Comments
 (0)