From bd54fc72ad03449d99192bfd5ea77daefff2beac Mon Sep 17 00:00:00 2001 From: Jonas Linter <{email_address}> Date: Fri, 17 Oct 2025 22:29:44 +0200 Subject: [PATCH] Passing parameters from config --- .../util/fix_postgres_sequences.py | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/alpine_bits_python/util/fix_postgres_sequences.py b/src/alpine_bits_python/util/fix_postgres_sequences.py index f1fdba6..d71867d 100644 --- a/src/alpine_bits_python/util/fix_postgres_sequences.py +++ b/src/alpine_bits_python/util/fix_postgres_sequences.py @@ -6,9 +6,13 @@ in each table. This is necessary because the migration script inserts records with explicit IDs, which doesn't automatically advance PostgreSQL sequences. Usage: - # Using config file + # Using default config.yaml uv run python -m alpine_bits_python.util.fix_postgres_sequences + # Using a specific config file + uv run python -m alpine_bits_python.util.fix_postgres_sequences \ + --config config/postgres.yaml + # Using DATABASE_URL environment variable DATABASE_URL="postgresql+asyncpg://user:pass@host/db" \ uv run python -m alpine_bits_python.util.fix_postgres_sequences @@ -20,12 +24,14 @@ Usage: import argparse import asyncio +import os import sys from pathlib import Path # Add parent directory to path so we can import alpine_bits_python sys.path.insert(0, str(Path(__file__).parent.parent.parent)) +import yaml from sqlalchemy import text from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine @@ -122,6 +128,13 @@ async def main(): "--database-url", help="PostgreSQL database URL (default: from config or DATABASE_URL env var)", ) + parser.add_argument( + "--config", + help=( + "Path to config file containing PostgreSQL database URL " + "(keeps password out of bash history)" + ), + ) args = parser.parse_args() @@ -133,15 +146,38 @@ async def main(): _LOGGER.warning("Failed to load config: %s. Using defaults.", e) config = {} - # Determine database URL + # Determine database URL (same logic as migrate_sqlite_to_postgres) if args.database_url: database_url = args.database_url + elif args.config: + # Load config file manually (simpler YAML without secrets) + _LOGGER.info("Loading database config from: %s", args.config) + try: + config_path = Path(args.config) + config_text = config_path.read_text() + target_config = yaml.safe_load(config_text) + database_url = target_config["database"]["url"] + _LOGGER.info("Successfully loaded config") + except (FileNotFoundError, ValueError, KeyError): + _LOGGER.exception("Failed to load config") + _LOGGER.info( + "Config file should contain: database.url with PostgreSQL connection" + ) + sys.exit(1) else: - database_url = get_database_url(config) + database_url = os.environ.get("DATABASE_URL") + if not database_url: + # Try from default config + database_url = get_database_url(config) if "postgresql" not in database_url and "postgres" not in database_url: _LOGGER.error("This script only works with PostgreSQL databases.") - _LOGGER.error("Current database URL: %s", database_url) + url_type = database_url.split("+")[0] if "+" in database_url else "unknown" + _LOGGER.error("Current database URL type detected: %s", url_type) + _LOGGER.error("\nSpecify PostgreSQL database using one of:") + _LOGGER.error(" - --config config/postgres.yaml") + _LOGGER.error(" - DATABASE_URL environment variable") + _LOGGER.error(" - --database-url postgresql+asyncpg://user:pass@host/db") sys.exit(1) # Run the fix