Skip to content

Commit 669b1df

Browse files
author
Alexey Nikitenko
committed
Alembic path customization
1 parent 89b561a commit 669b1df

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

migration_lint/extractor/alembic.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import re
23
import subprocess
34
from io import StringIO
@@ -15,12 +16,15 @@ class AlembicExtractor(BaseExtractor):
1516
def __init__(self, **kwargs):
1617
super().__init__(**kwargs)
1718
self.command = kwargs.get("alembic_command") or "make sqlmigrate"
19+
self.migration_path = os.environ.get(
20+
"MIGRATION_LINT_ALEMBIC_MIGRATIONS_PATH", "/migrations/versions/"
21+
)
1822

1923
def is_migration(self, path: str) -> bool:
2024
"""Check if the specified file is a migration."""
2125

2226
return (
23-
"/migrations/versions/" in path
27+
self.migration_path in path
2428
and path.endswith(".py")
2529
and "__init__.py" not in path
2630
)
@@ -34,7 +38,7 @@ def is_allowed_with_backward_incompatible_migration(self, path: str) -> bool:
3438
r".*/tables\.py",
3539
r".*/constants\.py",
3640
r".*/enums\.py",
37-
r".*/migrations/versions/.*\.py",
41+
rf".*{self.migration_path}.*\.py",
3842
r"^(?!.*\.py).*$",
3943
]
4044
for pattern in allowed_patterns:

tests/test_alembic_extractor.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from migration_lint.extractor.alembic import AlembicExtractor
5+
from migration_lint.extractor.alembic import AlembicExtractor, os
66
from migration_lint.source_loader.model import SourceDiff
77

88

@@ -53,3 +53,23 @@ def test_alembic_extractor_command__ok(command, expected_command):
5353
subprocess_mock.return_value = "".encode("utf-8")
5454
extractor.create_metadata(changed_files)
5555
subprocess_mock.assert_called_once_with(expected_command.split())
56+
57+
58+
def test_alembic_extractor_path__ok():
59+
changed_files = [
60+
SourceDiff(path="src/db/random/path/202202151945_fbea801d4464_auto.py"),
61+
]
62+
63+
with mock.patch(
64+
"migration_lint.extractor.alembic.subprocess.check_output"
65+
) as subprocess_mock, mock.patch.dict(
66+
os.environ,
67+
{"MIGRATION_LINT_ALEMBIC_MIGRATIONS_PATH": "/random/path"},
68+
clear=True,
69+
):
70+
extractor = AlembicExtractor()
71+
subprocess_mock.return_value = "".encode("utf-8")
72+
metadata = extractor.create_metadata(changed_files)
73+
74+
assert len(metadata.migrations) == 1
75+
assert metadata.changed_files[0].allowed_with_backward_incompatible is True

0 commit comments

Comments
 (0)