| import asyncio
|
| from app.database import engine
|
| from sqlmodel import SQLModel
|
| from app.models.user import User
|
| from app.models.task import Task
|
| from sqlalchemy import text
|
|
|
| async def create_tables():
|
| print("Creating database tables and ensuring schema is up to date...")
|
|
|
|
|
| async with engine.begin() as conn:
|
| await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
|
| try:
|
| async with engine.begin() as conn:
|
|
|
| result = await conn.execute(text("""
|
| SELECT column_name
|
| FROM information_schema.columns
|
| WHERE table_name = 'tasks' AND column_name = 'reminder'
|
| """))
|
|
|
| if not result.first():
|
|
|
| await conn.execute(text("ALTER TABLE tasks ADD COLUMN reminder TIMESTAMP"))
|
| print("Added reminder column to tasks table")
|
| else:
|
| print("Reminder column already exists in tasks table")
|
| except Exception as e:
|
|
|
| try:
|
|
|
|
|
| async with engine.begin() as conn:
|
| await conn.execute(text("ALTER TABLE tasks ADD COLUMN reminder TIMESTAMP"))
|
| print("Added reminder column to tasks table")
|
| except Exception as e2:
|
|
|
| print(f"Could not add reminder column: {e2}. Assuming it already exists.")
|
|
|
| print("✅ Database schema synchronized successfully!")
|
|
|
| if __name__ == "__main__":
|
| asyncio.run(create_tables())
|
|
|