@@ -2779,47 +2779,112 @@ shfmt-fix: shell-linters-install ## 🎨 Auto-format *.sh in place
2779
2779
# =============================================================================
2780
2780
# help: 🛢️ ALEMBIC DATABASE MIGRATIONS
2781
2781
# 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
2788
2795
# -----------------------------------------------------------------------------
2789
2796
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
2796
2799
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
2798
2801
2799
2802
alembic-install :
2800
2803
@echo " ➜ Installing Alembic ..."
2801
2804
pip install --quiet alembic sqlalchemy
2802
2805
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
2817
2863
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
2820
2883
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"
2823
2888
2824
2889
2825
2890
# =============================================================================
0 commit comments