Skip to content

Commit 51bccea

Browse files
committed
[IMP] util.alter_column_type: use format_query
Don't format queries manually. closes #27 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 1b47857 commit 51bccea

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

src/util/pg.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -435,27 +435,19 @@ def alter_column_type(cr, table, column, type, using=None, logger=_logger):
435435
using = "{{0}}::{}".format(type)
436436

437437
# else, create a new column and parallel update queries.
438-
cr.execute(
439-
"""
440-
ALTER TABLE "{table}" RENAME COLUMN "{column}" TO "_{column}_upg";
441-
ALTER TABLE "{table}" ADD COLUMN "{column}" {type};
442-
""".format(**locals())
443-
)
444-
using = using.format('"_{}_upg"'.format(column))
445-
parallel_execute(
438+
tmp_column = "_{}_upg".format(column)
439+
cr.execute(format_query(cr, "ALTER TABLE {} RENAME COLUMN {} TO {}", table, column, tmp_column))
440+
cr.execute(format_query(cr, "ALTER TABLE {} ADD COLUMN {} {}", table, column, sql.SQL(type)))
441+
442+
using = sql.SQL(format_query(cr, using, tmp_column))
443+
explode_execute(
446444
cr,
447-
explode_query_range(
448-
cr,
449-
"""
450-
UPDATE "{table}"
451-
SET "{column}" = {using}
452-
WHERE "_{column}_upg" IS NOT NULL
453-
""".format(**locals()),
454-
table=table,
455-
),
445+
format_query(cr, "UPDATE {} SET {} = {} WHERE {} IS NOT NULL", table, column, using, tmp_column),
446+
table=table,
456447
logger=logger,
457448
)
458-
cr.execute('ALTER TABLE "{table}" DROP COLUMN "_{column}_upg" CASCADE'.format(**locals()))
449+
450+
cr.execute(format_query(cr, "ALTER TABLE {} DROP COLUMN {} CASCADE", table, tmp_column))
459451

460452

461453
def table_exists(cr, table):

0 commit comments

Comments
 (0)