Coverage for mcpgateway/alembic/versions/e4fc04d1a442_add_annotations_to_tables.py: 36%
21 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 11:03 +0100
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-09 11:03 +0100
1# -*- coding: utf-8 -*-
2"""Add annotations to tables
4Revision ID: e4fc04d1a442
5Revises: b77ca9d2de7e
6Create Date: 2025-06-27 21:45:35.099713
8"""
10# Standard
11from typing import Sequence, Union
13# Third-Party
14import sqlalchemy as sa
16# First-Party
17from alembic import op
19# revision identifiers, used by Alembic.
20revision: str = "e4fc04d1a442"
21down_revision: Union[str, Sequence[str], None] = "b77ca9d2de7e"
22branch_labels: Union[str, Sequence[str], None] = None
23depends_on: Union[str, Sequence[str], None] = None
26def upgrade() -> None:
27 """
28 Applies the migration to add the 'annotations' column.
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)
37 if not inspector.has_table("gateways"):
38 print("Fresh database detected. Skipping migration.")
39 return
41 op.add_column("tools", sa.Column("annotations", sa.JSON(), server_default=sa.text("'{}'"), nullable=False))
44def downgrade() -> None:
45 """
46 Reverts the migration by removing the 'annotations' column.
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)
54 if not inspector.has_table("gateways"):
55 print("Fresh database detected. Skipping migration.")
56 return
58 op.drop_column("tools", "annotations")