Skip to content

Commit 2b989c5

Browse files
authored
Fix alembic_version creation (#477)
* Add script_location for alembic in Makefile Signed-off-by: Madhav Kandukuri <[email protected]> * Fix alembic_version creation Signed-off-by: Madhav Kandukuri <[email protected]> * Ruff fixes Signed-off-by: Madhav Kandukuri <[email protected]> * Fix doctest Signed-off-by: Madhav Kandukuri <[email protected]>
1 parent 5a4e3e2 commit 2b989c5

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,24 +2337,24 @@ alembic-install:
23372337

23382338
db-new:
23392339
@echo "➜ Generating revision: $(MSG)"
2340-
$(ALEMBIC) revision --autogenerate -m $(MSG)
2340+
$(ALEMBIC) -c mcpgateway/alembic.ini revision --autogenerate -m $(MSG)
23412341

23422342
db-up:
23432343
@echo "➜ Upgrading database to head ..."
2344-
$(ALEMBIC) upgrade head
2344+
$(ALEMBIC) -c mcpgateway/alembic.ini upgrade head
23452345

23462346
db-down:
23472347
@echo "➜ Downgrading database → $(REV) ..."
2348-
$(ALEMBIC) downgrade $(REV)
2348+
$(ALEMBIC) -c mcpgateway/alembic.ini downgrade $(REV)
23492349

23502350
db-current:
2351-
$(ALEMBIC) current
2351+
$(ALEMBIC) -c mcpgateway/alembic.ini current
23522352

23532353
db-history:
2354-
$(ALEMBIC) history --verbose
2354+
$(ALEMBIC) -c mcpgateway/alembic.ini history --verbose
23552355

23562356
db-revision-id:
2357-
@$(ALEMBIC) current --verbose | awk '/Current revision/ {print $$3}'
2357+
@$(ALEMBIC) -c mcpgateway/alembic.ini current --verbose | awk '/Current revision/ {print $$3}'
23582358

23592359

23602360
# =============================================================================

mcpgateway/alembic/env.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
This file is automatically executed by Alembic and should not be
4141
imported or run directly by application code.
4242
"""
43+
4344
# Standard
4445
from importlib.resources import files
4546
from logging.config import fileConfig
@@ -61,7 +62,12 @@
6162

6263

6364
# Create config object - this is the standard way in Alembic
64-
config = Config()
65+
config = getattr(context, "config", None) or Config()
66+
67+
def _inside_alembic() -> bool:
68+
"""Return True only when this file is running under Alembic CLI."""
69+
return getattr(context, "_proxy", None) is not None
70+
6571
config.set_main_option("script_location", str(files("mcpgateway").joinpath("alembic")))
6672

6773
# Interpret the config file for Python logging.
@@ -121,22 +127,27 @@ def run_migrations_online() -> None:
121127
and associate a connection with the context.
122128
123129
"""
124-
connectable = engine_from_config(
125-
config.get_section(config.config_ini_section, {}),
126-
prefix="sqlalchemy.",
127-
poolclass=pool.NullPool,
128-
)
129-
130-
with connectable.connect() as connection:
130+
connection = config.attributes.get("connection")
131+
if connection is None:
132+
connectable = engine_from_config(
133+
config.get_section(config.config_ini_section, {}),
134+
prefix="sqlalchemy.",
135+
poolclass=pool.NullPool,
136+
)
137+
138+
with connectable.connect() as connection:
139+
context.configure(connection=connection, target_metadata=target_metadata)
140+
141+
with context.begin_transaction():
142+
context.run_migrations()
143+
else:
131144
context.configure(connection=connection, target_metadata=target_metadata)
132145

133146
with context.begin_transaction():
134147
context.run_migrations()
135148

136-
137-
# Only run migrations if executed as a script (not on import)
138-
if __name__ == "__main__":
149+
if _inside_alembic():
139150
if context.is_offline_mode():
140151
run_migrations_offline()
141152
else:
142-
run_migrations_online()
153+
run_migrations_online()

mcpgateway/bootstrap_db.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,21 @@ async def main() -> None:
4949
engine = create_engine(settings.database_url)
5050
ini_path = files("mcpgateway").joinpath("alembic.ini")
5151
cfg = Config(str(ini_path)) # path in container
52-
cfg.attributes["configure_logger"] = False
52+
cfg.attributes["configure_logger"] = True
5353

54-
command.ensure_version(cfg)
54+
with engine.begin() as conn:
55+
cfg.attributes["connection"] = conn
56+
cfg.set_main_option("sqlalchemy.url", settings.database_url)
57+
58+
insp = inspect(conn)
59+
60+
if "gateways" not in insp.get_table_names():
61+
logger.info("Empty DB detected - creating baseline schema")
62+
Base.metadata.create_all(bind=conn)
63+
command.stamp(cfg, "head")
64+
else:
65+
command.upgrade(cfg, "head")
5566

56-
insp = inspect(engine)
57-
if "gateways" not in insp.get_table_names():
58-
logger.info("Empty DB detected - creating baseline schema")
59-
Base.metadata.create_all(engine)
60-
command.stamp(cfg, "head") # record baseline
61-
else:
62-
command.upgrade(cfg, "head") # apply any new revisions
6367
logger.info("Database ready")
6468

6569

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ import_heading_localfolder = "Local" # header for ad-hoc scripts / tests
272272
###############################################################################
273273
known_first_party = ["mcpgateway"] # treat "mcpgateway.*" as FIRSTPARTY
274274
known_local_folder = ["tests", "scripts"] # treat these folders as LOCALFOLDER
275+
known_third_party = ["alembic"] # treat "alembic" as THIRDPARTY
275276
# src_paths = ["src/mcpgateway"] # uncomment only if package moves under src/
276277

277278
###############################################################################

tests/playwright/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Playwright test configuration - Simple version without python-dotenv.
44
This assumes environment variables are loaded by the Makefile.
55
"""
6+
67
# Standard
78
import base64
89
import os

0 commit comments

Comments
 (0)