Fixed yaml loading
This commit is contained in:
@@ -46,9 +46,8 @@ import yaml
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
|
||||
|
||||
from alpine_bits_python.config_loader import load_config
|
||||
from alpine_bits_python.db import get_database_schema, get_database_url
|
||||
from alpine_bits_python.logging_config import get_logger, setup_logging
|
||||
from alpine_bits_python.logging_config import get_logger
|
||||
|
||||
_LOGGER = get_logger(__name__)
|
||||
|
||||
@@ -209,43 +208,53 @@ async def main():
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
try:
|
||||
# Load config
|
||||
config = load_config()
|
||||
setup_logging(config)
|
||||
except Exception as e:
|
||||
_LOGGER.warning("Failed to load config: %s. Using defaults.", e)
|
||||
config = {}
|
||||
|
||||
# Determine database URL (same logic as migrate_sqlite_to_postgres)
|
||||
# Determine database URL and schema
|
||||
schema_name = None
|
||||
if args.database_url:
|
||||
database_url = args.database_url
|
||||
# Get schema from default config if available
|
||||
schema_name = get_database_schema(config)
|
||||
elif args.config:
|
||||
# Load config file manually (simpler YAML without secrets)
|
||||
if args.config:
|
||||
# Load config file as plain YAML (no validation)
|
||||
_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"]
|
||||
schema_name = target_config.get("database", {}).get("schema")
|
||||
_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"
|
||||
)
|
||||
with open(args.config) as f:
|
||||
config = yaml.safe_load(f)
|
||||
|
||||
# Get database URL
|
||||
if not config or "database" not in config or "url" not in config["database"]:
|
||||
_LOGGER.error("Config file must contain database.url")
|
||||
sys.exit(1)
|
||||
|
||||
database_url = config["database"]["url"]
|
||||
schema_name = config.get("database", {}).get("schema")
|
||||
_LOGGER.info("Successfully loaded config")
|
||||
except FileNotFoundError:
|
||||
_LOGGER.error("Config file not found: %s", args.config)
|
||||
sys.exit(1)
|
||||
except yaml.YAMLError as e:
|
||||
_LOGGER.error("Failed to parse YAML config: %s", e)
|
||||
sys.exit(1)
|
||||
elif args.database_url:
|
||||
database_url = args.database_url
|
||||
# Get schema from environment variable
|
||||
schema_name = os.environ.get("DATABASE_SCHEMA")
|
||||
else:
|
||||
# Try environment variable or default config.yaml
|
||||
database_url = os.environ.get("DATABASE_URL")
|
||||
schema_name = os.environ.get("DATABASE_SCHEMA")
|
||||
|
||||
if not database_url:
|
||||
# Try from default config
|
||||
database_url = get_database_url(config)
|
||||
# Get schema from config or environment
|
||||
schema_name = get_database_schema(config)
|
||||
# Try to load from default config.yaml as plain YAML
|
||||
try:
|
||||
with open("config/config.yaml") as f:
|
||||
config = yaml.safe_load(f)
|
||||
database_url = config.get("database", {}).get("url")
|
||||
if not schema_name:
|
||||
schema_name = config.get("database", {}).get("schema")
|
||||
except Exception:
|
||||
pass # Ignore if default config doesn't exist
|
||||
|
||||
if not database_url:
|
||||
_LOGGER.error("No database URL provided")
|
||||
_LOGGER.error("Provide via --config, --database-url, or DATABASE_URL env var")
|
||||
sys.exit(1)
|
||||
|
||||
if "postgresql" not in database_url and "postgres" not in database_url:
|
||||
_LOGGER.error("This script only works with PostgreSQL databases.")
|
||||
|
||||
Reference in New Issue
Block a user