Skip to content

Commit 353c206

Browse files
committed
[FIX] util/pg: always change type with USING for booleans
For the ORM a NULL value in a boolean colum means False. We cannot exclude those values from the type conversion otherwise the assigned value may be wrong. For example, in this usage: ```py util.alter_column_type(cr, "table", "col", "varchar", using="CASE WHEN {0} THEN 'x' ELSE 'y' END" ``` we would fail to assign `x`/`y` to the column in null values. closes #71 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 71c35a8 commit 353c206

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/util/pg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,9 +570,12 @@ def alter_column_type(cr, table, column, type, using=None, logger=_logger):
570570
cr.execute(format_query(cr, "ALTER TABLE {} ADD COLUMN {} {}", table, column, sql.SQL(type)))
571571

572572
using = sql.SQL(format_query(cr, using, tmp_column))
573+
where_clause = sql.SQL("")
574+
if column_type(cr, table, column) != "bool":
575+
where_clause = sql.SQL(format_query(cr, "WHERE {} IS NOT NULL", tmp_column))
573576
explode_execute(
574577
cr,
575-
format_query(cr, "UPDATE {} SET {} = {} WHERE {} IS NOT NULL", table, column, using, tmp_column),
578+
format_query(cr, "UPDATE {} SET {} = {} {}", table, column, using, where_clause),
576579
table=table,
577580
logger=logger,
578581
)

0 commit comments

Comments
 (0)