From 3d33cfab046dfd7ca8df43c0afd3c6d738fcdc7e Mon Sep 17 00:00:00 2001 From: Louis-Amaury Chaib Date: Sat, 1 Feb 2025 04:02:34 -0500 Subject: [PATCH] refactor: add revision context to AutogenerateDiffsDetected so that wrappers may make other formatting of the diff ### Description As discussed in #1597, AutogenerateDiffsDetected should hold contextual information so that command can be wrapped for another format (CI, pre-commit hook...) ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [x] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [ ] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Closes: #1598 Pull-request: https://github.com/sqlalchemy/alembic/pull/1598 Pull-request-sha: 7799320ad3c1a1bd769254d610288c97f85cb345 Change-Id: Id08bc52a0586063f177736a36a61f96232459f1c --- alembic/command.py | 4 +++- alembic/util/exc.py | 21 ++++++++++++++++++++- docs/build/unreleased/issue1597.rst | 6 ++++++ 3 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 docs/build/unreleased/issue1597.rst diff --git a/alembic/command.py b/alembic/command.py index 89c12354..0ae1d9a8 100644 --- a/alembic/command.py +++ b/alembic/command.py @@ -298,7 +298,9 @@ def check(config: "Config") -> None: if diffs: raise util.AutogenerateDiffsDetected( - f"New upgrade operations detected: {diffs}" + f"New upgrade operations detected: {diffs}", + revision_context=revision_context, + diffs=diffs, ) else: config.print_stdout("No new upgrade operations detected.") diff --git a/alembic/util/exc.py b/alembic/util/exc.py index 0d0496b1..c790e18a 100644 --- a/alembic/util/exc.py +++ b/alembic/util/exc.py @@ -1,6 +1,25 @@ +from __future__ import annotations + +from typing import Any +from typing import List +from typing import Tuple +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from alembic.autogenerate import RevisionContext + + class CommandError(Exception): pass class AutogenerateDiffsDetected(CommandError): - pass + def __init__( + self, + message: str, + revision_context: RevisionContext, + diffs: List[Tuple[Any, ...]], + ) -> None: + super().__init__(message) + self.revision_context = revision_context + self.diffs = diffs diff --git a/docs/build/unreleased/issue1597.rst b/docs/build/unreleased/issue1597.rst new file mode 100644 index 00000000..b7fcb08d --- /dev/null +++ b/docs/build/unreleased/issue1597.rst @@ -0,0 +1,6 @@ + .. change:: + :tags: check, autogenerate + :tickets: 1597 + + Add revision context to AutogenerateDiffsDetected so that command can be wrapped and diffs may be output in a different format. + Pull request courtesy of Louis-Amaury Chaib (@lachaib). \ No newline at end of file -- 2.47.3