Skip to content

Commit

Permalink
fix(BA-584): Remove foreign key constraint from EndpointRow.image c…
Browse files Browse the repository at this point in the history
…olumn (#3599) (#3602)

Co-authored-by: Gyubong Lee <[email protected]>
  • Loading branch information
lablup-octodog and jopemachine authored Feb 7, 2025
1 parent 85df0f9 commit 1e4edbf
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions changes/3599.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove foreign key constraint from `EndpointRow.image` column.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Remove foreign key constraint from endpoints.image column
Revision ID: ecc9f6322be4
Revises: ef9a7960d234
Create Date: 2025-02-07 00:58:05.211395
"""

from alembic import op

from ai.backend.manager.models.base import GUID

# revision identifiers, used by Alembic.
revision = "ecc9f6322be4"
down_revision = "ef9a7960d234"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.drop_constraint("fk_endpoints_image_images", "endpoints", type_="foreignkey")
op.alter_column("endpoints", "image", existing_type=GUID, nullable=True)
op.create_check_constraint(
constraint_name="ck_image_required_unless_destroyed",
table_name="endpoints",
condition="lifecycle_stage = 'destroyed' OR image IS NOT NULL",
)


def downgrade() -> None:
op.create_foreign_key(
"fk_endpoints_image_images", "endpoints", "images", ["image"], ["id"], ondelete="RESTRICT"
)
op.alter_column("endpoints", "image", existing_type=GUID, nullable=False)
op.drop_constraint(
constraint_name="ck_image_required_unless_destroyed", table_name="endpoints", type_="check"
)
25 changes: 20 additions & 5 deletions src/ai/backend/manager/models/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import yarl
from graphene.types.datetime import DateTime as GQLDateTime
from graphql import Undefined
from sqlalchemy import CheckConstraint
from sqlalchemy.dialects import postgresql as pgsql
from sqlalchemy.ext.asyncio import AsyncConnection, AsyncSession
from sqlalchemy.orm import relationship, selectinload
from sqlalchemy.orm import foreign, relationship, selectinload
from sqlalchemy.orm.exc import NoResultFound

from ai.backend.common.config import model_definition_iv
Expand Down Expand Up @@ -102,6 +103,16 @@ class EndpointLifecycle(Enum):
class EndpointRow(Base):
__tablename__ = "endpoints"

__table_args__ = (
CheckConstraint(
sa.or_(
sa.column("lifecycle_stage") == EndpointLifecycle.DESTROYED.value,
sa.column("image").isnot(None),
),
name="ck_image_required_unless_destroyed",
),
)

id = EndpointIDColumn()
name = sa.Column("name", sa.String(length=512), nullable=False)
created_user = sa.Column(
Expand All @@ -114,9 +125,7 @@ class EndpointRow(Base):
desired_session_count = sa.Column(
"desired_session_count", sa.Integer, nullable=False, default=0, server_default="0"
)
image = sa.Column(
"image", GUID, sa.ForeignKey("images.id", ondelete="RESTRICT"), nullable=False
)
image = sa.Column("image", GUID)
model = sa.Column(
"model",
GUID,
Expand Down Expand Up @@ -206,7 +215,13 @@ class EndpointRow(Base):

routings = relationship("RoutingRow", back_populates="endpoint_row")
tokens = relationship("EndpointTokenRow", back_populates="endpoint_row")
image_row = relationship("ImageRow", back_populates="endpoints")
image_row = relationship(
"ImageRow",
primaryjoin=lambda: foreign(EndpointRow.image) == ImageRow.id,
foreign_keys=[image],
back_populates="endpoints",
)

model_row = relationship("VFolderRow", back_populates="endpoints")
created_user_row = relationship(
"UserRow", back_populates="created_endpoints", foreign_keys="EndpointRow.created_user"
Expand Down
15 changes: 13 additions & 2 deletions src/ai/backend/manager/models/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from redis.asyncio import Redis
from redis.asyncio.client import Pipeline
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import load_only, relationship, selectinload
from sqlalchemy.orm import foreign, load_only, relationship, selectinload

from ai.backend.common import redis_helper
from ai.backend.common.docker import ImageRef
Expand Down Expand Up @@ -179,6 +179,13 @@ class ImageType(enum.Enum):
SERVICE = "service"


# Defined for avoiding circular import
def _get_image_endpoint_join_condition():
from ai.backend.manager.models.endpoint import EndpointRow

return ImageRow.id == foreign(EndpointRow.image)


class ImageRow(Base):
__tablename__ = "images"
id = IDColumn("id")
Expand Down Expand Up @@ -221,7 +228,11 @@ class ImageRow(Base):
)
aliases: relationship
# sessions = relationship("SessionRow", back_populates="image_row")
endpoints = relationship("EndpointRow", back_populates="image_row")
endpoints = relationship(
"EndpointRow",
primaryjoin=_get_image_endpoint_join_condition,
back_populates="image_row",
)

def __init__(
self,
Expand Down

0 comments on commit 1e4edbf

Please sign in to comment.