forked from humanlayer/humanlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy-schema.sql
More file actions
38 lines (34 loc) · 1008 Bytes
/
y-schema.sql
File metadata and controls
38 lines (34 loc) · 1008 Bytes
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
-- Create the notes table
CREATE TABLE notes (
id SERIAL PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
-- Create the notes_operations table
CREATE TABLE notes_operations (
id SERIAL PRIMARY KEY,
note_id INTEGER REFERENCES notes(id) ON DELETE CASCADE,
op BYTEA NOT NULL
);
-- Create the ydoc_awareness table
CREATE TABLE ydoc_awareness (
clientId TEXT,
note_id INTEGER REFERENCES notes(id) ON DELETE CASCADE,
op BYTEA NOT NULL,
updated TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (clientId, note_id)
);
-- Create function to clean up old awareness data
CREATE OR REPLACE FUNCTION delete_old_rows()
RETURNS TRIGGER AS $$
BEGIN
DELETE FROM ydoc_awareness
WHERE updated < NOW() - INTERVAL '2 minutes';
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
-- Create trigger for cleanup
CREATE TRIGGER delete_old_rows_trigger
AFTER INSERT OR UPDATE ON ydoc_awareness
FOR EACH STATEMENT
EXECUTE FUNCTION delete_old_rows();