File size: 2,083 Bytes
69be42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3879e
69be42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc3879e
69be42f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""Database migration runner.

[Task]: T022, T023
[From]: specs/001-user-auth/tasks.md

This script runs SQL migrations against the database.

Usage:
    uv run python migrations/run_migration.py
"""
import os
import sys
from pathlib import Path
from sqlmodel import Session, text

# Add parent directory to path for imports
sys.path.insert(0, str(Path(__file__).parent.parent))

from core.database import engine


def run_migration(migration_file: str):
    """Run a single SQL migration file.

    Args:
        migration_file: Name of the migration file in migrations/ directory
    """
    migration_path = Path(__file__).parent / migration_file

    if not migration_path.exists():
        print(f"❌ Migration file not found: {migration_path}")
        return False

    print(f"📜 Running migration: {migration_file}")

    with open(migration_path, "r") as f:
        sql = f.read()

    try:
        with Session(engine) as session:
            # Execute the migration using text()
            session.exec(text(sql))
            session.commit()

        print(f"✅ Migration completed successfully: {migration_file}")
        return True
    except Exception as e:
        print(f"❌ Migration failed: {e}")
        return False


def main():
    """Run pending migrations."""
    # Migration files in order
    migrations = [
        "001_add_user_id_index.sql",
        "002_add_conversation_and_message_tables.sql",  # Phase III: AI Chatbot
    ]

    print("🚀 Starting database migrations...\n")

    success_count = 0
    for migration in migrations:
        if run_migration(migration):
            success_count += 1
        print()

    print(f"✅ {success_count}/{len(migrations)} migrations completed successfully")

    if success_count == len(migrations):
        print("\n🎉 All migrations completed!")
        print("\n📊 Database schema is ready for authentication.")
        return 0
    else:
        print("\n⚠️  Some migrations failed. Please check the errors above.")
        return 1


if __name__ == "__main__":
    sys.exit(main())