Skip to content

Commit 68d1869

Browse files
committed
[FIX] util/pg: use the old column to check the type
It's the original column what we need to check, the target column would have a different type. See 353c206 closes #76 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 173fbef commit 68d1869

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/base/tests/test_util.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,30 @@ def test_iter_browse_call_twice(self):
431431

432432

433433
class TestPG(UnitTestCase):
434+
def test_alter_column_type(self):
435+
cr = self.env.cr
436+
cr.execute(
437+
"""
438+
ALTER TABLE res_partner_title ADD COLUMN x bool;
439+
440+
UPDATE res_partner_title
441+
SET x = CASE id % 3
442+
WHEN 1 THEN NULL
443+
WHEN 2 THEN True
444+
ELSE False
445+
END
446+
"""
447+
)
448+
self.assertEqual(util.column_type(cr, "res_partner_title", "x"), "bool")
449+
util.alter_column_type(cr, "res_partner_title", "x", "int", using="CASE {0} WHEN True THEN 2 ELSE 1 END")
450+
self.assertEqual(util.column_type(cr, "res_partner_title", "x"), "int4")
451+
cr.execute("SELECT id, x FROM res_partner_title")
452+
data = cr.fetchall()
453+
self.assertTrue(
454+
all(x == 1 or (x == 2 and id_ % 3 == 2) for id_, x in data),
455+
"Some values where not casted correctly via USING",
456+
)
457+
434458
@parametrize(
435459
[
436460
("test", "<p>test</p>"),

src/util/pg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def alter_column_type(cr, table, column, type, using=None, logger=_logger):
605605

606606
using = sql.SQL(format_query(cr, using, tmp_column))
607607
where_clause = sql.SQL("")
608-
if column_type(cr, table, column) != "bool":
608+
if column_type(cr, table, tmp_column) != "bool":
609609
where_clause = sql.SQL(format_query(cr, "WHERE {} IS NOT NULL", tmp_column))
610610
explode_execute(
611611
cr,

0 commit comments

Comments
 (0)