Skip to content

Commit 0a22372

Browse files
authored
fixes for issue 365 (#617)
Signed-off-by: Shoumi <[email protected]>
1 parent f2dc235 commit 0a22372

File tree

2 files changed

+110
-31
lines changed

2 files changed

+110
-31
lines changed

Makefile

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,47 +2779,112 @@ shfmt-fix: shell-linters-install ## 🎨 Auto-format *.sh in place
27792779
# =============================================================================
27802780
# help: 🛢️ ALEMBIC DATABASE MIGRATIONS
27812781
# help: alembic-install - Install Alembic CLI (and SQLAlchemy) in the current env
2782-
# help: db-new - Create a new migration (override with MSG="your title")
2783-
# help: db-up - Upgrade DB to the latest revision (head)
2784-
# help: db-down - Downgrade one revision (override with REV=<id|steps>)
2785-
# help: db-current - Show the current head revision for the database
2786-
# help: db-history - Show the full migration graph / history
2787-
# help: db-revision-id - Echo just the current revision id (handy for scripting)
2782+
# help: db-init - Initialize alembic migrations
2783+
# help: db-migrate - Create a new migration
2784+
# help: db-upgrade - Upgrade database to latest migration
2785+
# help: db-downgrade - Downgrade database by one revision
2786+
# help: db-current - Show current database revision
2787+
# help: db-history - Show migration history
2788+
# help: db-heads - Show available heads
2789+
# help: db-show - Show a specific revision
2790+
# help: db-stamp - Stamp database with a specific revision
2791+
# help: db-reset - Reset database (CAUTION: drops all data)
2792+
# help: db-status - Show detailed database status
2793+
# help: db-check - Check if migrations are up to date
2794+
# help: db-fix-head - Fix multiple heads issue
27882795
# -----------------------------------------------------------------------------
27892796

2790-
# ──────────────────────────
2791-
# Internals & defaults
2792-
# ──────────────────────────
2793-
ALEMBIC ?= alembic # Override to e.g. `poetry run alembic`
2794-
MSG ?= "auto migration"
2795-
REV ?= -1 # Default: one step down; can be hash, -n, +n, etc.
2797+
# Database migration commands
2798+
ALEMBIC_CONFIG = mcpgateway/alembic.ini
27962799

2797-
.PHONY: alembic-install db-new db-up db-down db-current db-history db-revision-id
2800+
.PHONY: alembic-install db-init db-migrate db-upgrade db-downgrade db-current db-history db-heads db-show db-stamp db-reset db-status db-check db-fix-head
27982801

27992802
alembic-install:
28002803
@echo "➜ Installing Alembic ..."
28012804
pip install --quiet alembic sqlalchemy
28022805

2803-
db-new:
2804-
@echo "➜ Generating revision: $(MSG)"
2805-
$(ALEMBIC) -c mcpgateway/alembic.ini revision --autogenerate -m $(MSG)
2806-
2807-
db-up:
2808-
@echo "➜ Upgrading database to head ..."
2809-
$(ALEMBIC) -c mcpgateway/alembic.ini upgrade head
2810-
2811-
db-down:
2812-
@echo "➜ Downgrading database → $(REV) ..."
2813-
$(ALEMBIC) -c mcpgateway/alembic.ini downgrade $(REV)
2814-
2815-
db-current:
2816-
$(ALEMBIC) -c mcpgateway/alembic.ini current
2806+
.PHONY: db-init
2807+
db-init: ## Initialize alembic migrations
2808+
@echo "🗄️ Initializing database migrations..."
2809+
alembic -c $(ALEMBIC_CONFIG) init alembic
2810+
2811+
.PHONY: db-migrate
2812+
db-migrate: ## Create a new migration
2813+
@echo "�️ Creating new migration..."
2814+
@read -p "Enter migration message: " msg; \
2815+
alembic -c $(ALEMBIC_CONFIG) revision --autogenerate -m "$$msg"
2816+
2817+
.PHONY: db-upgrade
2818+
db-upgrade: ## Upgrade database to latest migration
2819+
@echo "🗄️ Upgrading database..."
2820+
alembic -c $(ALEMBIC_CONFIG) upgrade head
2821+
2822+
.PHONY: db-downgrade
2823+
db-downgrade: ## Downgrade database by one revision
2824+
@echo "�️ Downgrading database..."
2825+
alembic -c $(ALEMBIC_CONFIG) downgrade -1
2826+
2827+
.PHONY: db-current
2828+
db-current: ## Show current database revision
2829+
@echo "🗄️ Current database revision:"
2830+
@alembic -c $(ALEMBIC_CONFIG) current
2831+
2832+
.PHONY: db-history
2833+
db-history: ## Show migration history
2834+
@echo "🗄️ Migration history:"
2835+
@alembic -c $(ALEMBIC_CONFIG) history
2836+
2837+
.PHONY: db-heads
2838+
db-heads: ## Show available heads
2839+
@echo "�️ Available heads:"
2840+
@alembic -c $(ALEMBIC_CONFIG) heads
2841+
2842+
.PHONY: db-show
2843+
db-show: ## Show a specific revision
2844+
@read -p "Enter revision ID: " rev; \
2845+
alembic -c $(ALEMBIC_CONFIG) show $$rev
2846+
2847+
.PHONY: db-stamp
2848+
db-stamp: ## Stamp database with a specific revision
2849+
@read -p "Enter revision to stamp: " rev; \
2850+
alembic -c $(ALEMBIC_CONFIG) stamp $$rev
2851+
2852+
.PHONY: db-reset
2853+
db-reset: ## Reset database (CAUTION: drops all data)
2854+
@echo "⚠️ WARNING: This will drop all data!"
2855+
@read -p "Are you sure? (y/N): " confirm; \
2856+
if [ "$$confirm" = "y" ]; then \
2857+
alembic -c $(ALEMBIC_CONFIG) downgrade base && \
2858+
alembic -c $(ALEMBIC_CONFIG) upgrade head; \
2859+
echo "✅ Database reset complete"; \
2860+
else \
2861+
echo "❌ Database reset cancelled"; \
2862+
fi
28172863

2818-
db-history:
2819-
$(ALEMBIC) -c mcpgateway/alembic.ini history --verbose
2864+
.PHONY: db-status
2865+
db-status: ## Show detailed database status
2866+
@echo "�️ Database Status:"
2867+
@echo "Current revision:"
2868+
@alembic -c $(ALEMBIC_CONFIG) current
2869+
@echo ""
2870+
@echo "Pending migrations:"
2871+
@alembic -c $(ALEMBIC_CONFIG) history -r current:head
2872+
2873+
.PHONY: db-check
2874+
db-check: ## Check if migrations are up to date
2875+
@echo "🗄️ Checking migration status..."
2876+
@if alembic -c $(ALEMBIC_CONFIG) current | grep -q "(head)"; then \
2877+
echo "✅ Database is up to date"; \
2878+
else \
2879+
echo "⚠️ Database needs migration"; \
2880+
echo "Run 'make db-upgrade' to apply pending migrations"; \
2881+
exit 1; \
2882+
fi
28202883

2821-
db-revision-id:
2822-
@$(ALEMBIC) -c mcpgateway/alembic.ini current --verbose | awk '/Current revision/ {print $$3}'
2884+
.PHONY: db-fix-head
2885+
db-fix-head: ## Fix multiple heads issue
2886+
@echo "�️ Fixing multiple heads..."
2887+
alembic -c $(ALEMBIC_CONFIG) merge -m "merge heads"
28232888

28242889

28252890
# =============================================================================

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,20 @@ You can get started by copying the provided [.env.example](.env.example) to `.en
10941094

10951095
> 🧠 `none` disables caching entirely. Use `memory` for dev, `database` for persistence, or `redis` for distributed caching.
10961096

1097+
### Database Management
1098+
1099+
MCP Gateway uses Alembic for database migrations. Common commands:
1100+
1101+
- `make db-current` - Show current database version
1102+
- `make db-upgrade` - Apply pending migrations
1103+
- `make db-migrate` - Create new migration
1104+
- `make db-history` - Show migration history
1105+
- `make db-status` - Detailed migration status
1106+
1107+
#### Troubleshooting
1108+
1109+
If you see "No 'script_location' key found", ensure you're running from the project root directory.
1110+
10971111
### Development
10981112

10991113
| Setting | Description | Default | Options |

0 commit comments

Comments
 (0)