forked from chroma-core/chroma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Enabled FK constraints on all connections - Fixed FK for segments to collections - Implemented a new mechanism for disabling/enabling FKs for migration files (required to retrospectively apply it to tenants and databases migration as it was breaking FK constraints) - Removed unnecessary embedding metadata cleanup (taken care of by FKs) - Added utils lib to generate correct (topologically sorted in reverse) DROP statements for tables according FK constraints - Fixed client_test.py failing test - the test server dir was not removed so subsequent tests were failing - Fixed test_segment_manager.py where collection names were causing FK constraint failures
- Loading branch information
Showing
9 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from collections import defaultdict, deque | ||
from graphlib import TopologicalSorter | ||
from typing import List, Dict | ||
|
||
from chromadb.db.base import Cursor | ||
|
||
|
||
def fetch_tables(cursor: Cursor) -> List[str]: | ||
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';") | ||
return [row[0] for row in cursor.fetchall()] | ||
|
||
|
||
def fetch_foreign_keys(cursor: Cursor, table_name: str) -> List[str]: | ||
cursor.execute(f"PRAGMA foreign_key_list({table_name});") | ||
return [row[2] for row in cursor.fetchall()] # Table being referenced | ||
|
||
|
||
def build_dependency_graph(tables: List[str], cursor: Cursor) -> Dict[str, List[str]]: | ||
graph = defaultdict(list) | ||
for table in tables: | ||
foreign_keys = fetch_foreign_keys(cursor, table) | ||
for fk_table in foreign_keys: | ||
graph[table].append(fk_table) | ||
if not foreign_keys and table not in graph.keys(): | ||
graph[table] = [] | ||
|
||
return graph | ||
|
||
|
||
def topological_sort(graph: Dict[str, List[str]]) -> List[str]: | ||
ts = TopologicalSorter(graph) | ||
# Reverse the order since TopologicalSorter gives the order of dependencies, | ||
# but we want to drop tables in reverse dependency order | ||
return list(ts.static_order())[::-1] | ||
|
||
|
||
def get_drop_order(cursor: Cursor) -> List[str]: | ||
tables = fetch_tables(cursor) | ||
filtered_tables = [ | ||
table for table in tables if not table.startswith("embedding_fulltext_search_") | ||
] | ||
graph = build_dependency_graph(filtered_tables, cursor) | ||
drop_order = topological_sort(graph) | ||
return drop_order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- Disable foreign key constraints to us to update the segments table | ||
PRAGMA foreign_keys = OFF; | ||
|
||
CREATE TABLE embedding_metadata_temp ( | ||
id INTEGER REFERENCES embeddings(id) ON DELETE CASCADE NOT NULL, | ||
key TEXT NOT NULL, | ||
string_value TEXT, | ||
int_value INTEGER, | ||
float_value REAL, | ||
bool_value INTEGER, | ||
PRIMARY KEY (id, key) | ||
); | ||
|
||
INSERT INTO embedding_metadata_temp | ||
SELECT id, key, string_value, int_value, float_value, bool_value | ||
FROM embedding_metadata; | ||
|
||
DROP TABLE embedding_metadata; | ||
|
||
ALTER TABLE embedding_metadata_temp RENAME TO embedding_metadata; | ||
|
||
PRAGMA foreign_keys = ON; |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE segments_temp ( | ||
id TEXT PRIMARY KEY, | ||
type TEXT NOT NULL, | ||
scope TEXT NOT NULL, | ||
topic TEXT, | ||
collection TEXT REFERENCES collections(id) | ||
); | ||
|
||
INSERT INTO segments_temp SELECT * FROM segments; | ||
|
||
DROP TABLE segments; | ||
|
||
ALTER TABLE segments_temp RENAME TO segments; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters