-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmigrate.sql
More file actions
56 lines (38 loc) · 1.5 KB
/
migrate.sql
File metadata and controls
56 lines (38 loc) · 1.5 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
CREATE OR REPLACE FUNCTION "public"."send_message_to_realtime"() RETURNS "trigger"
LANGUAGE "plpgsql"
AS $$BEGIN
PERFORM realtime.send(
jsonb_build_object(
'id', new.id,
'content', new.content,
'username', new.username,
'createdAt', to_char(new.created_at, 'YYYY-MM-DD"T"HH24:MI:SS"Z"')
),
'message',
new.room,
true
);
RETURN NEW; -- Return the new row
END;$$;
ALTER FUNCTION "public"."send_message_to_realtime"() OWNER TO "postgres";
CREATE TABLE IF NOT EXISTS "public"."new_messages" (
"id" "uuid" DEFAULT "gen_random_uuid"() NOT NULL,
"created_at" timestamp with time zone DEFAULT "now"() NOT NULL,
"content" "text" NOT NULL,
"username" "text" NOT NULL,
"room" "text" NOT NULL
);
ALTER TABLE "public"."new_messages" OWNER TO "postgres";
ALTER TABLE ONLY "public"."new_messages"
ADD CONSTRAINT "new_messages_pkey" PRIMARY KEY ("id");
CREATE OR REPLACE TRIGGER "send_message_to_realtime" AFTER INSERT ON "public"."new_messages" FOR EACH ROW EXECUTE FUNCTION "public"."send_message_to_realtime"();
CREATE POLICY "Enable insert for anon users" ON "public"."new_messages" FOR INSERT TO "anon" WITH CHECK (true);
ALTER TABLE "public"."new_messages" ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Allow anon to listen for broadcast"
ON "realtime"."messages"
TO anon
using (true);
CREATE POLICY "Allow pushing broadcasts for anon users only"
ON "realtime"."messages"
TO anon
with CHECK (true);