Skip to content

Commit

Permalink
fix: ignore case when comparing
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanderhoof committed Jan 16, 2024
1 parent 51de152 commit 1b0dbc5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![](https://img.shields.io/pypi/v/alembic-dddl.svg)](https://pypi.org/project/alembic-dddl/) [![](https://img.shields.io/github/v/tag/Vanderhoof/alembic-dddl.svg?label=GitHub)](https://github.com/Vanderhoof/alembic-dddl) ![tests](https://github.com/Vanderhoof/alembic-dddl/actions/workflows/tests.yml/badge.svg) [![codecov](https://codecov.io/gh/Vanderhoof/alembic-dddl/graph/badge.svg?token=BQJBA9PXPN)](https://codecov.io/gh/Vanderhoof/alembic-dddl)

A plugin for [Alembic](https://alembic.sqlalchemy.org/en/latest/) DB migration tool that adds support for arbitrary user-defined objects like views or functions in autogenerate command.
A plugin for [Alembic](https://alembic.sqlalchemy.org/en/latest/) DB migration tool that adds support for arbitrary user-defined objects like views, functions, triggers, etc. in autogenerate command.

Alembic DDDL _does not_ compare the objects in the code with their state in the database. Instead, it **only tracks if the source code of the script has changed**, compared to the previous revision.

Expand Down
17 changes: 10 additions & 7 deletions alembic_dddl/src/comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def get_changed_ddls(self) -> List[Tuple[DDL, Optional[RevisionedScript]]]:

def _scripts_differ(self, one: str, two: str) -> bool:
"""
Compare two scripts, ignoring indentation differences and optinoally ignoring comments.
Compare two scripts, ignoring formatting and optionally ignoring comments.
Args:
one: the first script source code
Expand All @@ -161,11 +161,14 @@ def _scripts_differ(self, one: str, two: str) -> bool:
Returns:
True if the scripts differ, False if the scripts are the same
"""
one_norm = sqlparse.format(
one.strip(), reindent_aligned=True, strip_comments=self.ignore_comments
)
two_norm = sqlparse.format(
two.strip(), reindent_aligned=True, strip_comments=self.ignore_comments
)
kwargs = {
"reindent_aligned": True,
"strip_comments": self.ignore_comments,
"keyword_case": "upper",
"identifier_case": "lower",
"use_space_around_operators": True,
}
one_norm = sqlparse.format(one.strip(), **kwargs)
two_norm = sqlparse.format(two.strip(), **kwargs)

return one_norm != two_norm
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "alembic-dddl"
version = "0.1.1"
version = "0.1.2"
description = "Alembic extension that adds support for arbitrary user-defined objects like views or functions in autogenerate command."
authors = ["Daniil Minukhin <[email protected]>"]
license = "MIT"
Expand Down
6 changes: 6 additions & 0 deletions tests/src/comprator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ def test_reformatted_script(empty_comparator: CustomDDLComparator) -> None:
)
assert empty_comparator._scripts_differ(one=script1, two=script2) is False

@staticmethod
def test_changed_case_script(empty_comparator: CustomDDLComparator) -> None:
script1 = "SELECT * FROM Customers WHERE customer_name LIKE 'John%';"
script2 = "select * fRoM CuStOmErS WHERE CUSTOMER_NAME LIKE 'John%';"
assert empty_comparator._scripts_differ(one=script1, two=script2) is False

@staticmethod
def test_comments_ignored(empty_comparator: CustomDDLComparator) -> None:
empty_comparator.ignore_comments = True
Expand Down

0 comments on commit 1b0dbc5

Please sign in to comment.