Skip to content

Commit

Permalink
refactor: rename fk, pass name in migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiryous committed Oct 21, 2024
1 parent 31b0a78 commit 6a69389
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
9 changes: 8 additions & 1 deletion keep/api/models/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ class IncidentDto(IncidentDtoIn):

same_incident_in_the_past_id: UUID | None

merged_into_incident_id: UUID | None
merged_by: str | None
merged_at: datetime.datetime | None

_tenant_id: str = PrivateAttr()

def __str__(self) -> str:
Expand Down Expand Up @@ -457,7 +461,7 @@ def set_default_values(cls, values: Dict[str, Any]) -> Dict[str, Any]:
return values

@classmethod
def from_db_incident(cls, db_incident):
def from_db_incident(cls, db_incident: "Incident"):

severity = (
IncidentSeverity.from_number(db_incident.severity)
Expand Down Expand Up @@ -485,6 +489,9 @@ def from_db_incident(cls, db_incident):
services=db_incident.affected_services or [],
rule_fingerprint=db_incident.rule_fingerprint,
same_incident_in_the_past_id=db_incident.same_incident_in_the_past_id,
merged_into_incident_id=db_incident.merged_into_incident_id,
merged_by=db_incident.merged_by,
merged_at=db_incident.merged_at,
)

# This field is required for getting alerts when required
Expand Down
6 changes: 3 additions & 3 deletions keep/api/models/db/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class Incident(SQLModel, table=True):
),
)

merged_into_id: UUID | None = Field(
merged_into_incident_id: UUID | None = Field(
sa_column=Column(
UUIDType(binary=False),
ForeignKey("incident.id", ondelete="SET NULL"),
Expand All @@ -170,13 +170,13 @@ class Incident(SQLModel, table=True):
back_populates="merged_incidents",
sa_relationship_kwargs=dict(
remote_side="Incident.id",
foreign_keys="[Incident.merged_into_id]",
foreign_keys="[Incident.merged_into_incident_id]",
),
)
merged_incidents: List["Incident"] = Relationship(
back_populates="merged_into",
sa_relationship_kwargs=dict(
foreign_keys="[Incident.merged_into_id]",
foreign_keys="[Incident.merged_into_incident_id]",
),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def upgrade() -> None:
with op.batch_alter_table("incident", schema=None) as batch_op:
batch_op.add_column(
sa.Column(
"merged_into_id",
"merged_into_incident_id",
sqlalchemy_utils.types.uuid.UUIDType(binary=False),
nullable=True,
)
Expand All @@ -32,13 +32,19 @@ def upgrade() -> None:
sa.Column("merged_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True)
)
batch_op.create_foreign_key(
None, "incident", ["merged_into_id"], ["id"], ondelete="SET NULL"
"fk_incident_merged_into_incident_id",
"incident",
["merged_into_incident_id"],
["id"],
ondelete="SET NULL",
)


def downgrade() -> None:
with op.batch_alter_table("incident", schema=None) as batch_op:
batch_op.drop_constraint(None, type_="foreignkey")
batch_op.drop_constraint(
"fk_incident_merged_into_incident_id", type_="foreignkey"
)
batch_op.drop_column("merged_by")
batch_op.drop_column("merged_at")
batch_op.drop_column("merged_into_id")
batch_op.drop_column("merged_into_incident_id")

0 comments on commit 6a69389

Please sign in to comment.