Skip to content

Commit c37de7c

Browse files
authored
Update doctest env.py (#484)
Signed-off-by: Mihai Criveti <[email protected]>
1 parent 23603a7 commit c37de7c

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

mcpgateway/alembic/env.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,43 @@
6464
# Create config object - this is the standard way in Alembic
6565
config = getattr(context, "config", None) or Config()
6666

67+
6768
def _inside_alembic() -> bool:
68-
"""Return True only when this file is running under Alembic CLI."""
69+
"""Detect if this module is being executed by the Alembic CLI.
70+
71+
This function checks whether the current execution context is within
72+
an Alembic migration environment. It's used to prevent migration code
73+
from running when this module is imported for other purposes (e.g.,
74+
during testing or when importing models).
75+
76+
The detection works by checking for the presence of the '_proxy' attribute
77+
on the alembic.context object. This attribute is set internally by Alembic
78+
when it loads and executes the env.py file during migration operations.
79+
80+
Returns:
81+
bool: True if running under Alembic CLI (e.g., during 'alembic upgrade',
82+
'alembic downgrade', etc.), False if imported normally by Python
83+
code or during testing.
84+
85+
Examples:
86+
When running migrations::
87+
88+
$ alembic upgrade head
89+
# _inside_alembic() returns True
90+
91+
When importing in tests or application code::
92+
93+
from mcpgateway.alembic.env import target_metadata
94+
# _inside_alembic() returns False
95+
96+
Note:
97+
This guard is crucial to prevent the migration execution code at the
98+
bottom of this module from running during normal imports. Without it,
99+
importing this module would attempt to run migrations every time.
100+
"""
69101
return getattr(context, "_proxy", None) is not None
70102

103+
71104
config.set_main_option("script_location", str(files("mcpgateway").joinpath("alembic")))
72105

73106
# Interpret the config file for Python logging.
@@ -146,8 +179,9 @@ def run_migrations_online() -> None:
146179
with context.begin_transaction():
147180
context.run_migrations()
148181

182+
149183
if _inside_alembic():
150184
if context.is_offline_mode():
151185
run_migrations_offline()
152186
else:
153-
run_migrations_online()
187+
run_migrations_online()

mcpgateway/federation/forward.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ async def forward_request(
136136
target_gateway_id: Optional specific gateway ID for targeted forwarding
137137
138138
Returns:
139-
For targeted requests: Single gateway response
140-
For broadcast requests: List of responses from all active gateways
139+
Any: Single gateway response for targeted requests (when target_gateway_id
140+
is provided), or list of responses from all active gateways for
141+
broadcast requests (when target_gateway_id is None).
141142
142143
Raises:
143144
ForwardingError: If forwarding fails due to network issues,

0 commit comments

Comments
 (0)