Merging schema_extension #9
@@ -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.
|
with explicit IDs, which doesn't automatically advance PostgreSQL sequences.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
# Using config file
|
# Using default config.yaml
|
||||||
uv run python -m alpine_bits_python.util.fix_postgres_sequences
|
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
|
# Using DATABASE_URL environment variable
|
||||||
DATABASE_URL="postgresql+asyncpg://user:pass@host/db" \
|
DATABASE_URL="postgresql+asyncpg://user:pass@host/db" \
|
||||||
uv run python -m alpine_bits_python.util.fix_postgres_sequences
|
uv run python -m alpine_bits_python.util.fix_postgres_sequences
|
||||||
@@ -20,12 +24,14 @@ Usage:
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# Add parent directory to path so we can import alpine_bits_python
|
# Add parent directory to path so we can import alpine_bits_python
|
||||||
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
|
||||||
|
|
||||||
|
import yaml
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||||
|
|
||||||
@@ -122,6 +128,13 @@ async def main():
|
|||||||
"--database-url",
|
"--database-url",
|
||||||
help="PostgreSQL database URL (default: from config or DATABASE_URL env var)",
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -133,15 +146,38 @@ async def main():
|
|||||||
_LOGGER.warning("Failed to load config: %s. Using defaults.", e)
|
_LOGGER.warning("Failed to load config: %s. Using defaults.", e)
|
||||||
config = {}
|
config = {}
|
||||||
|
|
||||||
# Determine database URL
|
# Determine database URL (same logic as migrate_sqlite_to_postgres)
|
||||||
if args.database_url:
|
if args.database_url:
|
||||||
database_url = 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:
|
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:
|
if "postgresql" not in database_url and "postgres" not in database_url:
|
||||||
_LOGGER.error("This script only works with PostgreSQL databases.")
|
_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)
|
sys.exit(1)
|
||||||
|
|
||||||
# Run the fix
|
# Run the fix
|
||||||
|
|||||||
Reference in New Issue
Block a user