Defense-in-depth: The migration framework already handles missing inserts via INSERT OR REPLACE (db.rs:174), but adding explicit inserts to .sql files ensures consistency and makes migrations self-documenting. Migrations affected: - 022_notes_query_index - 024_note_documents - 025_note_dirty_backfill - 026_scoring_indexes - 027_surgical_sync_runs
25 lines
1.1 KiB
SQL
25 lines
1.1 KiB
SQL
-- Migration 022: Composite query indexes for notes + author_id column
|
|
-- Optimizes author-scoped and project-scoped date-range queries on notes.
|
|
-- Adds discussion JOIN indexes and immutable author identity column.
|
|
|
|
-- Composite index for author-scoped queries (who command, notes --author)
|
|
CREATE INDEX IF NOT EXISTS idx_notes_user_created
|
|
ON notes(project_id, author_username COLLATE NOCASE, created_at DESC, id DESC)
|
|
WHERE is_system = 0;
|
|
|
|
-- Composite index for project-scoped date-range queries
|
|
CREATE INDEX IF NOT EXISTS idx_notes_project_created
|
|
ON notes(project_id, created_at DESC, id DESC)
|
|
WHERE is_system = 0;
|
|
|
|
-- Discussion JOIN indexes
|
|
CREATE INDEX IF NOT EXISTS idx_discussions_issue_id ON discussions(issue_id);
|
|
CREATE INDEX IF NOT EXISTS idx_discussions_mr_id ON discussions(merge_request_id);
|
|
|
|
-- Immutable author identity column (GitLab numeric user ID)
|
|
ALTER TABLE notes ADD COLUMN author_id INTEGER;
|
|
CREATE INDEX IF NOT EXISTS idx_notes_author_id ON notes(author_id) WHERE author_id IS NOT NULL;
|
|
|
|
INSERT INTO schema_version (version, applied_at, description)
|
|
VALUES (22, strftime('%s', 'now') * 1000, '022_notes_query_index');
|