Fixing bug
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
-- Migration 033: AI Chat Schema
|
||||
-- Creates tables for the AI chat assistant extension:
|
||||
-- chat_sessions, chat_messages, knowledge_documents, and match_documents RPC
|
||||
|
||||
-- Enable pgvector for embedding storage
|
||||
create extension if not exists vector with schema extensions;
|
||||
|
||||
-- ============================================================
|
||||
-- chat_sessions
|
||||
-- ============================================================
|
||||
|
||||
create table public.chat_sessions (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
user_id uuid references auth.users on delete cascade not null,
|
||||
title text,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
alter table public.chat_sessions enable row level security;
|
||||
|
||||
create policy "chat_sessions_select" on public.chat_sessions
|
||||
for select using (auth.uid() = user_id);
|
||||
create policy "chat_sessions_insert" on public.chat_sessions
|
||||
for insert with check (auth.uid() = user_id);
|
||||
create policy "chat_sessions_update" on public.chat_sessions
|
||||
for update using (auth.uid() = user_id);
|
||||
create policy "chat_sessions_delete" on public.chat_sessions
|
||||
for delete using (auth.uid() = user_id);
|
||||
|
||||
create index idx_chat_sessions_user_created on public.chat_sessions (user_id, created_at desc);
|
||||
|
||||
create trigger chat_sessions_updated_at
|
||||
before update on public.chat_sessions
|
||||
for each row execute function public.update_updated_at_column();
|
||||
|
||||
-- ============================================================
|
||||
-- chat_messages
|
||||
-- ============================================================
|
||||
|
||||
create table public.chat_messages (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
session_id uuid references public.chat_sessions on delete cascade not null,
|
||||
user_id uuid references auth.users on delete cascade not null,
|
||||
role text not null check (role in ('user', 'assistant')),
|
||||
content text not null,
|
||||
sources jsonb,
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
alter table public.chat_messages enable row level security;
|
||||
|
||||
create policy "chat_messages_select" on public.chat_messages
|
||||
for select using (auth.uid() = user_id);
|
||||
create policy "chat_messages_insert" on public.chat_messages
|
||||
for insert with check (auth.uid() = user_id);
|
||||
create policy "chat_messages_update" on public.chat_messages
|
||||
for update using (auth.uid() = user_id);
|
||||
create policy "chat_messages_delete" on public.chat_messages
|
||||
for delete using (auth.uid() = user_id);
|
||||
|
||||
create index idx_chat_messages_session on public.chat_messages (session_id, created_at);
|
||||
|
||||
-- ============================================================
|
||||
-- knowledge_documents
|
||||
-- ============================================================
|
||||
|
||||
create table public.knowledge_documents (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
source_file text not null,
|
||||
title text not null,
|
||||
section_title text,
|
||||
content text not null,
|
||||
content_hash text unique not null,
|
||||
embedding extensions.vector(1536),
|
||||
metadata jsonb default '{}',
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
alter table public.knowledge_documents enable row level security;
|
||||
|
||||
-- Knowledge documents are shared — any authenticated user can read
|
||||
create policy "knowledge_documents_select" on public.knowledge_documents
|
||||
for select using (true);
|
||||
|
||||
create index idx_knowledge_documents_hash on public.knowledge_documents (content_hash);
|
||||
|
||||
-- ============================================================
|
||||
-- match_documents RPC (vector similarity search)
|
||||
-- ============================================================
|
||||
|
||||
create or replace function public.match_documents(
|
||||
query_embedding extensions.vector,
|
||||
match_count int default 5,
|
||||
match_threshold float default 0.7
|
||||
)
|
||||
returns table (
|
||||
id uuid,
|
||||
source_file text,
|
||||
title text,
|
||||
section_title text,
|
||||
content text,
|
||||
metadata jsonb,
|
||||
similarity float
|
||||
)
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = public, extensions
|
||||
as $$
|
||||
begin
|
||||
return query
|
||||
select
|
||||
kd.id,
|
||||
kd.source_file,
|
||||
kd.title,
|
||||
kd.section_title,
|
||||
kd.content,
|
||||
kd.metadata,
|
||||
1 - (kd.embedding <=> query_embedding)::float as similarity
|
||||
from public.knowledge_documents kd
|
||||
where 1 - (kd.embedding <=> query_embedding) >= match_threshold
|
||||
order by kd.embedding <=> query_embedding
|
||||
limit match_count;
|
||||
end;
|
||||
$$;
|
||||
|
||||
grant execute on function public.match_documents(extensions.vector, int, float) to authenticated;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Migration 034: Fix extension_data updated_at trigger
|
||||
-- The original trigger references update_updated_at() which does not exist.
|
||||
-- The correct function is public.update_updated_at_column().
|
||||
-- Wrapped in DO block in case extension_data table does not yet exist.
|
||||
|
||||
do $$
|
||||
begin
|
||||
if exists (
|
||||
select 1 from information_schema.tables
|
||||
where table_schema = 'public' and table_name = 'extension_data'
|
||||
) then
|
||||
drop trigger if exists extension_data_updated_at on public.extension_data;
|
||||
|
||||
create trigger extension_data_updated_at
|
||||
before update on public.extension_data
|
||||
for each row execute function public.update_updated_at_column();
|
||||
end if;
|
||||
end;
|
||||
$$;
|
||||
@@ -0,0 +1,28 @@
|
||||
-- Migration 035: Fix push notifications
|
||||
-- 1. Expand notification_log notification_type CHECK constraint to include new event types
|
||||
-- 2. Add per-event enabled columns to notification_settings
|
||||
|
||||
-- Drop and recreate the CHECK constraint with new types
|
||||
alter table public.notification_log
|
||||
drop constraint if exists notification_log_notification_type_check;
|
||||
|
||||
alter table public.notification_log
|
||||
add constraint notification_log_notification_type_check
|
||||
check (notification_type in (
|
||||
'tax_deadline',
|
||||
'invoice_due',
|
||||
'invoice_overdue',
|
||||
'period_locked',
|
||||
'period_year_closed',
|
||||
'invoice_sent',
|
||||
'receipt_extracted',
|
||||
'receipt_matched'
|
||||
));
|
||||
|
||||
-- Add new per-event enabled columns to notification_settings
|
||||
alter table public.notification_settings
|
||||
add column if not exists period_locked_enabled boolean default true,
|
||||
add column if not exists period_year_closed_enabled boolean default true,
|
||||
add column if not exists invoice_sent_enabled boolean default false,
|
||||
add column if not exists receipt_extracted_enabled boolean default true,
|
||||
add column if not exists receipt_matched_enabled boolean default true;
|
||||
@@ -0,0 +1,5 @@
|
||||
-- Migration 036: Fix Enable Banking
|
||||
-- Add authorization_id column to bank_connections for PSD2 authorization tracking
|
||||
|
||||
alter table public.bank_connections
|
||||
add column if not exists authorization_id text;
|
||||
@@ -0,0 +1,37 @@
|
||||
-- Migration 038: Fix match_documents search_path
|
||||
-- The function needs the extensions schema in search_path to use pgvector operators.
|
||||
|
||||
create or replace function public.match_documents(
|
||||
query_embedding extensions.vector,
|
||||
match_count int default 5,
|
||||
match_threshold float default 0.7
|
||||
)
|
||||
returns table (
|
||||
id uuid,
|
||||
source_file text,
|
||||
title text,
|
||||
section_title text,
|
||||
content text,
|
||||
metadata jsonb,
|
||||
similarity float
|
||||
)
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = public, extensions
|
||||
as $$
|
||||
begin
|
||||
return query
|
||||
select
|
||||
kd.id,
|
||||
kd.source_file,
|
||||
kd.title,
|
||||
kd.section_title,
|
||||
kd.content,
|
||||
kd.metadata,
|
||||
1 - (kd.embedding <=> query_embedding)::float as similarity
|
||||
from public.knowledge_documents kd
|
||||
where 1 - (kd.embedding <=> query_embedding) >= match_threshold
|
||||
order by kd.embedding <=> query_embedding
|
||||
limit match_count;
|
||||
end;
|
||||
$$;
|
||||
Reference in New Issue
Block a user