-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
104 lines (85 loc) · 3.32 KB
/
init_db.py
File metadata and controls
104 lines (85 loc) · 3.32 KB
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
#!/usr/bin/env python
"""
Database initialization script
Creates the database and initial superuser
"""
import asyncio
import sys
from getpass import getpass
from app.db.database import db_manager
from app.schemas.user import UserCreate
from app.services.user import create_user
async def init_database():
print("🚀 Initializing database...")
# Initialize database connection
await db_manager.initialize()
# Create all tables
print("📋 Creating database tables...")
await db_manager.create_tables()
print("✅ Tables created successfully!")
# Check if demo mode
from app.core.config import settings
if settings.IS_DEMO_MODE:
print("\n🎮 Demo Mode Detected!")
print("Creating demo user account...")
async with db_manager.async_session_maker() as session:
# Check if demo user already exists
from app.services.user import get_user_by_email
existing_user = await get_user_by_email(session, "demo@example.com")
if not existing_user:
demo_user = UserCreate(
email="demo@example.com",
username="demo_user",
password="demo123",
full_name="Demo User",
is_active=True,
is_superuser=False,
is_verified=True
)
try:
user = await create_user(session, demo_user)
print("✅ Demo user created successfully!")
print(" Email: demo@example.com")
print(" Password: demo123")
except Exception as e:
print(f"❌ Error creating demo user: {e}")
else:
print("ℹ️ Demo user already exists.")
# Ask if user wants to create a superuser
create_super = input("\nDo you want to create a superuser account? (y/n): ").lower()
if create_super == 'y':
print("\n👤 Creating superuser account...")
email = input("Email: ")
username = input("Username: ")
password = getpass("Password: ")
confirm_password = getpass("Confirm password: ")
if password != confirm_password:
print("❌ Passwords do not match!")
return
# Create superuser
async with db_manager.async_session_maker() as session:
user_data = UserCreate(
email=email,
username=username,
password=password,
is_active=True,
is_superuser=True,
is_verified=True
)
try:
user = await create_user(session, user_data)
print(f"✅ Superuser '{username}' created successfully!")
except Exception as e:
print(f"❌ Error creating superuser: {e}")
# Close database connection
await db_manager.close()
print("\n🎉 Database initialization complete!")
if __name__ == "__main__":
try:
asyncio.run(init_database())
except KeyboardInterrupt:
print("\n⚠️ Database initialization cancelled.")
sys.exit(1)
except Exception as e:
print(f"\n❌ Error: {e}")
sys.exit(1)