Phase 1: Database Schema Implementation - Migration 010: Sessions table with bulletproof auto-incrementing - Unique session IDs across all projects - Per-project session numbers (1, 2, 3, ...) - Atomic sequence generation (no race conditions) - Session-task and session-commit linking - Semantic search with pgvector HNSW indexes - Migration 011: Memories table with enhanced schema - Migrated existing session_memories to memories - Added session_id and task_id foreign keys - Renamed columns for consistency - HNSW indexing for semantic search - Migration 012: Builds table for CI/CD tracking - Links builds to sessions and versions - Tracks build status, timing, and metadata All migrations tested and verified on agiliton database. Related: CF-167 (Fix shared session-summary.md file conflict)
59 lines
2.7 KiB
SQL
59 lines
2.7 KiB
SQL
-- Migration 011: Memories/Learnings table with semantic search
|
|
-- Purpose: Migrate existing session_memories to memories table with enhanced schema
|
|
-- Dependencies: 001_base_schema.sql (pgvector), 010_sessions.sql (sessions table)
|
|
|
|
-- Rename existing session_memories table to memories
|
|
ALTER TABLE IF EXISTS session_memories RENAME TO memories;
|
|
|
|
-- Add missing columns
|
|
ALTER TABLE memories
|
|
ADD COLUMN IF NOT EXISTS task_id TEXT REFERENCES tasks(id) ON DELETE SET NULL;
|
|
|
|
-- Rename source_session to session_id for consistency
|
|
ALTER TABLE memories
|
|
RENAME COLUMN source_session TO session_id;
|
|
|
|
-- Add foreign key constraint to sessions table
|
|
ALTER TABLE memories
|
|
ADD CONSTRAINT fk_memories_session
|
|
FOREIGN KEY (session_id) REFERENCES sessions(id) ON DELETE SET NULL;
|
|
|
|
-- Rename times_surfaced to access_count for consistency
|
|
ALTER TABLE memories
|
|
RENAME COLUMN times_surfaced TO access_count;
|
|
|
|
-- Rename last_surfaced to last_accessed_at for consistency
|
|
ALTER TABLE memories
|
|
RENAME COLUMN last_surfaced TO last_accessed_at;
|
|
|
|
-- Change varchar columns to TEXT for consistency
|
|
ALTER TABLE memories
|
|
ALTER COLUMN project TYPE TEXT;
|
|
|
|
-- Note: Keeping embedding as vector(1536) to preserve existing data
|
|
-- Future embeddings can use vector(1024) by updating the column if needed
|
|
|
|
-- Add missing indexes
|
|
CREATE INDEX IF NOT EXISTS idx_memories_session ON memories(session_id);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_task ON memories(task_id);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_project ON memories(project);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_category ON memories(category);
|
|
CREATE INDEX IF NOT EXISTS idx_memories_created ON memories(created_at DESC);
|
|
|
|
-- Drop old index and create HNSW index for better performance
|
|
-- Note: Existing index is ivfflat, we want hnsw
|
|
DROP INDEX IF EXISTS idx_session_memories_embedding;
|
|
CREATE INDEX IF NOT EXISTS idx_memories_embedding ON memories USING hnsw (embedding vector_cosine_ops);
|
|
|
|
-- Full-text search on title and content (for keyword search)
|
|
CREATE INDEX IF NOT EXISTS idx_memories_fts ON memories USING gin(
|
|
to_tsvector('english', title || ' ' || content)
|
|
);
|
|
|
|
-- Comments for documentation
|
|
COMMENT ON TABLE memories IS 'Learnings and patterns discovered during development sessions';
|
|
COMMENT ON COLUMN memories.category IS 'Type of memory: pattern (reusable solution), fix (bug resolution), preference (user choice), gotcha (trap/pitfall), architecture (design decision)';
|
|
COMMENT ON COLUMN memories.embedding IS 'Vector embedding of title + content for semantic search';
|
|
COMMENT ON COLUMN memories.context IS 'Optional context describing when/where this memory applies';
|
|
COMMENT ON COLUMN memories.access_count IS 'Number of times this memory has been retrieved (for relevance ranking)';
|