[V0.5] Working application with all 4 screens as of yet.
This commit is contained in:
@@ -4,6 +4,7 @@ from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
from sqlalchemy import create_engine # Add create_engine import
|
||||
|
||||
from alembic import context
|
||||
|
||||
@@ -25,6 +26,29 @@ config = context.config
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# --- Construct DB URL from environment variables ---
|
||||
# Use environment variables similar to docker-compose
|
||||
db_user = os.getenv("POSTGRES_USER", "maia") # Default to 'maia' if not set
|
||||
db_password = os.getenv("POSTGRES_PASSWORD", "maia") # Default to 'maia' if not set
|
||||
db_host = os.getenv("DB_HOST", "db") # Default to 'db' service name
|
||||
db_port = os.getenv("DB_PORT", "5432") # Default to '5432'
|
||||
db_name = os.getenv("DB_NAME", "maia") # Default to 'maia'
|
||||
|
||||
# Construct the URL, falling back to alembic.ini if needed
|
||||
url = os.getenv("DB_URL")
|
||||
if not url:
|
||||
# Try constructing from parts if DB_URL isn't set
|
||||
url = f"postgresql://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
|
||||
# As a final fallback, use the URL from alembic.ini
|
||||
config_url = config.get_main_option("sqlalchemy.url")
|
||||
if not url and config_url:
|
||||
url = config_url
|
||||
|
||||
# Update the config object so engine_from_config can potentially use it,
|
||||
# though we'll primarily use the constructed 'url' directly.
|
||||
config.set_main_option("sqlalchemy.url", url)
|
||||
# ----------------------------------------------------
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
# from myapp import mymodel
|
||||
@@ -51,9 +75,8 @@ def run_migrations_offline() -> None:
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
url=url, # Use the constructed URL
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
@@ -70,11 +93,14 @@ def run_migrations_online() -> None:
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
# Create engine directly using the constructed URL
|
||||
connectable = create_engine(url, poolclass=pool.NullPool)
|
||||
# Original approach using engine_from_config:
|
||||
# connectable = engine_from_config(
|
||||
# config.get_section(config.config_ini_section, {}),
|
||||
# prefix="sqlalchemy.",
|
||||
# poolclass=pool.NullPool,
|
||||
# )
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(connection=connection, target_metadata=target_metadata)
|
||||
|
||||
Reference in New Issue
Block a user