File size: 1,546 Bytes
ab028ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Database setup script to create tables for the auth system
"""
import os
from dotenv import load_dotenv
from flask import Flask
from models import db, User

def create_app():
    """Create Flask app for database operations"""
    app = Flask(__name__)
    
    # Configure Flask
    app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
    app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('SQLALCHEMY_DATABASE_URI')
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    
    # Initialize database
    db.init_app(app)
    
    return app

def create_tables():
    """Create database tables"""
    print("Creating database tables...")
    
    app = create_app()
    
    with app.app_context():
        try:
            # Create all tables
            db.create_all()
            print("Database tables created successfully!")
            
            # Check if tables exist
            inspector = db.inspect(db.engine)
            tables = inspector.get_table_names()
            print(f"Tables created: {tables}")
            
        except Exception as e:
            print(f"Error creating tables: {e}")
            raise

if __name__ == "__main__":
    # Load environment variables
    load_dotenv()
    
    # Verify database URL
    db_url = os.environ.get('SQLALCHEMY_DATABASE_URI')
    if not db_url:
        print("DATABASE_URL not found in environment variables")
        exit(1)
    
    print(f"Using database: {db_url.split('@')[1] if '@' in db_url else 'local'}")
    
    # Create tables
    create_tables()