Add Schema files

This commit is contained in:
Χγφτ Kompanion 2025-10-15 10:39:44 +13:00
parent 122085b1f8
commit 779ac57f50
6 changed files with 97 additions and 0 deletions

2
db/init/001_roles.sql Normal file
View File

@ -0,0 +1,2 @@
CREATE ROLE kompanion LOGIN PASSWORD 'komp';
CREATE DATABASE kompanion OWNER kompanion;

View File

@ -0,0 +1,2 @@
CREATE EXTENSION IF NOT EXISTS vector;
CREATE EXTENSION IF NOT EXISTS pg_trgm;

39
db/init/010_schema.sql Normal file
View File

@ -0,0 +1,39 @@
CREATE TABLE IF NOT EXISTS namespaces (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT UNIQUE NOT NULL
);
CREATE TABLE IF NOT EXISTS memory_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
namespace_id UUID NOT NULL REFERENCES namespaces(id) ON DELETE CASCADE,
key TEXT,
content TEXT NOT NULL,
metadata JSONB NOT NULL DEFAULT '{}',
tags TEXT[] NOT NULL DEFAULT '{}',
revision INT NOT NULL DEFAULT 1,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
expires_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ,
last_accessed_at TIMESTAMPTZ
);
CREATE TABLE IF NOT EXISTS memory_chunks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
item_id UUID NOT NULL REFERENCES memory_items(id) ON DELETE CASCADE,
seq INT NOT NULL,
content TEXT NOT NULL,
expires_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ,
content_tsv tsvector GENERATED ALWAYS AS (to_tsvector('english', content)) STORED
);
CREATE TABLE IF NOT EXISTS embeddings (
id BIGSERIAL PRIMARY KEY,
chunk_id UUID NOT NULL REFERENCES memory_chunks(id) ON DELETE CASCADE,
model TEXT NOT NULL,
dim INT NOT NULL,
vector VECTOR(1536),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(chunk_id, model)
);

15
db/init/020_indexes.sql Normal file
View File

@ -0,0 +1,15 @@
CREATE UNIQUE INDEX IF NOT EXISTS ux_items_ns_key
ON memory_items(namespace_id, key)
WHERE key IS NOT NULL;
CREATE INDEX IF NOT EXISTS ix_chunks_item ON memory_chunks(item_id);
CREATE INDEX IF NOT EXISTS idx_chunks_content_tsv
ON memory_chunks USING GIN(content_tsv);
CREATE INDEX IF NOT EXISTS ix_embed_model_dim ON embeddings(model, dim);
-- per-model ANN index (duplicate with each concrete model name)
CREATE INDEX IF NOT EXISTS ix_embed_vec_model_default
ON embeddings USING ivfflat (vector vector_cosine_ops)
WHERE model = 'default-emb';

22
db/scripts/create-prod-db.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -euo pipefail
DB_NAME=${1:-kompanion}
ROLE=${ROLE:-kompanion}
PASS=${PASS:-komp}
psql -v ON_ERROR_STOP=1 <<SQL
DO $$ BEGIN
PERFORM 1 FROM pg_roles WHERE rolname = '$ROLE';
IF NOT FOUND THEN EXECUTE format('CREATE ROLE %I LOGIN PASSWORD %L', '$ROLE', '$PASS'); END IF;
END $$;
DO $$ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_database WHERE datname = '$DB_NAME') THEN
EXECUTE format('CREATE DATABASE %I OWNER %I', '$DB_NAME', '$ROLE');
END IF;
END $$;
SQL
for f in db/init/*.sql; do
echo "Applying $f"
psql -d "$DB_NAME" -f "$f"
done

17
db/scripts/create-test-db.sh Executable file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env bash
set -euo pipefail
DB_NAME=${1:-kompanion_test}
ROLE=${ROLE:-kompanion}
PASS=${PASS:-komup}
psql -v ON_ERROR_STOP=1 <<SQL
DROP DATABASE IF EXISTS "$DB_NAME";
CREATE DATABASE "$DB_NAME" OWNER "$ROLE";
SQL
for f in db/init/*.sql; do
echo "Applying $f"
psql -d "$DB_NAME" -f "$f"
done
echo "✓ Database $DB_NAME initialized."