File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import os
12import re
23import subprocess
34from 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 :
Original file line number Diff line number Diff line change 22
33import pytest
44
5- from migration_lint .extractor .alembic import AlembicExtractor
5+ from migration_lint .extractor .alembic import AlembicExtractor , os
66from 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
You can’t perform that action at this time.
0 commit comments