-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.sql
More file actions
62 lines (53 loc) · 1.61 KB
/
setup.sql
File metadata and controls
62 lines (53 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS "Video" (
id TEXT PRIMARY KEY,
"videoUrl" TEXT NOT NULL,
slug TEXT NOT NULL,
transcription TEXT,
status TEXT NOT NULL,
title TEXT,
"isSearchable" BOOLEAN DEFAULT false,
"createdAt" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
"userId" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS "VideoChunk" (
id SERIAL PRIMARY KEY,
video_id TEXT REFERENCES "Video"(id),
chunk_text TEXT NOT NULL,
chunk_embedding vector(1536),
chunk_start_time INTERVAL,
chunk_end_time INTERVAL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);
-- Drop existing index if it exists
DROP INDEX IF EXISTS "VideoChunk_chunk_embedding_idx";
-- Recreate the index with explicit name
CREATE INDEX "VideoChunk_chunk_embedding_idx" ON "VideoChunk"
USING ivfflat (chunk_embedding vector_cosine_ops)
WITH (lists = 100);
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
NEW."updatedAt" = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ language 'plpgsql';
CREATE TRIGGER update_video_updated_at
BEFORE UPDATE ON "Video"
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
-- Add notification trigger for new videos
CREATE OR REPLACE FUNCTION notify_new_video()
RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('new_video', row_to_json(NEW)::text);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER video_inserted_trigger
AFTER INSERT ON "Video"
FOR EACH ROW
EXECUTE FUNCTION notify_new_video();
\dt
\dx