Skip to content

Commit

Permalink
Clean up FKs and indexes for lastalert
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirFilonov committed Nov 24, 2024
1 parent 7c50d25 commit e05f0b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 11 additions & 3 deletions keep/api/models/db/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from uuid import UUID, uuid4

from pydantic import PrivateAttr
from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy import ForeignKey, UniqueConstraint, ForeignKeyConstraint
from sqlalchemy.dialects.mssql import DATETIME2 as MSSQL_DATETIME2
from sqlalchemy.dialects.mysql import DATETIME as MySQL_DATETIME
from sqlalchemy.engine.url import make_url
Expand Down Expand Up @@ -76,7 +76,7 @@ class AlertToIncident(SQLModel, table=True):
class LastAlert(SQLModel, table=True):

tenant_id: str = Field(foreign_key="tenant.id", nullable=False, primary_key=True)
fingerprint: str = Field(primary_key=True)
fingerprint: str = Field(primary_key=True, index=True)
alert_id: UUID = Field(foreign_key="alert.id")
timestamp: datetime = Field(nullable=False, index=True)

Expand All @@ -85,7 +85,7 @@ class LastAlertToIncident(SQLModel, table=True):
tenant_id: str = Field(foreign_key="tenant.id", nullable=False, primary_key=True)
timestamp: datetime = Field(default_factory=datetime.utcnow)

fingerprint: str = Field(foreign_key="lastalert.fingerprint", primary_key=True)
fingerprint: str = Field(primary_key=True)
incident_id: UUID = Field(
sa_column=Column(
UUIDType(binary=False),
Expand All @@ -103,6 +103,14 @@ class LastAlertToIncident(SQLModel, table=True):
default=NULL_FOR_DELETED_AT,
)

__table_args__ = (
ForeignKeyConstraint(
["tenant_id", "fingerprint"],
["lastalert.tenant_id", "lastalert.fingerprint"]),
{}
)


# alert: "Alert" = Relationship(
# back_populates="alert_to_incident_link",
# sa_relationship = relationship(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def upgrade() -> None:
["alert_id"],
["alert.id"],
),
sa.ForeignKeyConstraint(
["tenant_id"],
["tenant.id"],
),
sa.PrimaryKeyConstraint("tenant_id", "fingerprint"),
)
with op.batch_alter_table("lastalert", schema=None) as batch_op:
Expand Down

0 comments on commit e05f0b9

Please sign in to comment.