-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed_database.py
51 lines (40 loc) · 1.61 KB
/
seed_database.py
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
import asyncio
from api.core.config import app_config
from api.database.models import User
from api.database.repository import Repository
from api.database.session import DatabaseSessionManager
from api.schemas.user import Roles, UserCreate
from api.services.auth_service import AuthService
from api.utils.exceptions import IntegrityViolationError
async def seed_database():
db_session_manager = DatabaseSessionManager(
app_config.database_url,
)
async with db_session_manager.session() as session:
user_repository = Repository(session, User)
auth_service = AuthService(user_repository)
try:
print("Seeding database...\n")
user = UserCreate(
user_name=app_config.admin_username,
first_name=app_config.admin_first_name,
last_name=app_config.admin_last_name,
role=Roles.MANAGER,
email=app_config.admin_email,
password=app_config.admin_password,
)
db_user = User(
user_name=user.user_name,
hashed_password=auth_service.hash_pwd(user.password),
first_name=user.first_name,
last_name=user.last_name,
email=user.email.lower(),
role=user.role,
admin=user.role in Roles.MANAGER,
)
await user_repository.create(db_user)
print("\nDatabase seeded successfully")
except IntegrityViolationError:
print("\nDatabase already seeded")
if __name__ == "__main__":
asyncio.run(seed_database())