Repository navigation
Expand file tree
/
Copy pathinit_db.py
More file actions
138 lines (110 loc) · 4.48 KB
/
Copy pathinit_db.py
File metadata and controls
138 lines (110 loc) · 4.48 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
"""
Database initialization and default data setup
"""
from sqlalchemy.orm import Session
from app.core.database import get_db_session, init_database
from app.core.crud import UserCRUD, GalaxyCRUD, TemplateCRUD
from app.core.config import DEFAULT_GALAXIES
from app.models.database import User, Galaxy, Template
import logging
logger = logging.getLogger(__name__)
def create_default_galaxies(user_id: int) -> bool:
"""Create default galaxies for a new user"""
try:
with get_db_session() as db:
for galaxy_config in DEFAULT_GALAXIES:
# Create galaxy
galaxy = GalaxyCRUD.create(
db=db,
user_id=user_id,
name=galaxy_config["name"],
description=galaxy_config["description"],
icon=galaxy_config["icon"],
color=galaxy_config["color"],
is_default=True,
is_custom=False,
settings={
"preferred_models": galaxy_config["recommended_models"],
"auto_title": True,
"save_history": True,
"context_length": 10
}
)
# Create templates for this galaxy
for template_config in galaxy_config.get("templates", []):
TemplateCRUD.create(
db=db,
galaxy_id=galaxy.id,
name=template_config["name"],
prompt_template=template_config["prompt"],
tags=template_config["tags"],
description=f"Default template for {galaxy.name}",
is_public=False,
category="default"
)
logger.info(f"Created default galaxies for user {user_id}")
return True
except Exception as e:
logger.error(f"Error creating default galaxies: {e}")
return False
def setup_database():
"""Initialize database and create tables"""
logger.info("Initializing database...")
success = init_database()
if success:
logger.info("Database initialized successfully")
else:
logger.error("Database initialization failed")
return False
return True
def create_admin_user(email: str, password: str, full_name: str = "Admin User") -> bool:
"""Create an admin user for initial setup"""
try:
with get_db_session() as db:
# Check if user already exists
existing_user = UserCRUD.get_by_email(db, email)
if existing_user:
logger.warning(f"Admin user {email} already exists")
return True
# Create admin user
admin_user = UserCRUD.create(
db=db,
email=email,
password=password,
full_name=full_name
)
# Create default galaxies for admin
create_default_galaxies(admin_user.id)
logger.info(f"Admin user created: {email}")
return True
except Exception as e:
logger.error(f"Error creating admin user: {e}")
return False
def reset_database():
"""Reset database (DROP ALL TABLES) - USE WITH CAUTION!"""
from app.core.database import drop_tables
logger.warning("RESETTING DATABASE - ALL DATA WILL BE LOST!")
drop_tables()
setup_database()
logger.info("Database reset complete")
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "init":
setup_database()
elif command == "reset":
reset_database()
elif command == "admin" and len(sys.argv) >= 4:
email = sys.argv[2]
password = sys.argv[3]
full_name = sys.argv[4] if len(sys.argv) > 4 else "Admin User"
setup_database()
create_admin_user(email, password, full_name)
else:
print("Usage:")
print(" python init_db.py init # Initialize database")
print(" python init_db.py reset # Reset database (DANGER!)")
print(" python init_db.py admin <email> <password> [name] # Create admin user")
else:
setup_database()