Skip to content

Commit ff3001a

Browse files
committed
[FIX] util.column_type: return the max length of varchar column
This allow the set/drop the max length on existing columns with `util.alter_column_type()`. closes #340 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 3e78b44 commit ff3001a

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/base/tests/test_util.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,31 @@ def test_iter_browse_call_twice(self):
718718

719719

720720
class TestPG(UnitTestCase):
721+
@parametrize(
722+
[
723+
("res_country", "name", "jsonb" if util.version_gte("16.0") else "varchar"), # translated field
724+
("res_country", "code", "varchar(2)"),
725+
("res_currency", "active", "bool"),
726+
("res_country", "create_date", "timestamp"),
727+
("res_currency", "create_uid", "int4"),
728+
("res_country", "name_position", "varchar"),
729+
("res_country", "address_format", "text"),
730+
("res_partner", "does_not_exists", None),
731+
]
732+
)
733+
def test_column_type(self, table, column, expected):
734+
value = util.column_type(self.env.cr, table, column)
735+
if expected is None:
736+
self.assertIsNone(value)
737+
else:
738+
self.assertEqual(value, expected)
739+
721740
def test_alter_column_type(self):
722741
cr = self.env.cr
723742
cr.execute(
724743
"""
725744
ALTER TABLE res_partner_bank ADD COLUMN x bool;
745+
ALTER TABLE res_partner_bank ADD COLUMN y varchar(4);
726746
727747
UPDATE res_partner_bank
728748
SET x = CASE id % 3
@@ -742,6 +762,12 @@ def test_alter_column_type(self):
742762
"Some values where not casted correctly via USING",
743763
)
744764

765+
self.assertEqual(util.column_type(cr, "res_partner_bank", "y"), "varchar(4)")
766+
util.alter_column_type(cr, "res_partner_bank", "y", "varchar")
767+
self.assertEqual(util.column_type(cr, "res_partner_bank", "y"), "varchar")
768+
util.alter_column_type(cr, "res_partner_bank", "y", "varchar(12)")
769+
self.assertEqual(util.column_type(cr, "res_partner_bank", "y"), "varchar(12)")
770+
745771
@parametrize(
746772
[
747773
("test", "<p>test</p>"),

src/util/pg.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,14 @@ def get_value_or_en_translation(cr, table, column):
497497

498498
def _column_info(cr, table, column):
499499
_validate_table(table)
500+
# NOTE: usage of both `CONCAT` and `||` in the query below is done on purpose to take advantage of their NULL handling.
501+
# NULLS propagate with `||` and are ignored by `CONCAT`.
500502
cr.execute(
501503
"""
502-
SELECT COALESCE(bt.typname, t.typname) AS udt_name,
504+
SELECT CONCAT(
505+
COALESCE(bt.typname, t.typname),
506+
'(' || information_schema._pg_char_max_length(information_schema._pg_truetypid(a.*, t.*), information_schema._pg_truetypmod(a.*, t.*)) || ')'
507+
) AS udt_name,
503508
NOT (a.attnotnull OR t.typtype = 'd' AND t.typnotnull) AS is_nullable,
504509
( c.relkind IN ('r','p','v','f')
505510
AND pg_column_is_updatable(c.oid::regclass, a.attnum, false)

0 commit comments

Comments
 (0)