davidtran999 commited on
Commit
c0ae464
·
verified ·
1 Parent(s): 7cfffb5

Upload backend/scripts/verify_database_setup.py with huggingface_hub

Browse files
backend/scripts/verify_database_setup.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Script to verify database setup and migrations.
3
+ """
4
+ import os
5
+ import sys
6
+ from pathlib import Path
7
+
8
+ ROOT_DIR = Path(__file__).resolve().parents[2]
9
+ BACKEND_DIR = ROOT_DIR / "backend"
10
+
11
+ HUE_PORTAL_DIR = BACKEND_DIR / "hue_portal"
12
+
13
+ for path in (HUE_PORTAL_DIR, BACKEND_DIR, ROOT_DIR):
14
+ if str(path) not in sys.path:
15
+ sys.path.insert(0, str(path))
16
+
17
+ os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hue_portal.hue_portal.settings")
18
+
19
+ import django
20
+ django.setup()
21
+
22
+ from django.db import connection
23
+ from hue_portal.core.models import Procedure, Fine, Office, Advisory, AuditLog, MLMetrics, Synonym
24
+
25
+
26
+ def verify_extensions():
27
+ """Verify PostgreSQL extensions are enabled."""
28
+ print("\n" + "="*60)
29
+ print("Verifying PostgreSQL Extensions")
30
+ print("="*60)
31
+
32
+ with connection.cursor() as cursor:
33
+ cursor.execute("""
34
+ SELECT extname, extversion
35
+ FROM pg_extension
36
+ WHERE extname IN ('pg_trgm', 'unaccent')
37
+ ORDER BY extname;
38
+ """)
39
+ results = cursor.fetchall()
40
+
41
+ if results:
42
+ print("✅ Extensions enabled:")
43
+ for extname, extversion in results:
44
+ print(f" - {extname}: {extversion}")
45
+ else:
46
+ print("❌ No extensions found")
47
+ return len(results) == 2
48
+
49
+
50
+ def verify_tables():
51
+ """Verify all tables exist."""
52
+ print("\n" + "="*60)
53
+ print("Verifying Tables")
54
+ print("="*60)
55
+
56
+ tables = [
57
+ ("core_procedure", Procedure),
58
+ ("core_fine", Fine),
59
+ ("core_office", Office),
60
+ ("core_advisory", Advisory),
61
+ ("core_auditlog", AuditLog),
62
+ ("core_mlmetrics", MLMetrics),
63
+ ("core_synonym", Synonym),
64
+ ]
65
+
66
+ all_ok = True
67
+ for table_name, model_class in tables:
68
+ try:
69
+ count = model_class.objects.count()
70
+ print(f"✅ {table_name}: {count} records")
71
+ except Exception as e:
72
+ print(f"❌ {table_name}: Error - {e}")
73
+ all_ok = False
74
+
75
+ return all_ok
76
+
77
+
78
+ def verify_fields():
79
+ """Verify BM25 and embedding fields exist."""
80
+ print("\n" + "="*60)
81
+ print("Verifying Fields")
82
+ print("="*60)
83
+
84
+ models_to_check = [
85
+ ("Procedure", Procedure),
86
+ ("Fine", Fine),
87
+ ("Office", Office),
88
+ ("Advisory", Advisory),
89
+ ]
90
+
91
+ all_ok = True
92
+ for model_name, model_class in models_to_check:
93
+ has_tsv = hasattr(model_class, 'tsv_body')
94
+ has_embedding = hasattr(model_class, 'embedding')
95
+
96
+ if has_tsv and has_embedding:
97
+ print(f"✅ {model_name}: tsv_body ✓, embedding ✓")
98
+ else:
99
+ print(f"❌ {model_name}: tsv_body={has_tsv}, embedding={has_embedding}")
100
+ all_ok = False
101
+
102
+ # Check AuditLog fields
103
+ has_intent = hasattr(AuditLog, 'intent')
104
+ has_confidence = hasattr(AuditLog, 'confidence')
105
+ has_latency = hasattr(AuditLog, 'latency_ms')
106
+
107
+ if has_intent and has_confidence and has_latency:
108
+ print(f"✅ AuditLog: intent ✓, confidence ✓, latency_ms ✓")
109
+ else:
110
+ print(f"❌ AuditLog: intent={has_intent}, confidence={has_confidence}, latency_ms={has_latency}")
111
+ all_ok = False
112
+
113
+ # Check MLMetrics
114
+ if hasattr(MLMetrics, 'date'):
115
+ print(f"✅ MLMetrics: model exists")
116
+ else:
117
+ print(f"❌ MLMetrics: model not found")
118
+ all_ok = False
119
+
120
+ return all_ok
121
+
122
+
123
+ def verify_indexes():
124
+ """Verify GIN indexes for tsv_body."""
125
+ print("\n" + "="*60)
126
+ print("Verifying Indexes")
127
+ print("="*60)
128
+
129
+ with connection.cursor() as cursor:
130
+ cursor.execute("""
131
+ SELECT indexname, tablename
132
+ FROM pg_indexes
133
+ WHERE schemaname = 'public'
134
+ AND indexname LIKE '%_tsv_idx'
135
+ ORDER BY tablename;
136
+ """)
137
+ results = cursor.fetchall()
138
+
139
+ if results:
140
+ print("✅ GIN indexes found:")
141
+ for indexname, tablename in results:
142
+ print(f" - {indexname} on {tablename}")
143
+ else:
144
+ print("⚠️ No GIN indexes found (may need to run migrations)")
145
+
146
+ return len(results) >= 4
147
+
148
+
149
+ def test_bm25_search():
150
+ """Test BM25 search functionality."""
151
+ print("\n" + "="*60)
152
+ print("Testing BM25 Search")
153
+ print("="*60)
154
+
155
+ try:
156
+ from hue_portal.core.search_ml import search_with_ml
157
+
158
+ # Test with Fine model
159
+ from hue_portal.core.models import Fine
160
+
161
+ if Fine.objects.count() > 0:
162
+ results = search_with_ml(
163
+ Fine.objects.all(),
164
+ query="vượt đèn đỏ",
165
+ text_fields=["name", "code", "article"],
166
+ top_k=5,
167
+ use_hybrid=False # Test BM25 only
168
+ )
169
+ print(f"✅ BM25 search test: Found {len(results)} results")
170
+ if results:
171
+ print(f" First result: {results[0].name[:50]}...")
172
+ return True
173
+ else:
174
+ print("⚠️ No Fine records to test with")
175
+ return True # Not an error, just no data
176
+ except Exception as e:
177
+ print(f"❌ BM25 search test failed: {e}")
178
+ return False
179
+
180
+
181
+ def main():
182
+ print("="*60)
183
+ print("Database Setup Verification")
184
+ print("="*60)
185
+
186
+ results = {
187
+ "extensions": verify_extensions(),
188
+ "tables": verify_tables(),
189
+ "fields": verify_fields(),
190
+ "indexes": verify_indexes(),
191
+ "bm25_search": test_bm25_search(),
192
+ }
193
+
194
+ print("\n" + "="*60)
195
+ print("Summary")
196
+ print("="*60)
197
+
198
+ all_passed = all(results.values())
199
+
200
+ for check, passed in results.items():
201
+ status = "✅ PASS" if passed else "❌ FAIL"
202
+ print(f"{status}: {check}")
203
+
204
+ if all_passed:
205
+ print("\n🎉 All checks passed! Database is ready.")
206
+ else:
207
+ print("\n⚠️ Some checks failed. Please review above.")
208
+
209
+ return 0 if all_passed else 1
210
+
211
+
212
+ if __name__ == "__main__":
213
+ sys.exit(main())
214
+