Skip to content

Commit

Permalink
Fix issue with enum array default value
Browse files Browse the repository at this point in the history
  • Loading branch information
uriyyo committed Dec 19, 2023
1 parent 5780119 commit c1f2127
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion alembic_postgresql_enum/sql_commands/column_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ def rename_default_if_required(default_value: str,
enum_name: str,
enum_values_to_rename: List[Tuple[str, str]]
) -> str:
is_array = default_value.endswith("[]")
# remove old type postfix
column_default_value = default_value[:default_value.find("::")]

for old_value, new_value in enum_values_to_rename:
column_default_value = column_default_value.replace(f"'{old_value}'", f"'{new_value}'")
column_default_value = column_default_value.replace(f'"{old_value}"', f'"{new_value}"')

return f"{column_default_value}::{enum_name}"
suffix = "[]" if is_array else ""
return f"{column_default_value}::{enum_name}{suffix}"
7 changes: 7 additions & 0 deletions tests/sync_enum_values/test_rename_default_if_required.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ def test_array_with_renames():
assert (rename_default_if_required(old_default_value, 'order_status', [
('passive', 'inactive')
]) == """'{"inactive"}'::order_status""")


def test_array_default_value():
old_default_value = """'{}'::order_status_old[]"""

assert (rename_default_if_required(old_default_value, 'order_status', [])
== """'{}'::order_status[]""")

0 comments on commit c1f2127

Please sign in to comment.