aboutsummaryrefslogtreecommitdiffstats
path: root/thallium-backend/scripts
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2024-08-24 20:57:37 +0100
committerGravatar Chris Lovering <[email protected]>2024-08-24 20:57:37 +0100
commitea51667b2811ae2ff48917a6ad52d34de0f6a259 (patch)
tree1cb84e345f85f0ad8cf09af35b7da92e8e64c6ed /thallium-backend/scripts
parentAdd 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.py38
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__":