From 0d5dc9ebecba8cdb87c54eb81c94332af0c06ffc Mon Sep 17 00:00:00 2001 From: Tyler Barrus Date: Thu, 4 Jan 2024 01:10:41 -0500 Subject: [PATCH 1/3] Fix #53 Add schema to rename default * add schema when attempting to rename default, if present --- .../operations/sync_enum_values.py | 2 +- .../sql_commands/column_default.py | 7 ++- .../test_rename_default_if_required.py | 51 +++++++++++++++---- 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/alembic_postgresql_enum/operations/sync_enum_values.py b/alembic_postgresql_enum/operations/sync_enum_values.py index 309b179..792cb33 100644 --- a/alembic_postgresql_enum/operations/sync_enum_values.py +++ b/alembic_postgresql_enum/operations/sync_enum_values.py @@ -84,7 +84,7 @@ def _set_enum_values(cls, if column_default is not None: column_default = rename_default_if_required(column_default, enum_name, - enum_values_to_rename) + enum_values_to_rename, schema) set_default(connection, schema, table_reference, column_default) diff --git a/alembic_postgresql_enum/sql_commands/column_default.py b/alembic_postgresql_enum/sql_commands/column_default.py index 9f009a0..2c6f408 100644 --- a/alembic_postgresql_enum/sql_commands/column_default.py +++ b/alembic_postgresql_enum/sql_commands/column_default.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Union, List, Tuple +from typing import TYPE_CHECKING, Optional, Union, List, Tuple import sqlalchemy @@ -46,7 +46,8 @@ def set_default(connection: 'Connection', def rename_default_if_required(default_value: str, enum_name: str, - enum_values_to_rename: List[Tuple[str, str]] + enum_values_to_rename: List[Tuple[str, str]], + schema: Optional[str], ) -> str: is_array = default_value.endswith("[]") # remove old type postfix @@ -57,4 +58,6 @@ def rename_default_if_required(default_value: str, column_default_value = column_default_value.replace(f'"{old_value}"', f'"{new_value}"') suffix = "[]" if is_array else "" + if schema: + return f"{column_default_value}::{schema}.{enum_name}{suffix}" return f"{column_default_value}::{enum_name}{suffix}" diff --git a/tests/sync_enum_values/test_rename_default_if_required.py b/tests/sync_enum_values/test_rename_default_if_required.py index 9f5b0dc..c018fec 100644 --- a/tests/sync_enum_values/test_rename_default_if_required.py +++ b/tests/sync_enum_values/test_rename_default_if_required.py @@ -1,31 +1,64 @@ from alembic_postgresql_enum.sql_commands.column_default import rename_default_if_required -def test_without_renames(): +def test_without_renames_no_schema(): old_default_value = "'passive'::order_status_old" - assert (rename_default_if_required(old_default_value, 'order_status', []) + assert (rename_default_if_required(old_default_value, 'order_status', [], None) == "'passive'::order_status") -def test_with_renames(): +def test_with_renames_no_schema(): old_default_value = "'passive'::order_status_old" assert (rename_default_if_required(old_default_value, 'order_status', [ ('passive', 'inactive') - ]) == "'inactive'::order_status") + ], None) == "'inactive'::order_status") -def test_array_with_renames(): +def test_array_with_renames_no_schema(): old_default_value = """'{"passive"}'::order_status_old""" assert (rename_default_if_required(old_default_value, 'order_status', [ ('passive', 'inactive') - ]) == """'{"inactive"}'::order_status""") + ], None) == """'{"inactive"}'::order_status""") -def test_array_default_value(): +def test_array_default_value_no_schema(): old_default_value = """'{}'::order_status_old[]""" - assert (rename_default_if_required(old_default_value, 'order_status', []) - == """'{}'::order_status[]""") \ No newline at end of file + assert (rename_default_if_required(old_default_value, 'order_status', [], None) + == """'{}'::order_status[]""") + +################################################## +# Tests with schema provided +################################################## + +def test_without_renames_with_schema(): + old_default_value = "'passive'::test.order_status_old" + + assert (rename_default_if_required(old_default_value, 'order_status', [], "test") + == "'passive'::test.order_status") + + +def test_with_renames_with_schema(): + old_default_value = "'passive'::test.order_status_old" + + assert (rename_default_if_required(old_default_value, 'order_status', [ + ('passive', 'inactive') + ], "test") == "'inactive'::test.order_status") + + +def test_array_with_renames_with_schema(): + old_default_value = """'{"passive"}'::test.order_status_old""" + + assert (rename_default_if_required(old_default_value, 'order_status', [ + ('passive', 'inactive') + ], "test") == """'{"inactive"}'::test.order_status""") + + +def test_array_default_value_with_schema(): + old_default_value = """'{}'::test.order_status_old[]""" + + assert (rename_default_if_required(old_default_value, 'order_status', [], "test") + == """'{}'::test.order_status[]""") \ No newline at end of file From 50fecae9650969a201a0e3679272087142c08dc8 Mon Sep 17 00:00:00 2001 From: AlexandrovRoman Date: Thu, 4 Jan 2024 10:52:15 +0400 Subject: [PATCH 2/3] PR #54: unify codestyle --- .../operations/sync_enum_values.py | 4 +- .../sql_commands/column_default.py | 8 ++-- .../test_rename_default_if_required.py | 47 +++---------------- 3 files changed, 13 insertions(+), 46 deletions(-) diff --git a/alembic_postgresql_enum/operations/sync_enum_values.py b/alembic_postgresql_enum/operations/sync_enum_values.py index 792cb33..7ff1589 100644 --- a/alembic_postgresql_enum/operations/sync_enum_values.py +++ b/alembic_postgresql_enum/operations/sync_enum_values.py @@ -83,8 +83,8 @@ def _set_enum_values(cls, ) from error if column_default is not None: - column_default = rename_default_if_required(column_default, enum_name, - enum_values_to_rename, schema) + column_default = rename_default_if_required(schema, column_default, enum_name, + enum_values_to_rename) set_default(connection, schema, table_reference, column_default) diff --git a/alembic_postgresql_enum/sql_commands/column_default.py b/alembic_postgresql_enum/sql_commands/column_default.py index 2c6f408..54dd881 100644 --- a/alembic_postgresql_enum/sql_commands/column_default.py +++ b/alembic_postgresql_enum/sql_commands/column_default.py @@ -1,4 +1,4 @@ -from typing import TYPE_CHECKING, Optional, Union, List, Tuple +from typing import TYPE_CHECKING, Union, List, Tuple import sqlalchemy @@ -44,10 +44,10 @@ def set_default(connection: 'Connection', )) -def rename_default_if_required(default_value: str, +def rename_default_if_required(schema: str, + default_value: str, enum_name: str, - enum_values_to_rename: List[Tuple[str, str]], - schema: Optional[str], + enum_values_to_rename: List[Tuple[str, str]] ) -> str: is_array = default_value.endswith("[]") # remove old type postfix diff --git a/tests/sync_enum_values/test_rename_default_if_required.py b/tests/sync_enum_values/test_rename_default_if_required.py index c018fec..9c81ebc 100644 --- a/tests/sync_enum_values/test_rename_default_if_required.py +++ b/tests/sync_enum_values/test_rename_default_if_required.py @@ -1,64 +1,31 @@ from alembic_postgresql_enum.sql_commands.column_default import rename_default_if_required -def test_without_renames_no_schema(): - old_default_value = "'passive'::order_status_old" - - assert (rename_default_if_required(old_default_value, 'order_status', [], None) - == "'passive'::order_status") - - -def test_with_renames_no_schema(): - old_default_value = "'passive'::order_status_old" - - assert (rename_default_if_required(old_default_value, 'order_status', [ - ('passive', 'inactive') - ], None) == "'inactive'::order_status") - - -def test_array_with_renames_no_schema(): - old_default_value = """'{"passive"}'::order_status_old""" - - assert (rename_default_if_required(old_default_value, 'order_status', [ - ('passive', 'inactive') - ], None) == """'{"inactive"}'::order_status""") - - -def test_array_default_value_no_schema(): - old_default_value = """'{}'::order_status_old[]""" - - assert (rename_default_if_required(old_default_value, 'order_status', [], None) - == """'{}'::order_status[]""") - -################################################## -# Tests with schema provided -################################################## - def test_without_renames_with_schema(): old_default_value = "'passive'::test.order_status_old" - assert (rename_default_if_required(old_default_value, 'order_status', [], "test") + assert (rename_default_if_required('test', old_default_value, 'order_status', []) == "'passive'::test.order_status") def test_with_renames_with_schema(): old_default_value = "'passive'::test.order_status_old" - assert (rename_default_if_required(old_default_value, 'order_status', [ + assert (rename_default_if_required('test', old_default_value, 'order_status', [ ('passive', 'inactive') - ], "test") == "'inactive'::test.order_status") + ]) == "'inactive'::test.order_status") def test_array_with_renames_with_schema(): old_default_value = """'{"passive"}'::test.order_status_old""" - assert (rename_default_if_required(old_default_value, 'order_status', [ + assert (rename_default_if_required('test', old_default_value, 'order_status', [ ('passive', 'inactive') - ], "test") == """'{"inactive"}'::test.order_status""") + ]) == """'{"inactive"}'::test.order_status""") def test_array_default_value_with_schema(): old_default_value = """'{}'::test.order_status_old[]""" - assert (rename_default_if_required(old_default_value, 'order_status', [], "test") - == """'{}'::test.order_status[]""") \ No newline at end of file + assert (rename_default_if_required('test', old_default_value, 'order_status', []) + == """'{}'::test.order_status[]""") From a89aab79430627400dc876b46588cfefe9db94e7 Mon Sep 17 00:00:00 2001 From: Artem Golovin Date: Fri, 5 Jan 2024 22:24:19 +0400 Subject: [PATCH 3/3] Bump version to 1.0.2 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 71baa70..8ba4453 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "alembic-postgresql-enum" -version = "1.0.1" +version = "1.0.2" description = "Alembic autogenerate support for creation, alteration and deletion of enums" authors = ["RustyGuard"] license = "MIT"