-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
71 lines (57 loc) · 2.38 KB
/
init_db.py
File metadata and controls
71 lines (57 loc) · 2.38 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
from backend.app.models import Base, User
from backend.app.models.database import engine, SessionLocal
from backend.app.config import settings
def init_database():
# Initializing database with tables and users from settings
print("Initializing database...")
# Create all tables
Base.metadata.create_all(bind=engine)
print("Database tables created!")
# Load users from settings
default_users = settings.parsed_default_users
if not default_users:
print("No users found in configuration (DEFAULT_USERS)")
return
db = SessionLocal()
try:
users_created = 0
users_skipped = 0
for user_data in default_users:
username = user_data.get('username') or user_data.get('name')
if not username:
continue
existing_user = db.query(User).filter(
User.username == username
).first()
if existing_user:
print(f"User '{username}' already exists - skipping")
users_skipped += 1
continue
new_user = User(
username=username,
email=user_data.get('email', f"{username}@example.com"),
full_name=user_data.get('full_name', username),
role=user_data.get('role', 'hr')
)
# Assuming User model has set_password or hashed_password
if hasattr(new_user, 'set_password'):
new_user.set_password(user_data.get('password', 'changeme'))
elif hasattr(new_user, 'hashed_password'):
from backend.app.services.auth_service import get_password_hash
new_user.hashed_password = get_password_hash(user_data.get('password', 'changeme'))
db.add(new_user)
users_created += 1
print(f"Created user: {username} ({user_data.get('role', 'hr')})")
db.commit()
print(f"\nSummary:")
print(f" • Users created: {users_created}")
print(f" • Users skipped: {users_skipped}")
print(f" • Total users processed: {len(default_users)}")
except Exception as e:
db.rollback()
print(f"Error: {e}")
raise
finally:
db.close()
if __name__ == "__main__":
init_database()