ebba983a1d
- Change TokenPayload.sub type from int to str per JWT RFC 7519 - Add user_id property to TokenPayload for int conversion - Update token creation to serialize user_id as string - Update token consumers to use payload.user_id - Change dashboard port from 3000 to 8080 - Add pydantic[email] for email validation - Update default admin email to admin@sparc.dev 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
76 lines
2.3 KiB
Python
76 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Initialize the SPARC database schema.
|
|
|
|
This script creates the necessary tables and indexes for storing
|
|
LLM messages for testing and analytics.
|
|
|
|
Usage:
|
|
python scripts/init_database.py
|
|
"""
|
|
|
|
import secrets
|
|
import string
|
|
import sys
|
|
import os
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from SPARC import config
|
|
from SPARC.database import DatabaseClient
|
|
|
|
DEFAULT_ADMIN_EMAIL = "admin@sparc.dev"
|
|
|
|
|
|
def generate_password(length: int = 16) -> str:
|
|
"""Generate a secure random password."""
|
|
alphabet = string.ascii_letters + string.digits
|
|
return "".join(secrets.choice(alphabet) for _ in range(length))
|
|
|
|
|
|
def main():
|
|
"""Initialize the database schema."""
|
|
print("Initializing SPARC database...")
|
|
print(f"Database URL: {config.database_url}")
|
|
|
|
try:
|
|
db_client = DatabaseClient(config.database_url)
|
|
db_client.initialize_schema()
|
|
print("Database schema initialized successfully!")
|
|
print("\nTables created:")
|
|
print(" - llm_messages: Stores all LLM prompts and responses")
|
|
print(" - users: Stores user accounts")
|
|
print("\nIndexes created:")
|
|
print(" - idx_messages_timestamp: For time-based queries")
|
|
print(" - idx_messages_company: For company-specific queries")
|
|
print(" - idx_users_email: For user lookups")
|
|
|
|
# Create default admin user if not exists
|
|
existing_admin = db_client.get_user_by_email(DEFAULT_ADMIN_EMAIL)
|
|
if existing_admin:
|
|
print(f"\nDefault admin user already exists: {DEFAULT_ADMIN_EMAIL}")
|
|
else:
|
|
password = generate_password()
|
|
admin_user = db_client.create_user(
|
|
email=DEFAULT_ADMIN_EMAIL,
|
|
password=password,
|
|
role="admin",
|
|
)
|
|
if admin_user:
|
|
print("\n" + "=" * 50)
|
|
print("DEFAULT ADMIN CREDENTIALS")
|
|
print("=" * 50)
|
|
print(f"Email: {DEFAULT_ADMIN_EMAIL}")
|
|
print(f"Password: {password}")
|
|
print("=" * 50)
|
|
print("Please save these credentials securely!")
|
|
print("=" * 50)
|
|
|
|
except Exception as e:
|
|
print(f"Error initializing database: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|