DRRK/app/repositories/_upsert.py

21 lines
753 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from typing import Any
from sqlalchemy import Table
from sqlalchemy.dialects import postgresql, sqlite
from sqlalchemy.ext.asyncio import AsyncSession
def insert_for(session: AsyncSession, table: Table) -> Any:
"""INSERT нужного диалекта — у обоих есть on_conflict_do_update/excluded.
Postgres — прод, SQLite — локальная разработка; других БД не ждём.
"""
dialect = session.get_bind().dialect.name
if dialect == "postgresql":
return postgresql.insert(table)
if dialect == "sqlite":
return sqlite.insert(table)
raise RuntimeError(f"Upsert не поддержан для диалекта {dialect!r}.")