Public repository: timo / mitch

Reader

A migration stitcher (therefore mitch) for PostgreSQL, SQLite, MariaDB, and others

Minor cleanup for status.py

public

Minor cleanup for status.py
Changesetb7b1e183d424c41bbed2868abc49f812864c1f92
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsddb1462599a1
Children
Pushlog
by timo via ssh

modified src/mitch/cli/status.py +3 −24

@@ -1,9 +1,10 @@
1
-import os
21
 from pathlib import Path
32
 from typing import Annotated, NamedTuple
43
 
54
 import typer
65
 from sqlalchemy import select
6
+
7
+from mitch.helpers import guess_ci_status_from_env
78
 
89
 from ..errors import MissingMigrationError
910
 from ..helpers import connection_for
@@ -64,31 +65,9 @@
6465
     )
6566
 
6667
 
67
-def _env_truthy(name: str) -> bool:
68
-    value = os.getenv(name)
69
-    if value is None:
70
-        return False
71
-    return value.strip().lower() not in ("", "0", "false", "no", "off")
72
-
73
-
74
-def _ci_from_environment() -> bool:
75
-    return any(
76
-        _env_truthy(name)
77
-        for name in (
78
-            "CI",
79
-            "GITHUB_ACTIONS",
80
-            "GITLAB_CI",
81
-            "JENKINS_URL",
82
-            "BUILDKITE",
83
-            "TF_BUILD",
84
-            "CIRCLECI",
85
-        )
86
-    )
87
-
88
-
8968
 def _resolve_ci_mode(ci: bool | None) -> bool:
9069
     if ci is None:
91
-        return _ci_from_environment()
70
+        return guess_ci_status_from_env()
9271
     else:
9372
         return ci
9473
 

modified src/mitch/helpers.py +27 −2

@@ -13,21 +13,23 @@
1313
     transactional: bool
1414
     migrations: Iterable[Migration]
1515
 
16
+
1617
 def batch_transactional_migrations(migrations: Iterable[Migration]) -> Iterable[Batch]:
1718
     """Splits a list of migrations into batches based on their transactional property.
18
-    
19
+
1920
     Each batch is a list of migrations that can be run together in a single transaction.
2021
     Non-transactional migrations are placed in their own batch.
2122
     """
2223
 
2324
     # Start from 2 to differentiate from transactional (1)
24
-    counter = count(start=2) 
25
+    counter = count(start=2)
2526
 
2627
     for _, group in groupby(migrations, key=lambda m: 1 if m.transactional else next(counter)):
2728
         if _ > 1:
2829
             yield Batch(False, group)
2930
         else:
3031
             yield Batch(True, group)
32
+
3133
 
3234
 def create_engine(database_url: str | None) -> sa.engine.Engine:
3335
     resolved_database_url = database_url or os.getenv("MITCH_DATABASE_URL") or os.getenv("DATABASE_URL")
@@ -40,6 +42,7 @@
4042
     return sa.create_engine(resolved_database_url)
4143
     """Creates a SQLAlchemy engine for the given database URL."""
4244
 
45
+
4346
 @contextmanager
4447
 def connection_for(database_url: str | None):
4548
     """Context manager that yields a SQLAlchemy connection and ensures proper cleanup."""
@@ -50,3 +53,25 @@
5053
         connection.close()
5154
     finally:
5255
         engine.dispose()
56
+
57
+
58
+def _env_truthy(name: str) -> bool:
59
+    value = os.getenv(name)
60
+    if value is None:
61
+        return False
62
+    return value.strip().lower() not in ("", "0", "false", "no", "off")
63
+
64
+
65
+def guess_ci_status_from_env() -> bool:
66
+    return any(
67
+        _env_truthy(name)
68
+        for name in (
69
+            "CI",
70
+            "GITHUB_ACTIONS",
71
+            "GITLAB_CI",
72
+            "JENKINS_URL",
73
+            "BUILDKITE",
74
+            "TF_BUILD",
75
+            "CIRCLECI",
76
+        )
77
+    )