Skip to content

Commit 6d6a6f1

Browse files
committed
fix: implement to_diff_tuple to fix rendering of alembic check
1 parent 6fcc8d6 commit 6d6a6f1

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ venv.bak/
117117
.spyderproject
118118
.spyproject
119119

120+
# PyCharm project settings
121+
.idea
122+
120123
# Rope project settings
121124
.ropeproject
122125

src/alembic_utils/reversible_op.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Type
1+
from typing import TYPE_CHECKING, Any, Type
22

33
from alembic.autogenerate import renderers
44
from alembic.operations import MigrateOperation, Operations
@@ -52,6 +52,9 @@ class CreateOp(ReversibleOp):
5252
def reverse(self):
5353
return DropOp(self.target)
5454

55+
def to_diff_tuple(self) -> tuple[Any, ...]:
56+
return "create_entity", self.target.identity, str(self.target.to_sql_statement_create())
57+
5558

5659
@Operations.register_operation("drop_entity", "invoke_for_target_optional_cascade")
5760
class DropOp(ReversibleOp):
@@ -62,17 +65,34 @@ def __init__(self, target: "ReplaceableEntity", cascade: bool = False) -> None:
6265
def reverse(self):
6366
return CreateOp(self.target)
6467

68+
def to_diff_tuple(self) -> tuple[Any, ...]:
69+
return "drop_entity", self.target.identity
70+
6571

6672
@Operations.register_operation("replace_entity", "invoke_for_target")
6773
class ReplaceOp(ReversibleOp):
6874
def reverse(self):
6975
return RevertOp(self.target)
7076

77+
def to_diff_tuple(self) -> tuple[Any, ...]:
78+
return (
79+
"replace_or_revert_entity",
80+
self.target.identity,
81+
str(self.target.to_sql_statement_create_or_replace()),
82+
)
83+
7184

7285
class RevertOp(ReversibleOp):
7386
# Revert is never in an upgrade, so no need to implement reverse
7487
pass
7588

89+
def to_diff_tuple(self) -> tuple[Any, ...]:
90+
return (
91+
"replace_or_revert_entity",
92+
self.target.identity,
93+
str(self.target.to_sql_statement_create_or_replace()),
94+
)
95+
7696

7797
###################
7898
# Implementations #

src/alembic_utils/testbase.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"downgrade": alem_command.downgrade,
2020
"revision": alem_command.revision,
2121
"current": alem_command.current,
22+
"check": alem_command.check,
2223
}
2324

2425

src/test/test_create_function.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import pytest
2+
from alembic.util import AutogenerateDiffsDetected
13
from sqlalchemy import text
24

35
from alembic_utils.pg_function import PGFunction
6+
from alembic_utils.pg_view import PGView
7+
from alembic_utils.replaceable_entity import register_entities
8+
from alembic_utils.testbase import run_alembic_command
49

510
to_upper = PGFunction(
611
schema="public",
@@ -12,9 +17,20 @@
1217
""",
1318
)
1419

20+
TEST_VIEW_before = PGView(
21+
schema="public",
22+
signature="testExample",
23+
definition="select feature_name from information_schema.sql_features",
24+
)
25+
TEST_VIEW_after = PGView(
26+
schema="public",
27+
signature="testExample",
28+
definition="select feature_name, is_supported from information_schema.sql_features",
29+
)
30+
1531

1632
def test_create_and_drop(engine) -> None:
17-
"""Test that the alembic current command does not erorr"""
33+
"""Test that the alembic current command does not error"""
1834
# Runs with no error
1935
up_sql = to_upper.to_sql_statement_create()
2036
down_sql = to_upper.to_sql_statement_drop()
@@ -26,3 +42,17 @@ def test_create_and_drop(engine) -> None:
2642
assert result[0] == "HELLO"
2743
connection.execute(down_sql)
2844
assert True
45+
46+
47+
def test_check_diff_create(engine) -> None:
48+
register_entities([TEST_VIEW_before])
49+
50+
with pytest.raises(AutogenerateDiffsDetected) as e_info:
51+
run_alembic_command(engine, "check", {})
52+
53+
exp = (
54+
"New upgrade operations detected: "
55+
"[('create_entity', 'PGView: public.testExample', "
56+
'\'CREATE VIEW "public"."testExample" AS select feature_name from information_schema.sql_features;\')]'
57+
)
58+
assert e_info.value.args[0] == exp

0 commit comments

Comments
 (0)