File size: 1,516 Bytes
357bfdf | 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 | """Initialize authentication database tables and default roles."""
import sys
import asyncio
from pathlib import Path
# Add project root to path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
from sqlmodel import SQLModel, create_engine
from apps.auth.models import AuthUser, Role, Permission, UserRole
from apps.auth.models.role import RolePermission
from apps.auth.models.session import Session, PasswordResetToken
def init_auth_database():
"""Initialize authentication tables."""
# Create SQLite engine
DATABASE_URL = "sqlite:///./yaz_care.db"
engine = create_engine(DATABASE_URL, echo=False)
# Create all tables
SQLModel.metadata.create_all(engine)
print("✅ Authentication tables created successfully")
# Initialize default roles and permissions
from sqlmodel import Session as DBSession
from apps.auth.services.rbac_service import RBACService
with DBSession(engine) as session:
rbac_service = RBACService(session)
rbac_service.initialize_default_roles_and_permissions()
print("✅ Default roles and permissions initialized")
# List created roles
roles = rbac_service.list_roles()
print(f"\n📋 Created {len(roles)} roles:")
for role in roles:
perms = rbac_service.get_role_permissions(role.id)
print(f" - {role.name}: {len(perms)} permissions")
if __name__ == "__main__":
init_auth_database()
|