diff --git a/deploy/init_pg.py b/deploy/init_pg.py index e1d9ff7..5818603 100644 --- a/deploy/init_pg.py +++ b/deploy/init_pg.py @@ -2,7 +2,10 @@ from sqlalchemy import text +from src.auth.models import Group, Role, User from src.db.session import async_session +from src.enums import ReservedRoleSlug + async def create_pg_extensions() -> None: async with async_session() as session: @@ -10,7 +13,29 @@ async def create_pg_extensions() -> None: await session.execute(text('create EXTENSION if not EXISTS "hstore"')) await session.commit() + +async def create_init_user() -> None: + async with async_session() as session: + new_role = Role(name="Administrator", slug=ReservedRoleSlug.ADMIN, description="App system admin") + session.add(new_role) + await session.commit() + new_group = Group( + name="System Administrator", + description="App systemic administrators group", + role_id=new_role.id, + user=[ + User( + name="Administrator", + email="admin@system.com", + password="admin", # noqa: S106 + role_id=new_role.id, + ) + ], + ) + session.add(new_group) + await session.commit() + + if __name__ == "__main__": asyncio.run(create_pg_extensions()) - - + asyncio.run(create_init_user()) diff --git a/src/auth/models.py b/src/auth/models.py index 088491b..1a70be7 100644 --- a/src/auth/models.py +++ b/src/auth/models.py @@ -67,7 +67,7 @@ class User(Base, AuditTimeMixin): group: Mapped["Group"] = relationship(back_populates="user", passive_deletes=True) role_id: Mapped[int] = mapped_column(ForeignKey(Role.id, ondelete="CASCADE")) role: Mapped["Role"] = relationship(backref="user", passive_deletes=True) - auth_info: Mapped[dict] = mapped_column(MutableDict.as_mutable(JSON)) + auth_info: Mapped[dict | None] = mapped_column(MutableDict.as_mutable(JSON)) Group.user_count = column_property(