File size: 1,489 Bytes
5c244a3 |
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 |
"""
Initialize Neon Database with Python
Run: python scripts/init_db.py
"""
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from src.models import Base
from src.utils.config import settings
from src.utils.logging import configure_logging, get_logger
# Use Neon database
NEON_DB_URL = "postgresql+asyncpg://neondb_owner:npg_4oK0utXaHpci@ep-broad-darkness-abnsobdy-pooler.eu-west-2.aws.neon.tech/neondb?sslmode=require"
configure_logging()
logger = get_logger(__name__)
async def init_database():
"""Initialize database schema"""
logger.info("๐ Initializing Neon Database...")
# Create async engine for Neon
engine = create_async_engine(
NEON_DB_URL,
echo=True,
)
try:
# Create all tables
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
logger.info("โ
Database initialized successfully!")
logger.info("๐ Tables created:")
for table in Base.metadata.tables.keys():
logger.info(f" - {table}")
except Exception as e:
logger.error(f"โ Database initialization failed: {e}")
raise
finally:
await engine.dispose()
if __name__ == "__main__":
asyncio.run(init_database())
|