Skip to content

Commit

Permalink
fix alembic configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
liberty-rising committed Jan 21, 2024
1 parent 157601b commit df4231e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/alembic.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[alembic]
# ... other settings ...
sqlalchemy.url = driver://user:pass@localhost/dummy
sqlalchemy.url = postgresql://admin:[email protected]:5432/db
script_location = alembic
46 changes: 33 additions & 13 deletions backend/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,43 @@
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from models.base import Base # noqa: E402
from settings import APP_ENV, DATABASE_URL # noqa: E402


def run_migrations_online():
connectable = context.config.attributes.get("connection")

# For app database
if connectable is None:
db_url = context.get_x_argument(as_dictionary=True).get("db", None)
if db_url:
connectable = engine_from_config(
context.config.get_section(context.config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
url=db_url,
)
# Run migrations for app database
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=Base)
with context.begin_transaction():
context.run_migrations()
if APP_ENV == "dev":
db_url = "postgresql://admin:[email protected]:5432/db"
else:
db_url = DATABASE_URL
connectable = engine_from_config(
context.config.get_section(context.config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
url=db_url,
)
# Run migrations for app database
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=Base)
with context.begin_transaction():
context.run_migrations()


def run_migrations_offline():
context.configure(url=DATABASE_URL)

with context.begin_transaction():
context.run_migrations()


def run_migrations():
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()


run_migrations()

0 comments on commit df4231e

Please sign in to comment.