feat: create database tables and queries for notifications (#13536)

This commit is contained in:
Danny Kopping
2024-06-28 09:21:25 +00:00
committed by GitHub
parent 4213560b7a
commit 0a221e8d5b
17 changed files with 999 additions and 0 deletions
@@ -0,0 +1,4 @@
DROP TABLE IF EXISTS notification_messages;
DROP TABLE IF EXISTS notification_templates;
DROP TYPE IF EXISTS notification_method;
DROP TYPE IF EXISTS notification_message_status;
@@ -0,0 +1,65 @@
CREATE TYPE notification_message_status AS ENUM (
'pending',
'leased',
'sent',
'permanent_failure',
'temporary_failure',
'unknown'
);
CREATE TYPE notification_method AS ENUM (
'smtp',
'webhook'
);
CREATE TABLE notification_templates
(
id uuid NOT NULL,
name text NOT NULL,
title_template text NOT NULL,
body_template text NOT NULL,
actions jsonb,
"group" text,
PRIMARY KEY (id),
UNIQUE (name)
);
COMMENT ON TABLE notification_templates IS 'Templates from which to create notification messages.';
CREATE TABLE notification_messages
(
id uuid NOT NULL,
notification_template_id uuid NOT NULL,
user_id uuid NOT NULL,
method notification_method NOT NULL,
status notification_message_status NOT NULL DEFAULT 'pending'::notification_message_status,
status_reason text,
created_by text NOT NULL,
payload jsonb NOT NULL,
attempt_count int DEFAULT 0,
targets uuid[],
created_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp with time zone,
leased_until timestamp with time zone,
next_retry_after timestamp with time zone,
PRIMARY KEY (id),
FOREIGN KEY (notification_template_id) REFERENCES notification_templates (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
);
CREATE INDEX idx_notification_messages_status ON notification_messages (status);
-- TODO: autogenerate constants which reference the UUIDs
INSERT INTO notification_templates (id, name, title_template, body_template, "group", actions)
VALUES ('f517da0b-cdc9-410f-ab89-a86107c420ed', 'Workspace Deleted', E'Workspace "{{.Labels.name}}" deleted',
E'Hi {{.UserName}}\n\nYour workspace **{{.Labels.name}}** was deleted.\nThe specified reason was "**{{.Labels.reason}}**".',
'Workspace Events', '[
{
"label": "View workspaces",
"url": "{{ base_url }}/workspaces"
},
{
"label": "View templates",
"url": "{{ base_url }}/templates"
}
]'::jsonb);
@@ -0,0 +1,21 @@
DO
$$
DECLARE
template text;
BEGIN
SELECT 'You successfully did {{.thing}}!' INTO template;
INSERT INTO notification_templates (id, name, title_template, body_template, "group")
VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'A', template, template, 'Group 1'),
('b0eebc99-9c0b-4ef8-bb6d-6bb9bd380a12', 'B', template, template, 'Group 1'),
('c0eebc99-9c0b-4ef8-bb6d-6bb9bd380a13', 'C', template, template, 'Group 2');
INSERT INTO users(id, email, username, hashed_password, created_at, updated_at, status, rbac_roles, deleted)
VALUES ('fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'githubuser@coder.com', 'githubuser', '\x', '2022-11-02 13:05:21.445455+02', '2022-11-02 13:05:21.445455+02', 'active', '{}', false) ON CONFLICT DO NOTHING;
INSERT INTO notification_messages (id, notification_template_id, user_id, method, created_by, payload)
VALUES (
gen_random_uuid(), 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'fc1511ef-4fcf-4a3b-98a1-8df64160e35a', 'smtp'::notification_method, 'test', '{}'
);
END
$$;