Coverage for mcpgateway/alembic/versions/e4fc04d1a442_add_annotations_to_tables.py: 36%

1# -*- coding: utf-8 -*-

2"""Add annotations to tables

3

4 Revision ID: e4fc04d1a442

5 Revises: b77ca9d2de7e

6 Create Date: 2025-06-27 21:45:35.099713

7

8"""

9

10# Standard

11 from typing import Sequence, Union

12

13# Third-Party

14 import sqlalchemy as sa

15

16# First-Party

17 from alembic import op

18

19# revision identifiers, used by Alembic.

20 revision: str = "e4fc04d1a442"

21 down_revision: Union[str, Sequence[str], None] = "b77ca9d2de7e"

22 branch_labels: Union[str, Sequence[str], None] = None

23 depends_on: Union[str, Sequence[str], None] = None

24

25

26 def upgrade() -> None:

27 """

28 Applies the migration to add the 'annotations' column.

29

30 This function adds a new column named 'annotations' of type JSON to the 'tool'

31 table. It includes a server-side default of an empty JSON object ('{}') to ensure

32 that existing rows get a non-null default value.

33 """

34 bind = op.get_bind()

35 inspector = sa.inspect(bind)

36

37 if not inspector.has_table("gateways"):

38 print("Fresh database detected. Skipping migration.")

39 return

40

41 op.add_column("tools", sa.Column("annotations", sa.JSON(), server_default=sa.text("'{}'"), nullable=False))

42

43

44 def downgrade() -> None:

45 """

46 Reverts the migration by removing the 'annotations' column.

47

48 This function provides a way to undo the migration, safely removing the

49 'annotations' column from the 'tool' table.

50 """

51 bind = op.get_bind()

52 inspector = sa.inspect(bind)

53

54 if not inspector.has_table("gateways"):

55 print("Fresh database detected. Skipping migration.")

56 return

57

58 op.drop_column("tools", "annotations")