- Create documents and document_chunks tables with status tracking, cascading deletes, and chunk uniqueness constraints - Enable pgvector and store 768-dimensional document embeddings - Add an initial source metadata migration placeholder - Add node-pg-migrate for database migration management - Implement Ollama embedding generation using nomic-embed-text with timeout and response validation
23 lines
797 B
SQL
23 lines
797 B
SQL
CREATE EXTENSION IF NOT EXISTS vector;
|
|
|
|
CREATE TABLE documents (
|
|
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
title TEXT NOT NULL,
|
|
source TEXT,
|
|
status TEXT NOT NULL DEFAULT 'pending'
|
|
CHECK (status IN ('pending', 'processing', 'completed', 'failed')),
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
|
);
|
|
|
|
CREATE TABLE document_chunks (
|
|
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
document_id BIGINT NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
chunk_index INTEGER NOT NULL CHECK (chunk_index >= 0),
|
|
content TEXT NOT NULL,
|
|
embedding vector(768) NOT NULL,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
UNIQUE (document_id, chunk_index)
|
|
);
|
|
|
|
CREATE INDEX document_chunks_document_id_idx
|
|
ON document_chunks (document_id);
|