consbio / pg-database-utils

A suite of utilities for PostgreSQL database queries and operations built on sqlalchemy
BSD 3-Clause "New" or "Revised" License
1 stars 0 forks source link

Fix handling of `using` argument in `alter_column_type` #4

Closed nikmolnar closed 3 years ago

nikmolnar commented 4 years ago

Currently, any using value passed to alter_column_type is overwritten by the following logic:

if using is not None:
    using = f"{column_name}::{using}"
if new_type != "bool":
    using = f"{column_name}::{new_type}"
else:
    using = f"CASE WHEN {column_name}::int=0 THEN FALSE WHEN {column_name} IS NULL THEN NULL ELSE TRUE END"

Either new_type is "bool" or not, and in either case, any original value ends up being replaced.

This PR corrects the issue by replacing if new_type != "bool" to elif new_type != "bool".