Spaces:
Runtime error

fullstack_todo_app / init_db.py
Maheen Ghouri
fix directory errors
aaae345
Raw History Blame Contribute Delete
1.85 kB
"""Script to initialize database tables and create demo user"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), '.env'))
from sqlmodel import Session, SQLModel, text
from db import engine, create_db_and_tables
from models import User
from auth import hash_password
import uuid
from datetime import datetime
def init_db():
"""Initialize database with tables and demo user"""
print("Creating database tables...")
create_db_and_tables()
print("[OK] Tables created successfully!")
# Create demo user
print("Creating demo user...")
with Session(engine) as session:
# Check if demo user already exists
result = session.execute(text("SELECT COUNT(*) FROM users WHERE email = 'user@example.com'"))
count = result.scalar()
if count > 0:
print("[OK] Demo user already exists!")
else:
# Generate proper bcrypt hash
password_hash = hash_password("password123")
user_id = str(uuid.uuid4())
now = datetime.now().isoformat()
# Insert demo user
session.execute(text(f"""
INSERT INTO users (id, email, password_hash, name, created_at, updated_at)
VALUES (
'{user_id}',
'user@example.com',
'{password_hash}',
'Test User',
'{now}',
'{now}'
)
"""))
session.commit()
print("[OK] Demo user created!")
print(" Email: user@example.com")
print(" Password: password123")
if __name__ == "__main__":
init_db()