Minor cleanup for status.py
public
Minor cleanup for status.py
modified
src/mitch/cli/status.py
+3 −24
| @@ -1,9 +1,10 @@ |
| 1 | | -import os |
| 2 | 1 | from pathlib import Path |
| 3 | 2 | from typing import Annotated, NamedTuple |
| 4 | 3 | |
| 5 | 4 | import typer |
| 6 | 5 | from sqlalchemy import select |
| 6 | + |
| 7 | +from mitch.helpers import guess_ci_status_from_env |
| 7 | 8 | |
| 8 | 9 | from ..errors import MissingMigrationError |
| 9 | 10 | from ..helpers import connection_for |
| @@ -64,31 +65,9 @@ |
| 64 | 65 | ) |
| 65 | 66 | |
| 66 | 67 | |
| 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 | | - |
| 89 | 68 | def _resolve_ci_mode(ci: bool | None) -> bool: |
| 90 | 69 | if ci is None: |
| 91 | | - return _ci_from_environment() |
| 70 | + return guess_ci_status_from_env() |
| 92 | 71 | else: |
| 93 | 72 | return ci |
| 94 | 73 | |
modified
src/mitch/helpers.py
+27 −2
| @@ -13,21 +13,23 @@ |
| 13 | 13 | transactional: bool |
| 14 | 14 | migrations: Iterable[Migration] |
| 15 | 15 | |
| 16 | + |
| 16 | 17 | def batch_transactional_migrations(migrations: Iterable[Migration]) -> Iterable[Batch]: |
| 17 | 18 | """Splits a list of migrations into batches based on their transactional property. |
| 18 | | - |
| 19 | + |
| 19 | 20 | Each batch is a list of migrations that can be run together in a single transaction. |
| 20 | 21 | Non-transactional migrations are placed in their own batch. |
| 21 | 22 | """ |
| 22 | 23 | |
| 23 | 24 | # Start from 2 to differentiate from transactional (1) |
| 24 | | - counter = count(start=2) |
| 25 | + counter = count(start=2) |
| 25 | 26 | |
| 26 | 27 | for _, group in groupby(migrations, key=lambda m: 1 if m.transactional else next(counter)): |
| 27 | 28 | if _ > 1: |
| 28 | 29 | yield Batch(False, group) |
| 29 | 30 | else: |
| 30 | 31 | yield Batch(True, group) |
| 32 | + |
| 31 | 33 | |
| 32 | 34 | def create_engine(database_url: str | None) -> sa.engine.Engine: |
| 33 | 35 | resolved_database_url = database_url or os.getenv("MITCH_DATABASE_URL") or os.getenv("DATABASE_URL") |
| @@ -40,6 +42,7 @@ |
| 40 | 42 | return sa.create_engine(resolved_database_url) |
| 41 | 43 | """Creates a SQLAlchemy engine for the given database URL.""" |
| 42 | 44 | |
| 45 | + |
| 43 | 46 | @contextmanager |
| 44 | 47 | def connection_for(database_url: str | None): |
| 45 | 48 | """Context manager that yields a SQLAlchemy connection and ensures proper cleanup.""" |
| @@ -50,3 +53,25 @@ |
| 50 | 53 | connection.close() |
| 51 | 54 | finally: |
| 52 | 55 | 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 | + ) |