Skip to content

Commit 4b9275c

Browse files
authored
Merge pull request #6 from PandaDoc/squawk-pg-version
Add pg_version argument for squawk
2 parents 9865c11 + 2627118 commit 4b9275c

5 files changed

Lines changed: 54 additions & 13 deletions

File tree

migration_lint/analyzer/squawk.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class SquawkLinter(BaseLinter):
1919
ignored_rules = [
2020
"ban-drop-column", # Backward-incompatible, checked by compatibility analyzer.
2121
"ban-drop-table", # Backward-incompatible, checked by compatibility analyzer.
22+
"adding-not-nullable-field", # Backward-incompatible, checked by compatibility analyzer.
2223
"prefer-big-int", # Deprecated.
2324
"prefer-identity",
2425
"prefer-timestamptz",
@@ -27,9 +28,12 @@ class SquawkLinter(BaseLinter):
2728
"transaction-nesting", # TODO: Add transactions tracking.
2829
]
2930

30-
def __init__(self, config_path: Optional[str] = None) -> None:
31+
def __init__(
32+
self, config_path: Optional[str] = None, pg_version: Optional[str] = None
33+
) -> None:
3134
bin_dir = os.path.join(migration_lint.__path__[0], "bin")
3235
self.config_path = config_path
36+
self.pg_version = pg_version
3337
if sys.platform == "linux":
3438
self.squawk = os.path.join(bin_dir, "squawk-linux-x86")
3539
elif sys.platform == "darwin":
@@ -41,8 +45,9 @@ def squawk_command(self, migration_sql: str) -> str:
4145
"""Get squawk command."""
4246
exclude = f"--exclude={','.join(self.ignored_rules)}"
4347
config = f"--config={self.config_path}" if self.config_path else ""
48+
pg_version = f"--pg-version={self.pg_version}" if self.pg_version else ""
4449

45-
return " ".join([self.squawk, exclude, config]).strip()
50+
return " ".join([self.squawk, exclude, config, pg_version]).strip()
4651

4752
def lint(
4853
self,

migration_lint/django/management/commands/migration_lint.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,28 @@ def add_arguments(self, parser):
7272
default=os.getenv("MIGRATION_LINTER_SQUAWK_CONFIG_PATH"),
7373
help="squawk config path",
7474
)
75+
parser.add_argument(
76+
"--squawk-pg-version",
77+
dest="squawk_pg_version",
78+
type=str,
79+
default=os.getenv("MIGRATION_LINTER_SQUAWK_PG_VERSION"),
80+
help="squawk version of PostgreSQL",
81+
)
7582

76-
def handle(self, loader_type, squawk_config_path, **options):
83+
def handle(self, loader_type, squawk_config_path, squawk_pg_version, **options):
7784
logger.info("Start analysis..")
7885

7986
loader = SourceLoader.get(loader_type)(**options)
8087
extractor = Extractor.get("django_management")(**options)
8188
analyzer = Analyzer(
8289
loader=loader,
8390
extractor=extractor,
84-
linters=[CompatibilityLinter(), SquawkLinter(squawk_config_path)],
91+
linters=[
92+
CompatibilityLinter(),
93+
SquawkLinter(
94+
config_path=squawk_config_path,
95+
pg_version=squawk_pg_version,
96+
),
97+
],
8598
)
8699
analyzer.analyze()

migration_lint/main.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,24 @@
6464
help="squawk config path",
6565
default=os.getenv("MIGRATION_LINTER_SQUAWK_CONFIG_PATH"),
6666
)
67+
@click.option(
68+
"--squawk-pg-version",
69+
"squawk_pg_version",
70+
help="squawk version of PostgreSQL",
71+
default=os.getenv("MIGRATION_LINTER_SQUAWK_PG_VERSION"),
72+
)
6773
@click.option(
6874
"--alembic-command",
6975
"alembic_command",
7076
help="command to get Alembic migrations sql",
7177
default=os.getenv("MIGRATION_LINTER_ALEMBIC_COMMAND"),
7278
)
7379
def main(
74-
loader_type: str, extractor_type: str, squawk_config_path: str, **kwargs
80+
loader_type: str,
81+
extractor_type: str,
82+
squawk_config_path: str,
83+
squawk_pg_version: str,
84+
**kwargs,
7585
) -> None:
7686
logger.info("Start analysis..")
7787

@@ -80,7 +90,13 @@ def main(
8090
analyzer = Analyzer(
8191
loader=loader,
8292
extractor=extractor,
83-
linters=[CompatibilityLinter(), SquawkLinter(squawk_config_path)],
93+
linters=[
94+
CompatibilityLinter(),
95+
SquawkLinter(
96+
config_path=squawk_config_path,
97+
pg_version=squawk_pg_version,
98+
),
99+
],
84100
)
85101
analyzer.analyze()
86102

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "migration-lint"
3-
version = "0.2.8"
3+
version = "0.2.9"
44
description = "Tool for lint operations in DB migrations SQL"
55
authors = ["Alexey Nikitenko <alexey.nikitenko@pandadoc.com>"]
66
readme = "README.md"

tests/test_squawk_linter.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from typing import Union
21
from unittest.mock import patch
32

43
import pytest
@@ -26,19 +25,27 @@ def test_unsupported_platform():
2625

2726

2827
@pytest.mark.parametrize(
29-
"config_path, result_flags",
30-
[(None, ""), (".squawk.toml", "--config=.squawk.toml")],
31-
ids=["Without config", "With config"],
28+
"params, result_flags",
29+
[
30+
({}, ""),
31+
({"config_path": ".squawk.toml"}, "--config=.squawk.toml"),
32+
({"pg_version": "13.0"}, " --pg-version=13.0"),
33+
(
34+
{"config_path": ".squawk.toml", "pg_version": "13.0"},
35+
"--config=.squawk.toml --pg-version=13.0",
36+
),
37+
],
38+
ids=["Without params", "With config", "With pg version", "With all params"],
3239
)
3340
@patch("migration_lint.__path__", ["path"])
3441
@patch("sys.platform", "linux")
35-
def test_squawk_command(config_path: Union[str, None], result_flags: str):
42+
def test_squawk_command(params: dict, result_flags: str):
3643
ignored_rules = ["ignored-rule"]
3744

3845
with patch.object(
3946
SquawkLinter, "ignored_rules", new_callable=lambda: ignored_rules
4047
):
41-
linter = SquawkLinter(config_path)
48+
linter = SquawkLinter(**params)
4249

4350
result = linter.squawk_command(FAKE_STATEMENT)
4451

0 commit comments

Comments
 (0)