Skip to content

Commit

Permalink
some new models
Browse files Browse the repository at this point in the history
  • Loading branch information
bailliekova committed Jul 21, 2024
1 parent 2a644c6 commit ccfea69
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions backend/app/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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 @@ -36,3 +38,35 @@ class GerryDBTableBase(TimeStampMixin, SQLModel):
class GerryDBTable(GerryDBTableBase, table=True):
uuid: str = Field(sa_column=Column(UUIDType, unique=True))
name: str = Field(nullable=False, unique=True)


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


class AssignmentsMixin(SQLModel):
# mixin used for defining parent table + each partition on the fly
document_id: str = Field(foreign_key="document.document_id")
geo_id: str
zone: int


class Assignments(AssignmentsMixin, table=True):
# this is the empty parent table; not a partition itself

__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)

0 comments on commit ccfea69

Please sign in to comment.