import os
import subprocess
import sys
from dotenv import load_dotenv

# Load environment variables from .env file
load_dotenv()

# Get the database URL from the environment
db_url = os.getenv("YOYO_DB_URL")
if not db_url:
    raise ValueError("YOYO_DB_URL not set in environment or .env file")

# Get the yoyo subcommand (e.g., 'apply', 'rollback') and any other arguments
subcommand = sys.argv[1]
other_args = sys.argv[2:]

# Construct the yoyo command with the correct order
yoyo_command = ["yoyo", subcommand, "--database", db_url] + other_args

# Execute the yoyo command
try:
    subprocess.run(yoyo_command, check=True)
except subprocess.CalledProcessError as e:
    print(f"Error running yoyo command: {e}")
    sys.exit(e.returncode)
