File size: 3,284 Bytes
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""Database migration script: Drop old Better Auth tables and create new ones.

This script drops the old users table (from Better Auth) and recreates it
with the new schema for FastAPI JWT authentication.

[From]: specs/001-user-auth/plan.md
"""
import sys
import os

# Add parent directory to path to import from backend modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from sqlmodel import SQLModel, Session, create_engine, text
from core.config import get_settings

settings = get_settings()

# Create database engine
engine = create_engine(settings.database_url)


def drop_old_tables():
    """Drop old Better Auth tables."""
    print("Dropping old tables...")

    with Session(engine) as session:
        try:
            # Drop the old users table if it exists
            session.exec(text("DROP TABLE IF EXISTS users CASCADE"))
            session.commit()
            print("✓ Dropped old 'users' table")
        except Exception as e:
            print(f"✗ Error dropping tables: {e}")
            session.rollback()
            raise


def create_new_tables():
    """Create new tables with the updated schema."""
    print("\nCreating new tables...")

    from models.user import User  # Import to register with SQLModel

    SQLModel.metadata.create_all(engine)
    print("✓ Created new 'users' table with schema:")
    print("  - id: UUID (primary key)")
    print("  - email: VARCHAR(255) (unique, indexed)")
    print("  - hashed_password: VARCHAR(255)")
    print("  - created_at: TIMESTAMP")
    print("  - updated_at: TIMESTAMP")


def verify_schema():
    """Verify the new schema was created correctly."""
    print("\nVerifying schema...")

    with Session(engine) as session:
        result = session.exec(text("""
            SELECT column_name, data_type, is_nullable, column_default
            FROM information_schema.columns
            WHERE table_name = 'users'
            ORDER BY ordinal_position
        """))

        print("\nTable structure:")
        for row in result:
            print(f"  - {row.column_name}: {row.data_type}")


def main():
    """Run the migration."""
    print("=" * 60)
    print("DATABASE MIGRATION: Better Auth → FastAPI JWT")
    print("=" * 60)

    print("\n⚠️  WARNING: This will DELETE all existing user data!")
    print("    Old Better Auth tables will be dropped.\n")

    # Ask for confirmation
    response = input("Continue? (yes/no): ").strip().lower()

    if response not in ["yes", "y"]:
        print("\n❌ Migration cancelled.")
        sys.exit(0)

    try:
        # Step 1: Drop old tables
        drop_old_tables()

        # Step 2: Create new tables
        create_new_tables()

        # Step 3: Verify schema
        verify_schema()

        print("\n" + "=" * 60)
        print("✅ MIGRATION SUCCESSFUL!")
        print("=" * 60)
        print("\nNext steps:")
        print("1. Restart the backend server")
        print("2. Test registration at http://localhost:8000/docs")
        print("3. Create a new user account")

    except Exception as e:
        print("\n" + "=" * 60)
        print("❌ MIGRATION FAILED!")
        print("=" * 60)
        print(f"\nError: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()