Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use upsert to create/update #89

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions hq_superset/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from sqlalchemy.dialects.postgresql import insert
from typing import Any

from authlib.integrations.sqla_oauth2 import (
Expand Down Expand Up @@ -46,12 +47,18 @@ def update_dataset(self):
engine.connect() as connection,
connection.begin() # Commit on leaving context
):
delete_stmt = table.delete().where(table.c.doc_id == self.doc_id)
connection.execute(delete_stmt)
if self.data:
rows = list(cast_data_for_table(self.data, table))
insert_stmt = table.insert().values(rows)
connection.execute(insert_stmt)
upsert = insert(table).values(rows)
upsert.on_conflict_do_update(
constraint=table.primary_key,
set_={
col.name: col for col in upsert.excluded if not col.primary_key
}
)
else:
delete_stmt = table.delete().where(table.c.doc_id == self.doc_id)
connection.execute(delete_stmt)


class OAuth2Client(db.Model, OAuth2ClientMixin):
Expand Down