Spaces:
Sleeping
Sleeping
File size: 10,337 Bytes
1d10b0a |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
"""Direct SQLite rebuild - populate collections table from existing folders."""
import os
import sys
import sqlite3
import uuid as uuid_lib
import json
import pickle
from datetime import datetime
def extract_collection_name(collection_path, collection_id):
"""Extract collection name from metadata files.
Tries multiple approaches:
1. Read from metadata.json
2. Parse ChromaDB index_metadata.pickle
3. Query from segments table (will be done later)
4. Generate name from UUID
"""
# Try metadata.json
metadata_file = os.path.join(collection_path, "metadata.json")
if os.path.exists(metadata_file):
try:
with open(metadata_file, 'r') as f:
metadata = json.load(f)
if isinstance(metadata, dict) and 'name' in metadata:
name = metadata['name']
print(f" π Found in metadata.json: {name}")
return name
except Exception as e:
print(f" β οΈ Error reading metadata.json: {e}")
# Try to parse index_metadata.pickle
index_file = os.path.join(collection_path, "index_metadata.pickle")
if os.path.exists(index_file):
try:
with open(index_file, 'rb') as f:
index_data = pickle.load(f)
# Check if it contains collection name
if isinstance(index_data, dict):
if 'name' in index_data:
name = index_data['name']
print(f" π Found in pickle: {name}")
return name
# Log available keys for debugging
keys = list(index_data.keys())[:5]
print(f" π Pickle keys: {keys}")
except Exception as e:
print(f" β οΈ Error reading pickle: {e}")
# Generate name from UUID
generated_name = f"collection_{collection_id[:8]}"
print(f" π Using generated name: {generated_name}")
return generated_name
def rebuild_sqlite_directly():
"""Directly rebuild SQLite collections table from existing collection folders.
This script:
1. Reads existing collection folders
2. Inserts entries into SQLite collections table
3. Verifies ChromaDB can find them
"""
print("\n" + "=" * 80)
print("π§ Direct SQLite Collections Table Rebuild")
print("=" * 80)
chroma_path = "./chroma_db"
sqlite_path = os.path.join(chroma_path, "chroma.sqlite3")
# Step 1: Find all collection folders
print("\nπ Step 1: Scanning collection folders...")
print("-" * 80)
collections = []
try:
for item in os.listdir(chroma_path):
item_path = os.path.join(chroma_path, item)
# UUID folder check
if os.path.isdir(item_path) and len(item) == 36 and item.count('-') == 4:
print(f"\nβ
Found collection: {item}")
# Extract the actual collection name
collection_name = extract_collection_name(item_path, item)
collections.append({
'uuid': item,
'name': collection_name,
'path': item_path,
'files': os.listdir(item_path)
})
except Exception as e:
print(f"β Error scanning: {e}")
return False
if len(collections) == 0:
print("β οΈ No collections found")
return True
print(f"\nβ
Total collections: {len(collections)}")
# Step 2: Connect to SQLite
print("\nπΎ Step 2: Connecting to SQLite...")
print("-" * 80)
if not os.path.exists(sqlite_path):
print("β ERROR: chroma.sqlite3 not found!")
print(" Run 'streamlit run streamlit_app.py' first to create it")
return False
try:
conn = sqlite3.connect(sqlite_path)
cursor = conn.cursor()
print("β
Connected to SQLite")
except Exception as e:
print(f"β Error connecting to SQLite: {e}")
return False
# Step 3: Check collections table schema
print("\nπ Step 3: Checking collections table schema...")
print("-" * 80)
try:
cursor.execute("PRAGMA table_info(collections)")
columns = cursor.fetchall()
print("β
Collections table schema:")
for col in columns:
col_id, col_name, col_type, not_null, default, pk = col
print(f" β’ {col_name} ({col_type})")
except Exception as e:
print(f"β Error reading schema: {e}")
conn.close()
return False
# Step 4: Get database and tenant IDs
print("\nπ Step 4: Getting database and tenant IDs...")
print("-" * 80)
try:
# Get default tenant
cursor.execute("SELECT id FROM tenants LIMIT 1")
tenant_result = cursor.fetchone()
if tenant_result:
tenant_id = tenant_result[0]
print(f"β
Tenant ID: {tenant_id}")
else:
# Create default tenant
tenant_id = str(uuid_lib.uuid4())
cursor.execute("INSERT INTO tenants (id, name) VALUES (?, ?)",
(tenant_id, 'default'))
conn.commit()
print(f"β
Created Tenant ID: {tenant_id}")
# Get default database
cursor.execute("SELECT id FROM databases WHERE tenant_id = ? LIMIT 1", (tenant_id,))
db_result = cursor.fetchone()
if db_result:
db_id = db_result[0]
print(f"β
Database ID: {db_id}")
else:
# Create default database
db_id = str(uuid_lib.uuid4())
cursor.execute(
"INSERT INTO databases (id, name, tenant_id) VALUES (?, ?, ?)",
(db_id, 'default', tenant_id)
)
conn.commit()
print(f"β
Created Database ID: {db_id}")
except Exception as e:
print(f"β Error getting IDs: {e}")
conn.close()
return False
# Step 4b: Try to extract collection names from segments table
print("\nπ Step 4b: Checking for collection names in segments table...")
print("-" * 80)
segment_names = {}
try:
cursor.execute("""
SELECT DISTINCT collection_id, metadata
FROM segment_metadata
WHERE collection_id IN ({})
""".format(','.join(['?' for _ in collections])),
[c['uuid'] for c in collections])
for collection_id, metadata_str in cursor.fetchall():
try:
if metadata_str:
metadata = json.loads(metadata_str)
if isinstance(metadata, dict) and 'name' in metadata:
segment_names[collection_id] = metadata['name']
print(f"β
Found name in segments: {metadata['name']}")
except:
pass
except Exception as e:
print(f"βΉοΈ Could not query segments: {e}")
# Update collection names from segments if found
for collection in collections:
if collection['uuid'] in segment_names:
collection['name'] = segment_names[collection['uuid']]
print(f" Updated {collection['uuid'][:8]} -> {collection['name']}")
# Step 5: Insert collection records
print("\nπ Step 5: Inserting collection records into SQLite...")
print("-" * 80)
inserted_count = 0
for collection in collections:
collection_id = collection['uuid']
collection_name = collection['name'] # Use extracted/generated name
try:
cursor.execute("""
INSERT OR REPLACE INTO collections
(id, name, database_id)
VALUES (?, ?, ?)
""", (collection_id, collection_name, db_id))
inserted_count += 1
print(f"β
Inserted: {collection_name}")
print(f" ID: {collection_id}")
except Exception as e:
print(f"β οΈ Could not insert {collection_id}: {e}")
# Step 6: Commit changes
print("\nπΎ Step 6: Committing changes to SQLite...")
print("-" * 80)
try:
conn.commit()
print(f"β
Committed {inserted_count} collection record(s)")
except Exception as e:
print(f"β Error committing: {e}")
conn.close()
return False
# Step 7: Verify
print("\nβ
Step 7: Verifying collections in SQLite...")
print("-" * 80)
try:
cursor.execute("SELECT id, name FROM collections")
verified = cursor.fetchall()
print(f"β
Collections in SQLite: {len(verified)}")
for collection_id, name in verified:
print(f" β’ {name}")
conn.close()
if len(verified) > 0:
return True
else:
return False
except Exception as e:
print(f"β Error verifying: {e}")
conn.close()
return False
def main():
"""Main entry point."""
try:
success = rebuild_sqlite_directly()
print("\n" + "=" * 80)
if success:
print("β
SQLITE REBUILD COMPLETE!")
print("\nπ Next steps:")
print(" 1. Restart Streamlit: streamlit run streamlit_app.py")
print(" 2. Check 'Existing Collections' dropdown")
print(" 3. Your collections should now appear")
print(" 4. Load a collection and verify data is intact")
exit_code = 0
else:
print("β SQLITE REBUILD FAILED")
print("\nπ‘ Try manual approach:")
print(" 1. Delete chroma.sqlite3")
print(" 2. Restart Streamlit to create fresh database")
print(" 3. Manually re-upload datasets to recreate collections")
exit_code = 1
print("=" * 80 + "\n")
sys.exit(exit_code)
except Exception as e:
print(f"\nβ FATAL ERROR: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()
|