Skip to content

Commit

Permalink
create document endpoint and fake trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
bailliekova committed Jul 21, 2024
1 parent ccfea69 commit 07d14ac
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
20 changes: 20 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sentry_sdk
from app.core.db import engine
from app.core.config import settings
from app.models import Document

if settings.ENVIRONMENT == "production":
sentry_sdk.init(
Expand Down Expand Up @@ -53,3 +54,22 @@ async def db_is_alive(session: Session = Depends(get_session)):
raise HTTPException(
status_code=status.HTTP_503_SERVICE_UNAVAILABLE, detail="DB is unreachable"
)


@app.post("/create_document")
async def create_document(session: Session = Depends(get_session)):
doc = Document()
session.add(doc)
session.commit()
session.refresh(doc)
document_id = doc.document_id
# poor man's trigger because I couldnt get SQLAchemy DDL to work with a dynamic table name
session.execute(
text(
f"""
CREATE TABLE assignments_{document_id} PARTITION OF assignments
VALUES IN ('{document_id}')
"""
)
)
return doc
33 changes: 17 additions & 16 deletions backend/app/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from datetime import datetime
from typing import Optional
from sqlmodel import Field, SQLModel, UUID, TIMESTAMP, text, Column
from sqlalchemy.schema import DDL
from sqlalchemy import event


class UUIDType(UUID):
Expand Down Expand Up @@ -41,7 +39,7 @@ class GerryDBTable(GerryDBTableBase, table=True):


class Document(TimeStampMixin, SQLModel):
document_id: str = Field(sa_column=Column(UUIDType), unique=True)
document_id: str | None = Field(sa_column=Column(UUIDType), unique=True)


class AssignmentsMixin(SQLModel):
Expand All @@ -57,16 +55,19 @@ class Assignments(AssignmentsMixin, table=True):
__table_args__ = {"postgres_partition_by": "document_id"}


# one time, create the trigger that creates new assignments partitions
create_partition_trigger = DDL(
"""
CREATE TRIGGER create_assignment_partition AFTER INSERT ON document
BEGIN
CREATE TABLE assignments_{document_id} PARTITION OF assignments
VALUES IN (new.document_id)
END
"""
)
# do it when the document table is created
event.listen(Document.__table, "after_create", create_partition_trigger)
# In a better world, we'd create the partition on assignments via trigger
# so that it happens even if a document is created outside the API.
#
# # one time, create the trigger that creates new assignments partitions
# create_partition_trigger = DDL(
# """
# CREATE TRIGGER create_assignment_partition AFTER INSERT ON document

# BEGIN
# CREATE TABLE assignments_{document_id} PARTITION OF assignments
# VALUES IN (new.document_id)
# END
# """
# )
# # do it when the document table is created
# event.listen(Document.__table, "after_create", create_partition_trigger)

0 comments on commit 07d14ac

Please sign in to comment.