Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions database/graphql/message.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mutation addMessage($user_uuid: uuid!, $room_uuid: uuid!, $content: String!) {
insert_message_one(object: {user_uuid: $user_uuid, room_uuid: $room_uuid, content: $content}) {
mutation addMessage($user_uuid: uuid!, $room_uuid: uuid!, $content: String!, $reply_to: uuid) {
insert_message_one(object: {user_uuid: $user_uuid, room_uuid: $room_uuid, content: $content, reply_to: $reply_to}) {
uuid
}
}
Expand All @@ -13,5 +13,13 @@ subscription getMessagesByRoom($room_uuid: uuid!) {
}
content
created_at
reply_to
replied_to {
uuid
content
user {
username
}
}
}
}
25 changes: 24 additions & 1 deletion database/hasura/hasura_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": 3,
"sources": [
{
"name": "workshop",
"name": "web-workshop",
"kind": "postgres",
"tables": [
{
Expand All @@ -13,6 +13,12 @@
"schema": "public"
},
"object_relationships": [
{
"name": "replied_to",
"using": {
"foreign_key_constraint_on": "reply_to"
}
},
{
"name": "room",
"using": {
Expand All @@ -26,6 +32,20 @@
}
}
],
"array_relationships": [
{
"name": "replies",
"using": {
"foreign_key_constraint_on": {
"column": "reply_to",
"table": {
"name": "message",
"schema": "public"
}
}
}
}
],
"insert_permissions": [
{
"role": "user",
Expand All @@ -34,6 +54,7 @@
"columns": [
"content",
"created_at",
"reply_to",
"room_uuid",
"user_uuid",
"uuid"
Expand All @@ -49,6 +70,7 @@
"columns": [
"content",
"created_at",
"reply_to",
"room_uuid",
"user_uuid",
"uuid"
Expand All @@ -65,6 +87,7 @@
"columns": [
"content",
"created_at",
"reply_to",
"room_uuid",
"user_uuid",
"uuid"
Expand Down
4 changes: 2 additions & 2 deletions database/hasura/metadata/databases/databases.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
- name: workshop
- name: web-workshop
kind: postgres
configuration:
connection_info:
database_url:
from_env: PG_DATABASE_URL
isolation_level: read-committed
use_prepared_statements: false
tables: "!include workshop/tables/tables.yaml"
tables: "!include web-workshop/tables/tables.yaml"
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@ table:
name: message
schema: public
object_relationships:
- name: replied_to
using:
foreign_key_constraint_on: reply_to
- name: room
using:
foreign_key_constraint_on: room_uuid
- name: user
using:
foreign_key_constraint_on: user_uuid
array_relationships:
- name: replies
using:
foreign_key_constraint_on:
column: reply_to
table:
name: message
schema: public
insert_permissions:
- role: user
permission:
check: {}
columns:
- content
- created_at
- reply_to
- room_uuid
- user_uuid
- uuid
Expand All @@ -25,6 +37,7 @@ select_permissions:
columns:
- content
- created_at
- reply_to
- room_uuid
- user_uuid
- uuid
Expand All @@ -36,6 +49,7 @@ update_permissions:
columns:
- content
- created_at
- reply_to
- room_uuid
- user_uuid
- uuid
Expand Down
31 changes: 31 additions & 0 deletions database/sql/message.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,38 @@ create table if not exists public.message (
room_uuid uuid not null,
content text not null,
created_at timestamp default current_timestamp not null,
reply_to uuid,
primary key (uuid)
);
alter table public.message
add constraint message_user_uuid_fkey foreign key (user_uuid) references public.user (uuid) on update cascade on delete cascade;
alter table public.message
add constraint message_room_uuid_fkey foreign key (room_uuid) references public.room (uuid) on update cascade on delete cascade;
alter table public.message
add constraint message_reply_to_fkey foreign key (reply_to) references public.message (uuid) on update cascade on delete set null;
alter table public.message
add constraint message_no_self_reply check (reply_to is null or reply_to <> uuid);
alter table public.message
add constraint message_reply_to_unique unique (reply_to);

create or replace function public.check_message_reply() returns trigger as $$
begin
if new.reply_to is not null then
if exists (
select 1 from public.message
where uuid = new.reply_to and reply_to is not null
) then
raise exception 'cannot reply to a reply (multi-level reply is not allowed)';
end if;
end if;
return new;
end;
$$ language plpgsql;

drop trigger if exists message_reply_check on public.message;
create trigger message_reply_check
before insert or update of reply_to on public.message
for each row execute function public.check_message_reply();

insert into public.message (user_uuid, room_uuid, content) values
('00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-100000000001', '大家好,我叫张三'),
Expand Down Expand Up @@ -42,3 +68,8 @@ insert into public.message (user_uuid, room_uuid, content) values
('00000000-0000-0000-0000-000000000004', '00000000-0000-0000-0000-100000000003', '好了,今天就到这里吧');

update public.message set created_at = '2021-01-01 00:00:00' where room_uuid = '00000000-0000-0000-0000-100000000002';

insert into public.message (uuid, user_uuid, room_uuid, content) values
('00000000-0000-0000-0000-000000000101', '00000000-0000-0000-0000-000000000001', '00000000-0000-0000-0000-100000000001', '有谁能听到我说话吗?');
insert into public.message (uuid, user_uuid, room_uuid, content, reply_to) values
('00000000-0000-0000-0000-000000000102', '00000000-0000-0000-0000-000000000002', '00000000-0000-0000-0000-100000000001', '我能听到,怎么啦?', '00000000-0000-0000-0000-000000000101');