-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ENH] Rust Sqlite migrations #3599
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0a5f2cb
[ENH] Rust Sqlite migrations
HammadB 4b8bbed
Sqlite db
HammadB 77aa506
cln
HammadB 1941096
merge main
HammadB 7e428f5
context save
HammadB 07e7744
refactor for cleanliness
HammadB d33fa39
cln
HammadB e0574af
tmp
HammadB b8a841c
cln
HammadB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 @@ | ||
[package] | ||
name = "sqlite" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
sqlx = { workspace = true } | ||
sha2 = { workspace = true} | ||
regex = { workspace = true } | ||
tokio = { workspace = true} | ||
md5 = { workspace = true} | ||
rust-embed = {version = "8.5.0", features = ["include-exclude"]} | ||
thiserror = { workspace = true } | ||
|
||
[dev-dependencies] | ||
tempfile = { workspace = true } |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unneeded |
Empty file.
10 changes: 10 additions & 0 deletions
10
rust/sqlite/migrations/embeddings_queue/00001-embeddings.sqlite.sql
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,10 @@ | ||
CREATE TABLE embeddings_queue ( | ||
seq_id INTEGER PRIMARY KEY, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
operation INTEGER NOT NULL, | ||
topic TEXT NOT NULL, | ||
id TEXT NOT NULL, | ||
vector BLOB, | ||
encoding TEXT, | ||
metadata TEXT | ||
); |
4 changes: 4 additions & 0 deletions
4
rust/sqlite/migrations/embeddings_queue/00002-embeddings-queue-config.sqlite.sql
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,4 @@ | ||
CREATE TABLE embeddings_queue_config ( | ||
id INTEGER PRIMARY KEY, | ||
config_json_str TEXT | ||
); |
24 changes: 24 additions & 0 deletions
24
rust/sqlite/migrations/metadb/00001-embedding-metadata.sqlite.sql
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,24 @@ | ||
CREATE TABLE embeddings ( | ||
id INTEGER PRIMARY KEY, | ||
segment_id TEXT NOT NULL, | ||
embedding_id TEXT NOT NULL, | ||
seq_id BLOB NOT NULL, | ||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
UNIQUE (segment_id, embedding_id) | ||
); | ||
|
||
CREATE TABLE embedding_metadata ( | ||
id INTEGER REFERENCES embeddings(id), | ||
key TEXT NOT NULL, | ||
string_value TEXT, | ||
int_value INTEGER, | ||
float_value REAL, | ||
PRIMARY KEY (id, key) | ||
); | ||
|
||
CREATE TABLE max_seq_id ( | ||
segment_id TEXT PRIMARY KEY, | ||
seq_id BLOB NOT NULL | ||
); | ||
|
||
CREATE VIRTUAL TABLE embedding_fulltext USING fts5(id, string_value); |
5 changes: 5 additions & 0 deletions
5
rust/sqlite/migrations/metadb/00002-embedding-metadata.sqlite.sql
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,5 @@ | ||
-- SQLite does not support adding check with alter table, as a result, adding a check | ||
-- involve creating a new table and copying the data over. It is over kill with adding | ||
-- a boolean type column. The application write to the table needs to ensure the data | ||
-- integrity. | ||
ALTER TABLE embedding_metadata ADD COLUMN bool_value INTEGER |
3 changes: 3 additions & 0 deletions
3
rust/sqlite/migrations/metadb/00003-full-text-tokenize.sqlite.sql
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,3 @@ | ||
CREATE VIRTUAL TABLE embedding_fulltext_search USING fts5(string_value, tokenize='trigram'); | ||
INSERT INTO embedding_fulltext_search (rowid, string_value) SELECT rowid, string_value FROM embedding_metadata; | ||
DROP TABLE embedding_fulltext; |
3 changes: 3 additions & 0 deletions
3
rust/sqlite/migrations/metadb/00004-metadata-indices.sqlite.sql
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,3 @@ | ||
CREATE INDEX IF NOT EXISTS embedding_metadata_int_value ON embedding_metadata (key, int_value) WHERE int_value IS NOT NULL; | ||
CREATE INDEX IF NOT EXISTS embedding_metadata_float_value ON embedding_metadata (key, float_value) WHERE float_value IS NOT NULL; | ||
CREATE INDEX IF NOT EXISTS embedding_metadata_string_value ON embedding_metadata (key, string_value) WHERE string_value IS NOT NULL; |
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,15 @@ | ||
CREATE TABLE collections ( | ||
id TEXT PRIMARY KEY, | ||
name TEXT NOT NULL, | ||
topic TEXT NOT NULL, | ||
UNIQUE (name) | ||
); | ||
|
||
CREATE TABLE collection_metadata ( | ||
collection_id TEXT REFERENCES collections(id) ON DELETE CASCADE, | ||
key TEXT NOT NULL, | ||
str_value TEXT, | ||
int_value INTEGER, | ||
float_value REAL, | ||
PRIMARY KEY (collection_id, key) | ||
); |
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,16 @@ | ||
CREATE TABLE segments ( | ||
id TEXT PRIMARY KEY, | ||
type TEXT NOT NULL, | ||
scope TEXT NOT NULL, | ||
topic TEXT, | ||
collection TEXT REFERENCES collection(id) | ||
); | ||
|
||
CREATE TABLE segment_metadata ( | ||
segment_id TEXT REFERENCES segments(id) ON DELETE CASCADE, | ||
key TEXT NOT NULL, | ||
str_value TEXT, | ||
int_value INTEGER, | ||
float_value REAL, | ||
PRIMARY KEY (segment_id, key) | ||
); |
1 change: 1 addition & 0 deletions
1
rust/sqlite/migrations/sysdb/00003-collection-dimension.sqlite.sql
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 @@ | ||
ALTER TABLE collections ADD COLUMN dimension INTEGER; |
29 changes: 29 additions & 0 deletions
29
rust/sqlite/migrations/sysdb/00004-tenants-databases.sqlite.sql
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,29 @@ | ||
CREATE TABLE IF NOT EXISTS tenants ( | ||
id TEXT PRIMARY KEY, | ||
UNIQUE (id) | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS databases ( | ||
id TEXT PRIMARY KEY, -- unique globally | ||
name TEXT NOT NULL, -- unique per tenant | ||
tenant_id TEXT NOT NULL REFERENCES tenants(id) ON DELETE CASCADE, | ||
UNIQUE (tenant_id, name) -- Ensure that a tenant has only one database with a given name | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS collections_tmp ( | ||
id TEXT PRIMARY KEY, -- unique globally | ||
name TEXT NOT NULL, -- unique per database | ||
topic TEXT NOT NULL, | ||
dimension INTEGER, | ||
database_id TEXT NOT NULL REFERENCES databases(id) ON DELETE CASCADE, | ||
UNIQUE (name, database_id) | ||
); | ||
|
||
-- Create default tenant and database | ||
INSERT OR REPLACE INTO tenants (id) VALUES ('default_tenant'); -- The default tenant id is 'default_tenant' others are UUIDs | ||
INSERT OR REPLACE INTO databases (id, name, tenant_id) VALUES ('00000000-0000-0000-0000-000000000000', 'default_database', 'default_tenant'); | ||
|
||
INSERT OR REPLACE INTO collections_tmp (id, name, topic, dimension, database_id) | ||
SELECT id, name, topic, dimension, '00000000-0000-0000-0000-000000000000' FROM collections; | ||
DROP TABLE collections; | ||
ALTER TABLE collections_tmp RENAME TO collections; |
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,4 @@ | ||
-- Remove the topic column from the Collections and Segments tables | ||
|
||
ALTER TABLE collections DROP COLUMN topic; | ||
ALTER TABLE segments DROP COLUMN topic; |
6 changes: 6 additions & 0 deletions
6
rust/sqlite/migrations/sysdb/00006-collection-segment-metadata.sqlite.sql
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,6 @@ | ||
-- SQLite does not support adding check with alter table, as a result, adding a check | ||
-- involve creating a new table and copying the data over. It is over kill with adding | ||
-- a boolean type column. The application write to the table needs to ensure the data | ||
-- integrity. | ||
ALTER TABLE collection_metadata ADD COLUMN bool_value INTEGER; | ||
ALTER TABLE segment_metadata ADD COLUMN bool_value INTEGER; |
2 changes: 2 additions & 0 deletions
2
rust/sqlite/migrations/sysdb/00007-collection-config.sqlite.sql
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,2 @@ | ||
-- Stores collection configuration dictionaries. | ||
ALTER TABLE collections ADD COLUMN config_json_str TEXT; |
7 changes: 7 additions & 0 deletions
7
rust/sqlite/migrations/sysdb/00008-maintenance-log.sqlite.sql
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,7 @@ | ||
-- Records when database maintenance operations are performed. | ||
-- At time of creation, this table is only used to record vacuum operations. | ||
CREATE TABLE maintenance_log ( | ||
id INT PRIMARY KEY, | ||
timestamp INT NOT NULL, | ||
operation TEXT NOT NULL | ||
); |
11 changes: 11 additions & 0 deletions
11
rust/sqlite/migrations/sysdb/00009-segment-collection-not-null.sqlite.sql
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,11 @@ | ||
-- This makes segments.collection non-nullable. | ||
CREATE TABLE segments_temp ( | ||
id TEXT PRIMARY KEY, | ||
type TEXT NOT NULL, | ||
scope TEXT NOT NULL, | ||
collection TEXT REFERENCES collection(id) NOT NULL | ||
); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#[derive(Clone)] | ||
pub struct SqliteDBConfig { | ||
// The SQLite database URL | ||
pub url: String, | ||
pub hash_type: MigrationHash, | ||
pub migration_mode: MigrationMode, | ||
} | ||
|
||
/// Migration mode for the database | ||
/// - Apply: Apply the migrations | ||
/// - Validate: Validate the applied migrations and ensure none are unappliued | ||
#[derive(Clone, PartialEq)] | ||
pub enum MigrationMode { | ||
Apply, | ||
Validate, | ||
} | ||
|
||
/// The hash function to use for the migration files | ||
/// - SHA256: Use SHA256 hash | ||
/// - MD5: Use MD5 hash | ||
#[derive(Clone)] | ||
pub enum MigrationHash { | ||
SHA256, | ||
MD5, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please name chroma-sqlite.