-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
executable file
·93 lines (80 loc) · 2.98 KB
/
migrate.py
File metadata and controls
executable file
·93 lines (80 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python3
"""
Migration script for KnowledgeOps AI database
"""
import os
import sys
import subprocess
from pathlib import Path
def run_command(cmd, description):
"""Run a command and handle errors"""
print(f"🔄 {description}...")
try:
result = subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)
print(f"✅ {description} completed successfully")
if result.stdout:
print(result.stdout)
return True
except subprocess.CalledProcessError as e:
print(f"❌ {description} failed:")
print(f" Error: {e}")
if e.stdout:
print(f" stdout: {e.stdout}")
if e.stderr:
print(f" stderr: {e.stderr}")
return False
def check_database_connection():
"""Check if database is accessible"""
print("🔍 Checking database connection...")
# Try to run a simple alembic command
result = subprocess.run(
"alembic current",
shell=True,
capture_output=True,
text=True
)
if result.returncode == 0:
print("✅ Database connection successful")
return True
else:
print("❌ Database connection failed")
print(" Make sure PostgreSQL is running and accessible")
print(" Check your DATABASE_URL in .env file")
return False
def main():
"""Run database migrations"""
print("🚀 KnowledgeOps AI Database Migration")
print("=" * 50)
# Check if we're in the right directory
if not Path("alembic.ini").exists():
print("❌ alembic.ini not found. Make sure you're in the project root directory.")
sys.exit(1)
# Check database connection
if not check_database_connection():
print("\n💡 Troubleshooting tips:")
print(" 1. Make sure PostgreSQL is running")
print(" 2. Check your .env file has correct DATABASE_URL")
print(" 3. Ensure the database exists")
print(" 4. Verify pgvector extension is available")
sys.exit(1)
# Run migrations
print("\n📊 Running database migrations...")
# Show current status
run_command("alembic current", "Checking current migration status")
# Run upgrade
if run_command("alembic upgrade head", "Running migrations"):
print("\n🎉 Database migration completed successfully!")
print("\n📋 Migration Summary:")
print(" ✅ pgvector extension enabled")
print(" ✅ documents table created")
print(" ✅ chunks table created with vector support")
print(" ✅ conversations table created")
print(" ✅ Performance indexes created")
# Show final status
run_command("alembic current", "Final migration status")
print("\n🚀 Your KnowledgeOps AI database is ready!")
else:
print("\n❌ Migration failed. Check the error messages above.")
sys.exit(1)
if __name__ == "__main__":
main()