Spaces:
Sleeping
Sleeping
File size: 1,425 Bytes
830a2d0 6eda67d 830a2d0 6eda67d 830a2d0 fd2ce9d 830a2d0 fd2ce9d 830a2d0 | 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 | import logging
import databases
import sqlalchemy
from app.core.config import DATABASE_URL
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Database URL is constructed in app.core.config via build_database_uri
# Initialize the database connection and metadata
try:
# Create database connection
database = databases.Database(DATABASE_URL)
metadata = sqlalchemy.MetaData()
logger.info("Database connection initialized successfully.")
except Exception as e:
logger.error("Failed to initialize database connection: %s", e)
raise
# Helper function to initialize database
async def connect_to_database():
"""
Connects to the database when the application starts.
"""
try:
await database.connect()
logger.info("Successfully connected to the database.")
except Exception as e:
logger.error("Error connecting to the database: %s", e)
raise
# Helper function to disconnect database
async def disconnect_from_database():
"""
Disconnects from the database when the application shuts down.
"""
try:
await database.disconnect()
logger.info("Successfully disconnected from the database.")
except Exception as e:
logger.error("Error disconnecting from the database: %s", e)
raise |