Spaces:
Running
Running
File size: 1,545 Bytes
20cccb6 40403f3 20cccb6 40403f3 20cccb6 40403f3 20cccb6 40403f3 a221e9d 40403f3 20cccb6 40403f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
"""
init_db.py
This script initializes the database by creating all tables defined in the ORM models.
It uses async SQLAlchemy operations to create tables in the PostgreSQL database.
Run this script once to set up your database schema.
"""
# Standard Library Imports
import asyncio
# Local Application Imports
from src.config import Config, logger
from src.database.database import engine
from src.database.models import Base
async def init_db_async():
"""
Asynchronously create all database tables defined in the ORM models.
This function connects to the database using the configured async engine
and creates all tables that are mapped to SQLAlchemy models derived from
the Base class. It uses SQLAlchemy's create_all method with the async
engine context.
Returns:
None
"""
async with engine.begin() as conn:
# In SQLAlchemy 2.0 with async, we use the connection directly
await conn.run_sync(Base.metadata.create_all)
logger.info("Database tables created successfully using async SQLAlchemy.")
def main():
"""
Main entry point for the database initialization script.
This function creates the configuration, ensures the async engine is
initialized, and runs the async initialization function within an
event loop.
Returns:
None
"""
# Make sure config is loaded first to initialize the engine
Config.get()
# Run the async initialization function
asyncio.run(init_db_async())
if __name__ == "__main__":
main()
|