diff options
author | 2024-08-24 20:57:37 +0100 | |
---|---|---|
committer | 2024-08-24 20:57:37 +0100 | |
commit | ea51667b2811ae2ff48917a6ad52d34de0f6a259 (patch) | |
tree | 1cb84e345f85f0ad8cf09af35b7da92e8e64c6ed /thallium-backend/scripts | |
parent | Add postgres to testing CI (diff) |
Create users in the seed script
This also adds a on_conflict_do_nothing to ignore PK violations
Diffstat (limited to 'thallium-backend/scripts')
-rw-r--r-- | thallium-backend/scripts/seed.py | 38 |
1 files changed, 30 insertions, 8 deletions
diff --git a/thallium-backend/scripts/seed.py b/thallium-backend/scripts/seed.py index b27a586..a5ca015 100644 --- a/thallium-backend/scripts/seed.py +++ b/thallium-backend/scripts/seed.py @@ -1,19 +1,41 @@ import asyncio -from src.orm import Voucher +from sqlalchemy.dialects.postgresql import insert +from sqlalchemy.ext.asyncio import AsyncSession + +from src.dto import UserPermission +from src.orm import User, Voucher from src.settings import Connections +async def create_vouchers(session: AsyncSession) -> None: + """Create some test vouchers in the db.""" + entries = [ + {"voucher_code": "k1p", "balance": "13.37", "active": False}, + {"voucher_code": "k1p", "balance": "13.37", "active": False}, + {"voucher_code": "k1p", "balance": "13.37"}, + ] + stmt = insert(Voucher).values(entries).on_conflict_do_nothing() + await session.execute(stmt) + + +async def create_users(session: AsyncSession) -> None: + """Create some test vouchers in the db.""" + entries = [ + {"permissions": UserPermission.VIEW_PRODUCTS | UserPermission.VIEW_VOUCHERS}, + {"permissions": UserPermission.MANAGE_USERS}, + {"permissions": UserPermission.ISSUE_VOUCHERS | UserPermission.REVOKE_VOUCHERS}, + {"permissions": UserPermission.UPDATE_TEMPLATES}, + ] + stmt = insert(User).values(entries).on_conflict_do_nothing() + await session.execute(stmt) + + async def main() -> None: """Seed the database with some test data.""" async with Connections.DB_SESSION_MAKER() as session, session.begin(): - session.add_all( - [ - Voucher(voucher_code="k1p", balance="13.37", active=False), - Voucher(voucher_code="k1p", balance="13.37", active=False), - Voucher(voucher_code="k1p", balance="13.37"), - ] - ) + await create_vouchers(session) + await create_users(session) if __name__ == "__main__": |