Skip to content

Commit ef91f2d

Browse files
authored
closes #467 achieve 100% docstring coverage (#469)
* Add docstring to config.py Signed-off-by: Mihai Criveti <[email protected]> * Add docstring to main.py Signed-off-by: Mihai Criveti <[email protected]> * Update docstring in schemas.py Signed-off-by: Mihai Criveti <[email protected]> * Update docstring in translate.py Signed-off-by: Mihai Criveti <[email protected]> * Alembic docstring Signed-off-by: Mihai Criveti <[email protected]> * create_jwt_token docstring Signed-off-by: Mihai Criveti <[email protected]> * session_registry docstring Signed-off-by: Mihai Criveti <[email protected]> * autoflake8 isort Signed-off-by: Mihai Criveti <[email protected]> * autoflake8 isort pylint fixes Signed-off-by: Mihai Criveti <[email protected]> --------- Signed-off-by: Mihai Criveti <[email protected]>
1 parent 8dee24e commit ef91f2d

File tree

9 files changed

+782
-3
lines changed

9 files changed

+782
-3
lines changed

mcpgateway/alembic/env.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
# -*- coding: utf-8 -*-
2+
"""Alembic environment configuration for database migrations.
3+
4+
Copyright 2025
5+
SPDX-License-Identifier: Apache-2.0
6+
Authors: Mihai Criveti, Madhav Kandukuri
7+
8+
This module configures the Alembic migration environment for the MCP Gateway
9+
application. It sets up both offline and online migration modes, configures
10+
logging, and establishes the database connection parameters.
11+
12+
The module performs the following key functions:
13+
- Configures Alembic to locate migration scripts in the mcpgateway package
14+
- Sets up Python logging based on the alembic.ini configuration
15+
- Imports the SQLAlchemy metadata from the application models
16+
- Configures the database URL from application settings
17+
- Provides functions for running migrations in both offline and online modes
18+
19+
Offline mode generates SQL scripts without connecting to the database, while
20+
online mode executes migrations directly against a live database connection.
21+
22+
Attributes:
23+
config (Config): The Alembic configuration object loaded from alembic.ini.
24+
target_metadata (MetaData): SQLAlchemy metadata object containing all
25+
table definitions from the application models.
26+
27+
Examples:
28+
Running migrations in offline mode::
29+
30+
alembic upgrade head --sql
31+
32+
Running migrations in online mode::
33+
34+
alembic upgrade head
35+
36+
The module is typically not imported directly but is used by Alembic
37+
when executing migration commands.
38+
39+
Note:
40+
This file is automatically executed by Alembic and should not be
41+
imported or run directly by application code.
42+
"""
243
# Standard
344
from importlib.resources import files
445
from logging.config import fileConfig

mcpgateway/alembic/versions/b77ca9d2de7e_uuid_pk_and_slug_refactor.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,68 @@
3131
# Helpers
3232
# ──────────────────────────────────────────────────────────────────────────────
3333
def _use_batch() -> bool:
34+
"""Determine if batch operations are required for the current database.
35+
36+
SQLite requires batch mode for certain ALTER TABLE operations like dropping
37+
columns or altering column types. This helper checks the database dialect
38+
to determine if batch operations should be used.
39+
40+
Returns:
41+
bool: True if the database is SQLite (requires batch mode), False otherwise.
42+
43+
Examples:
44+
>>> # In a SQLite context
45+
>>> _use_batch() # doctest: +SKIP
46+
True
47+
>>> # In a PostgreSQL context
48+
>>> _use_batch() # doctest: +SKIP
49+
False
50+
"""
3451
return op.get_bind().dialect.name == "sqlite"
3552

3653

3754
# ──────────────────────────────────────────────────────────────────────────────
3855
# Upgrade
3956
# ──────────────────────────────────────────────────────────────────────────────
4057
def upgrade() -> None:
58+
"""Migrate database schema from integer to UUID primary keys with slugs.
59+
60+
This migration performs a comprehensive schema transformation in three stages:
61+
62+
Stage 1 - Add placeholder columns:
63+
- Adds UUID columns (id_new) to gateways, tools, and servers
64+
- Adds slug columns for human-readable identifiers
65+
- Adds columns to preserve original tool names before prefixing
66+
67+
Stage 2 - Data migration:
68+
- Generates UUIDs for all primary keys
69+
- Creates slugs from names (e.g., "My Gateway" -> "my-gateway")
70+
- Prefixes tool names with gateway slugs (e.g., "my-tool" -> "gateway-slug-my-tool")
71+
- Updates all foreign key references to use new UUIDs
72+
73+
Stage 3 - Schema finalization:
74+
- Drops old integer columns
75+
- Renames new UUID columns to replace old ones
76+
- Recreates primary keys and foreign key constraints
77+
- Adds unique constraints on slugs and URLs
78+
79+
The migration is designed to work with both SQLite (using batch operations)
80+
and other databases. It preserves all existing data relationships while
81+
transforming the schema.
82+
83+
Note:
84+
- Skips migration if database is fresh (no gateways table)
85+
- Uses batch operations for SQLite compatibility
86+
- Commits data changes before schema alterations
87+
88+
Examples:
89+
>>> # Running the migration
90+
>>> upgrade() # doctest: +SKIP
91+
Fresh database detected. Skipping migration.
92+
>>> # Or for existing database
93+
>>> upgrade() # doctest: +SKIP
94+
Existing installation detected. Starting data and schema migration...
95+
"""
4196
bind = op.get_bind()
4297
sess = Session(bind=bind)
4398
inspector = sa.inspect(bind)
@@ -402,6 +457,39 @@ def upgrade() -> None:
402457

403458

404459
def downgrade() -> None:
460+
"""Revert database schema from UUID primary keys back to integers.
461+
462+
This downgrade reverses the UUID migration but with significant limitations:
463+
- Schema structure is restored but data is NOT preserved
464+
- All UUID values and slug fields are lost
465+
- Foreign key relationships are broken (columns will be NULL)
466+
- Original integer IDs cannot be recovered
467+
468+
The downgrade operates in reverse order of the upgrade:
469+
470+
Stage 1 - Revert schema changes:
471+
- Drops UUID-based constraints and keys
472+
- Renames UUID columns back to temporary names
473+
- Re-adds integer columns (empty/NULL)
474+
475+
Stage 2 - Data migration (skipped):
476+
- Original integer IDs cannot be restored from UUIDs
477+
- Relationships cannot be reconstructed
478+
479+
Stage 3 - Remove temporary columns:
480+
- Drops all UUID and slug columns
481+
- Leaves database with original schema but no data
482+
483+
Warning:
484+
This downgrade is destructive and should only be used if you need
485+
to revert the schema structure. All data in affected tables will
486+
need to be manually restored from backups.
487+
488+
Examples:
489+
>>> # Running the downgrade
490+
>>> downgrade() # doctest: +SKIP
491+
# Schema reverted but data is lost
492+
"""
405493
# ── STAGE 1 (REVERSE): Revert Schema to original state ─────────────────
406494
# This reverses the operations from STAGE 3 of the upgrade.
407495
# Data from the new columns will be lost, which is expected.

0 commit comments

Comments
 (0)