-
Notifications
You must be signed in to change notification settings - Fork 164
fix(store): migrate sqlite-vec table to partition keys #416
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
Open
arabold
wants to merge
5
commits into
main
Choose a base branch
from
fix/411-sqlite-vec-partition-keys
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2b4e886
fix(store): migrate vectors to partition keys
arabold 4b010de
fix(store): derive vector partition keys during rebuild
arabold 96b5ce2
fix(store): harden migration workflow
arabold 7d900c3
fix(store): handle vector schema variations
arabold 74cc618
chore(db): add progress markers to older migrations
arabold 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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,56 @@ | ||
| -- Migration: rebuild documents_vec with sqlite-vec partition keys | ||
| -- This enables selective KNN queries by library_id and version_id. | ||
|
|
||
| -- @migration-step preserve existing vectors | ||
| -- Preserve compatible vectors from the existing vec table. The migration | ||
| -- runner replaces __DOCUMENTS_VEC_DIMENSION__ with the current documents_vec | ||
| -- dimension so databases already reconciled to a custom embedding dimension | ||
| -- keep their existing vector size. This uses a disk-backed staging table | ||
| -- because large vector indexes can exceed memory. | ||
| DROP TABLE IF EXISTS _documents_vec_partition_migration; | ||
|
|
||
| CREATE TABLE _documents_vec_partition_migration AS | ||
| SELECT | ||
| d.id AS rowid, | ||
| v.library_id, | ||
| v.id AS version_id, | ||
| dv.embedding | ||
| FROM documents_vec dv | ||
| JOIN documents d ON dv.rowid = d.id | ||
| JOIN pages p ON d.page_id = p.id | ||
| JOIN versions v ON p.version_id = v.id | ||
| WHERE vec_length(dv.embedding) = __DOCUMENTS_VEC_DIMENSION__; | ||
|
|
||
| -- @migration-step rebuild vector table | ||
| DROP TABLE documents_vec; | ||
|
|
||
| CREATE VIRTUAL TABLE documents_vec USING vec0( | ||
| library_id INTEGER partition key, | ||
| version_id INTEGER partition key, | ||
| embedding FLOAT[__DOCUMENTS_VEC_DIMENSION__] | ||
| ); | ||
|
|
||
| -- @migration-step restore existing vectors | ||
| INSERT OR REPLACE INTO documents_vec (rowid, library_id, version_id, embedding) | ||
| SELECT rowid, library_id, version_id, embedding | ||
| FROM _documents_vec_partition_migration; | ||
|
|
||
| -- @migration-step backfill missing vectors | ||
| -- Backfill any vectors stored on documents but missing from the vec table. | ||
| INSERT OR REPLACE INTO documents_vec (rowid, library_id, version_id, embedding) | ||
| SELECT | ||
| d.id, | ||
| v.library_id, | ||
| v.id AS version_id, | ||
| json_extract(d.embedding, '$') AS embedding | ||
| FROM documents d | ||
| JOIN pages p ON d.page_id = p.id | ||
| JOIN versions v ON p.version_id = v.id | ||
| WHERE d.embedding IS NOT NULL | ||
| AND vec_length(json_extract(d.embedding, '$')) = __DOCUMENTS_VEC_DIMENSION__ | ||
| AND NOT EXISTS ( | ||
| SELECT 1 FROM documents_vec existing WHERE existing.rowid = d.id | ||
| ); | ||
|
|
||
| -- @migration-step cleanup staging data | ||
| DROP TABLE _documents_vec_partition_migration; | ||
This file contains hidden or 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 hidden or 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 @@ | ||
| schema: spec-driven | ||
| created: 2026-06-06 |
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.
Uh oh!
There was an error while loading. Please reload this page.