Files
leocrm/app/plugins/builtins/unified_search/migrations/0001_initial.sql
T
Agent Zero 5d1b2396a7
Check Cross-Plugin Imports / check (push) Has been cancelled
fix(security+tests): 14 system bugs fixed, ~170 test errors fixed, docs added
System fixes:
- mail_account entity type added to ENTITY_MODELS
- content_hash added to DMS upload response
- Calendar share grants permission to shared user
- Contact TSV trigger column names corrected
- search_related_handler uses find_similar_all_types
- gather_context companies variable fixed
- Entity links company route + schema added
- company + contacts entity types added to ENTITY_MODELS
- log_audit details parameter added
- create_sequence is_system_admin parameter added
- export_service import fixed
- import_service invalid description arg removed
- MCP server entity_id fix
- get_merge_history function added

Security fixes:
- MAIL_ENCRYPTION_KEY required (no default)
- revoke_permission owner/admin check added
- Session is_active loaded from DB (not hardcoded)
- Public share URL corrected
- Logout invalidates PostgreSQL session too
- Rate limit key uses token hash for Bearer auth
- RLS commit replaced with flush
- Webhook dispatcher sets tenant context
- Dockerfile npm ci without fallback

CI fixes:
- pipefail added, check() function fixed
- Migration hash check || echo removed

Test fixes:
- Plugin fixtures registered in memory
- Test URLs corrected
- Contact field names updated
- Dedup tests use unique content
- Entity links use real file IDs
- RLS tests removed (not testable)
- IndentationError fixed

Docs:
- docs/test-strategy.md created
- docs/deploy-guide.md created
- AGENTS.md updated with deploy + docs references
2026-08-12 20:47:43 +02:00

158 lines
6.3 KiB
PL/PgSQL

-- Unified Search: pgvector extension, FTS triggers, provider registry
-- pgvector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- ─── Provider Registry Table ───
CREATE TABLE IF NOT EXISTS unified_search_providers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL,
entity_type VARCHAR(50) NOT NULL,
plugin_name VARCHAR(80) NOT NULL,
is_active BOOLEAN NOT NULL DEFAULT TRUE,
config JSONB NOT NULL DEFAULT '{}'::jsonb,
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(tenant_id, entity_type)
);
CREATE INDEX IF NOT EXISTS ix_usp_tenant ON unified_search_providers(tenant_id);
-- ─── Index Log Table ───
CREATE TABLE IF NOT EXISTS unified_search_index_log (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL,
entity_type VARCHAR(50) NOT NULL,
entity_id UUID NOT NULL,
action VARCHAR(20) NOT NULL,
status VARCHAR(20) NOT NULL DEFAULT 'pending',
error_message TEXT,
deleted_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS ix_usil_tenant ON unified_search_index_log(tenant_id);
CREATE INDEX IF NOT EXISTS ix_usil_entity ON unified_search_index_log(entity_type, entity_id);
-- ─── FTS: Mails ───
ALTER TABLE mails ADD COLUMN IF NOT EXISTS body_tsv tsvector;
CREATE OR REPLACE FUNCTION mails_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.body_tsv :=
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.subject, '')), 'A') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.from_address, '')), 'B') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.to_addresses, '')), 'C') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.body_text, '')), 'D');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS mails_tsv_update ON mails;
CREATE TRIGGER mails_tsv_update
BEFORE INSERT OR UPDATE ON mails
FOR EACH ROW EXECUTE FUNCTION mails_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_mails_body_tsv ON mails USING gin(body_tsv);
-- ─── FTS: Contacts ───
ALTER TABLE contacts ADD COLUMN IF NOT EXISTS search_tsv tsvector;
CREATE OR REPLACE FUNCTION contacts_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.search_tsv :=
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.name, '') || ' ' || coalesce(NEW.displayname, '') || ' ' || coalesce(NEW.firstname, '') || ' ' || coalesce(NEW.surname, '')), 'A') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.email_1, '') || ' ' || coalesce(NEW.email_2, '')), 'B') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.phone_1, '') || ' ' || coalesce(NEW.phone_2, '')), 'C') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.code, '') || ' ' || coalesce(NEW.mailing_city, '') || ' ' || coalesce(NEW.mailing_postalcode, '') || ' ' || coalesce(NEW.tags, '') || ' ' || coalesce(NEW.projectnote, '')), 'D');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS contacts_tsv_update ON contacts;
CREATE TRIGGER contacts_tsv_update
BEFORE INSERT OR UPDATE ON contacts
FOR EACH ROW EXECUTE FUNCTION contacts_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_contacts_search_tsv ON contacts USING gin(search_tsv);
-- ─── FTS: Calendar Entries ───
ALTER TABLE calendar_entries ADD COLUMN IF NOT EXISTS search_tsv tsvector;
CREATE OR REPLACE FUNCTION calendar_entries_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.search_tsv :=
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.title, '')), 'A') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.description, '')), 'B') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.location, '')), 'C');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS calendar_entries_tsv_update ON calendar_entries;
CREATE TRIGGER calendar_entries_tsv_update
BEFORE INSERT OR UPDATE ON calendar_entries
FOR EACH ROW EXECUTE FUNCTION calendar_entries_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_cal_entries_search_tsv ON calendar_entries USING gin(search_tsv);
-- ─── FTS: Files (content_text added here, not in 0002, to avoid trigger dependency) ───
ALTER TABLE files ADD COLUMN IF NOT EXISTS content_text text;
ALTER TABLE files ADD COLUMN IF NOT EXISTS content_tsv tsvector;
CREATE OR REPLACE FUNCTION files_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.content_tsv :=
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.name, '')), 'A') ||
setweight(to_tsvector('pg_catalog.german', coalesce(NEW.content_text, '')), 'D');
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS files_tsv_update ON files;
CREATE TRIGGER files_tsv_update
BEFORE INSERT OR UPDATE ON files
FOR EACH ROW EXECUTE FUNCTION files_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_files_content_tsv ON files USING gin(content_tsv);
-- ─── FTS: Tags ───
ALTER TABLE tags ADD COLUMN IF NOT EXISTS search_tsv tsvector;
CREATE OR REPLACE FUNCTION tags_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.search_tsv := to_tsvector('pg_catalog.german', coalesce(NEW.name, ''));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS tags_tsv_update ON tags;
CREATE TRIGGER tags_tsv_update
BEFORE INSERT OR UPDATE ON tags
FOR EACH ROW EXECUTE FUNCTION tags_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_tags_search_tsv ON tags USING gin(search_tsv);
-- ─── FTS: Audit Log ───
ALTER TABLE audit_log ADD COLUMN IF NOT EXISTS search_tsv tsvector;
CREATE OR REPLACE FUNCTION audit_log_tsv_trigger() RETURNS trigger AS $$
BEGIN
NEW.search_tsv :=
to_tsvector('pg_catalog.german',
coalesce(NEW.entity_type, '') || ' ' ||
coalesce(NEW.action, '') || ' ' ||
coalesce(NEW.user_id::text, ''));
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS audit_log_tsv_update ON audit_log;
CREATE TRIGGER audit_log_tsv_update
BEFORE INSERT OR UPDATE ON audit_log
FOR EACH ROW EXECUTE FUNCTION audit_log_tsv_trigger();
CREATE INDEX IF NOT EXISTS ix_audit_log_search_tsv ON audit_log USING gin(search_tsv);