From a2542425eddd2ccddcea278ffd76eb3447a921e0 Mon Sep 17 00:00:00 2001 From: akanksh5 Date: Tue, 19 Mar 2024 21:47:06 -0700 Subject: [PATCH 1/3] Added the insert statements for the defined db --- internal/db/sqlite/queries.sql | 71 ++++++---------------------------- 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/internal/db/sqlite/queries.sql b/internal/db/sqlite/queries.sql index f4f5a49..6cff2f4 100644 --- a/internal/db/sqlite/queries.sql +++ b/internal/db/sqlite/queries.sql @@ -1,68 +1,21 @@ -- name: CreateResource :exec -INSERT INTO resources (id, title, content_md, image_url, resource_type, resource_list_id, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); +INSERT INTO resources (uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); --- name: CreateResourceList :exec -INSERT INTO resource_lists (title, created_at, updated_at) VALUES (?, ?, ?); +-- name: CreateEvent :exec +INSERT INTO event (uuid, location, start_at, end_at, is_all_day, host, visibility) VALUES (?, ?, ?, ?, ?, ?, ?); + +-- name: CreatePerson :exec +INSERT INTO person (uuid, name, preferred_pronoun) VALUES (?, ?, ?); --- name: CreateResourceReference :exec -INSERT INTO resource_references (resource_id, resource_list_id, created_at, updated_at) VALUES (?, ?, ?, ?); +-- name: CreateResourceGroupMapping :exec +INSERT INTO resource_id_group_id_mapping (resource_uuid, group_uuid, type, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); --- name: GetResourceList :many -SELECT rr.resource_id, rr.resource_list_id, rr.created_at, rr.updated_at -FROM resource_references rr -JOIN resources r ON rr.resource_id = r.id -JOIN resource_lists rl ON rr.resource_list_id = rl.id -WHERE rl.id = ? -ORDER BY rr.index_in_list ASC; +-- name: CreateGroupResourceMapping :exec +INSERT INTO group_id_resource_list_mapping ( group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); --- name: AddResource :exec -INSERT INTO resource_references (resource_id, resource_list_id, index_in_list, created_at, updated_at) VALUES (?, ?, ?, ?, ?); +-- name: CreateAnnouncement :exec +INSERT INTO announcements (uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id) VALUES (?, ?, ?, ?, ?, ?, ?); -- name: DeleteResource :exec DELETE FROM resources WHERE id = ?; --- name: CreateEvent :exec -INSERT INTO events (id, location, start_at, duration_ms, is_all_day, host, visibility) VALUES (?, ?, ?, ?, ?, ?, ?); - --- name: GetEvent :one -SELECT - r.id, - r.title, - r.content_md, - r.image_url, - r.resource_type, - r.resource_list_id, - r.created_at, - r.updated_at, - e.location, - e.start_at, - e.duration_ms, - e.is_all_day, - e.host, - e.visibility -FROM resources r -INNER JOIN events e ON r.id = e.id -WHERE r.id = ?; - --- name: CreateAnnouncement :exec -INSERT INTO announcements (id, event_list_id, approved_by_list_id, visibility, announce_at, discord_channel_id, discord_message_id) VALUES (?, ?, ?, ?, ?, ?, ?); - --- name: GetAnnouncement :one -SELECT - r.id, - r.title, - r.content_md, - r.image_url, - r.resource_type, - r.resource_list_id, - r.created_at, - r.updated_at, - a.event_list_id, - a.approved_by_list_id, - a.visibility, - a.announce_at, - a.discord_channel_id, - a.discord_message_id -FROM resources r -INNER JOIN announcements a ON r.id = a.id -WHERE r.id = ?; From 068daee7ee634a61535bda1edd1096ce2430ca27 Mon Sep 17 00:00:00 2001 From: akanksh5 Date: Fri, 22 Mar 2024 23:38:33 -0700 Subject: [PATCH 2/3] Added the select queries in queries.sql --- internal/db/sqlite/queries.sql | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/internal/db/sqlite/queries.sql b/internal/db/sqlite/queries.sql index 6cff2f4..15cd74e 100644 --- a/internal/db/sqlite/queries.sql +++ b/internal/db/sqlite/queries.sql @@ -1,5 +1,5 @@ -- name: CreateResource :exec -INSERT INTO resources (uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); +INSERT INTO resource (uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); -- name: CreateEvent :exec INSERT INTO event (uuid, location, start_at, end_at, is_all_day, host, visibility) VALUES (?, ?, ?, ?, ?, ?, ?); @@ -11,11 +11,28 @@ INSERT INTO person (uuid, name, preferred_pronoun) VALUES (?, ?, ?); INSERT INTO resource_id_group_id_mapping (resource_uuid, group_uuid, type, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); -- name: CreateGroupResourceMapping :exec -INSERT INTO group_id_resource_list_mapping ( group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); +INSERT INTO group_id_resource_list_mapping (group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); -- name: CreateAnnouncement :exec -INSERT INTO announcements (uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id) VALUES (?, ?, ?, ?, ?, ?, ?); +INSERT INTO announcement (uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id) VALUES (?, ?, ?, ?, ?, ?, ?); -- name: DeleteResource :exec -DELETE FROM resources WHERE id = ?; +DELETE FROM resource WHERE id = ?; +-- name: GetResource :exec +SELECT uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at from resource where uuid = ?; + +-- name: GetEvent :exec +SELECT uuid, location, start_at, end_at, is_all_day, host, visibility from event where uuid = ?; + +-- name: GetPerson :exec +SELECT uuid, name, preferred_pronoun from person where uuid = ?; + +-- name: GetResourceGroupMapping :exec +SELECT resource_uuid, group_uuid, type, created_at, updated_at, deleted_at from resource_id_group_id_mapping where resource_uuid = ?; + +-- name: GetGroupResourceMapping :exec +SELECT group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at from group_id_resource_list_mapping where group_uuid = ?; + +-- name: GetAnnouncement :exec +SELECT uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id from announcements where uuid = ?; \ No newline at end of file From 41923eebe7526b9b6eb0be477a1413d5eabc101e Mon Sep 17 00:00:00 2001 From: Ethan Davidson <31261035+EthanThatOneKid@users.noreply.github.com> Date: Sat, 23 Mar 2024 16:38:50 -0700 Subject: [PATCH 3/3] fmt sql code Formatted with vscode extension `inferrinizzard.prettier-sql-vscode`. --- .vscode/settings.json | 3 + internal/db/sqlite/queries.sql | 146 ++++++++++++++++++++++++++++++--- internal/db/sqlite/schema.sql | 115 +++++++++++++------------- 3 files changed, 194 insertions(+), 70 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 0f409aa..ac51e4e 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,8 @@ "editor.defaultFormatter": "esbenp.prettier-vscode", "[go]": { "editor.defaultFormatter": "golang.go" + }, + "[sql]": { + "editor.defaultFormatter": "inferrinizzard.prettier-sql-vscode" } } diff --git a/internal/db/sqlite/queries.sql b/internal/db/sqlite/queries.sql index 15cd74e..812ffbb 100644 --- a/internal/db/sqlite/queries.sql +++ b/internal/db/sqlite/queries.sql @@ -1,38 +1,158 @@ -- name: CreateResource :exec -INSERT INTO resource (uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?); +INSERT INTO + resource ( + uuid, + title, + content_md, + image_url, + resource_type, + created_at, + updated_at, + deleted_at + ) +VALUES + (?, ?, ?, ?, ?, ?, ?, ?); -- name: CreateEvent :exec -INSERT INTO event (uuid, location, start_at, end_at, is_all_day, host, visibility) VALUES (?, ?, ?, ?, ?, ?, ?); +INSERT INTO + event ( + uuid, + location, + start_at, + end_at, + is_all_day, + host, + visibility + ) +VALUES + (?, ?, ?, ?, ?, ?, ?); -- name: CreatePerson :exec -INSERT INTO person (uuid, name, preferred_pronoun) VALUES (?, ?, ?); +INSERT INTO + person (uuid, name, preferred_pronoun) +VALUES + (?, ?, ?); -- name: CreateResourceGroupMapping :exec -INSERT INTO resource_id_group_id_mapping (resource_uuid, group_uuid, type, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); +INSERT INTO + resource_id_group_id_mapping ( + resource_uuid, + group_uuid, + type, + created_at, + updated_at, + deleted_at + ) +VALUES + (?, ?, ?, ?, ?, ?); -- name: CreateGroupResourceMapping :exec -INSERT INTO group_id_resource_list_mapping (group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at) VALUES (?, ?, ?, ?, ?, ?); +INSERT INTO + group_id_resource_list_mapping ( + group_uuid, + resource_uuid, + index_in_list, + created_at, + updated_at, + deleted_at + ) +VALUES + (?, ?, ?, ?, ?, ?); -- name: CreateAnnouncement :exec -INSERT INTO announcement (uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id) VALUES (?, ?, ?, ?, ?, ?, ?); +INSERT INTO + announcement ( + uuid, + event_groups_group_uuid, + approved_by_list_uuid, + visibility, + announce_at, + discord_channel_id, + discord_message_id + ) +VALUES + (?, ?, ?, ?, ?, ?, ?); -- name: DeleteResource :exec -DELETE FROM resource WHERE id = ?; +DELETE FROM resource +WHERE + id = ?; -- name: GetResource :exec -SELECT uuid, title, content_md, image_url, resource_type, created_at, updated_at,deleted_at from resource where uuid = ?; +SELECT + uuid, + title, + content_md, + image_url, + resource_type, + created_at, + updated_at, + deleted_at +from + resource +where + uuid = ?; -- name: GetEvent :exec -SELECT uuid, location, start_at, end_at, is_all_day, host, visibility from event where uuid = ?; +SELECT + uuid, + location, + start_at, + end_at, + is_all_day, + host, + visibility +from + event +where + uuid = ?; -- name: GetPerson :exec -SELECT uuid, name, preferred_pronoun from person where uuid = ?; +SELECT + uuid, + name, + preferred_pronoun +from + person +where + uuid = ?; -- name: GetResourceGroupMapping :exec -SELECT resource_uuid, group_uuid, type, created_at, updated_at, deleted_at from resource_id_group_id_mapping where resource_uuid = ?; +SELECT + resource_uuid, + group_uuid, + type, + created_at, + updated_at, + deleted_at +from + resource_id_group_id_mapping +where + resource_uuid = ?; -- name: GetGroupResourceMapping :exec -SELECT group_uuid, resource_uuid, index_in_list, created_at, updated_at, deleted_at from group_id_resource_list_mapping where group_uuid = ?; +SELECT + group_uuid, + resource_uuid, + index_in_list, + created_at, + updated_at, + deleted_at +from + group_id_resource_list_mapping +where + group_uuid = ?; -- name: GetAnnouncement :exec -SELECT uuid, event_groups_group_uuid, approved_by_list_uuid, visibility, announce_at, discord_channel_id, discord_message_id from announcements where uuid = ?; \ No newline at end of file +SELECT + uuid, + event_groups_group_uuid, + approved_by_list_uuid, + visibility, + announce_at, + discord_channel_id, + discord_message_id +from + announcements +where + uuid = ?; \ No newline at end of file diff --git a/internal/db/sqlite/schema.sql b/internal/db/sqlite/schema.sql index cf99925..2108afc 100644 --- a/internal/db/sqlite/schema.sql +++ b/internal/db/sqlite/schema.sql @@ -1,64 +1,65 @@ -- Language: sqlite - -- Create the 'resource_mapping' table. -CREATE TABLE IF NOT EXISTS resource_id_group_id_mapping ( - resource_uuid TEXT REFERENCES resource(uuid), - group_uuid TEXT NOT NULL REFERENCES group_resource_list_mapping(uuid), - type TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP DEFAULT NULL, -); +CREATE TABLE + IF NOT EXISTS resource_id_group_id_mapping ( + resource_uuid TEXT REFERENCES resource (uuid), + group_uuid TEXT NOT NULL REFERENCES group_resource_list_mapping (uuid), + type TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP DEFAULT NULL, + ); -CREATE TABLE IF NOT EXISTS group_id_resource_list_mapping ( - group_uuid TEXT, - resource_uuid TEXT NOT NULL REFERENCES resource(uuid), - index_in_list INTEGER NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP DEFAULT NULL, -); +CREATE TABLE + IF NOT EXISTS group_id_resource_list_mapping ( + group_uuid TEXT, + resource_uuid TEXT NOT NULL REFERENCES resource (uuid), + index_in_list INTEGER NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP DEFAULT NULL, + ); -- Create the 'resource' table. -CREATE TABLE IF NOT EXISTS resource ( - uuid TEXT PRIMARY KEY, - title TEXT NOT NULL, - content_md TEXT NOT NULL, - image_url TEXT, - resource_type TEXT NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - deleted_at TIMESTAMP DEFAULT NULL, -); +CREATE TABLE + IF NOT EXISTS resource ( + uuid TEXT PRIMARY KEY, + title TEXT NOT NULL, + content_md TEXT NOT NULL, + image_url TEXT, + resource_type TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + deleted_at TIMESTAMP DEFAULT NULL, + ); -- Create the 'events' table which is a table of event resources. -CREATE TABLE IF NOT EXISTS event ( - uuid TEXT PRIMARY KEY REFERENCES resource(uuid), - location TEXT NOT NULL, - start_at NUMBER NOT NULL, -- Start time in UTC milliseconds. - end_at NUMBER NOT NULL, - is_all_day BOOLEAN NOT NULL, - host TEXT NOT NULL, -- Accepts team ID or plain text. - visibility TEXT NOT NULL, -- Accepts 'public' or 'private'. -) - --- Create the 'person' table which is a table of person resources. -CREATE TABLE IF NOT EXISTS person ( - uuid TEXT REFERENCES resource(uuid), - name TEXT, - preferred_pronoun TEXT -) - --- Create the 'announcement' table which is a table of announcement resources. -CREATE TABLE IF NOT EXISTS announcement ( - uuid TEXT PRIMARY KEY REFERENCES resource(uuid), - event_groups_group_uuid TEXT REFERENCES resource_group_mapping(resource_uuid), - approved_by_list_uuid TEXT REFERENCES group_id_resource_list_mapping(uuid), - visibility TEXT NOT NULL, -- Accepts 'public' or 'private'. - announce_at INTEGER NOT NULL, -- UTC milliseconds. - discord_channel_id TEXT, -- Discord channel ID. - discord_message_id TEXT, -- Discord message ID. If present, the announcement has been posted. - UNIQUE (id) -) - - +CREATE TABLE + IF NOT EXISTS event ( + uuid TEXT PRIMARY KEY REFERENCES resource (uuid), + location TEXT NOT NULL, + start_at NUMBER NOT NULL, -- Start time in UTC milliseconds. + end_at NUMBER NOT NULL, + is_all_day BOOLEAN NOT NULL, + host TEXT NOT NULL, -- Accepts team ID or plain text. + visibility TEXT NOT NULL, -- Accepts 'public' or 'private'. + ) + -- Create the 'person' table which is a table of person resources. +CREATE TABLE + IF NOT EXISTS person ( + uuid TEXT REFERENCES resource (uuid), + name TEXT, + preferred_pronoun TEXT + ) + -- Create the 'announcement' table which is a table of announcement resources. +CREATE TABLE + IF NOT EXISTS announcement ( + uuid TEXT PRIMARY KEY REFERENCES resource (uuid), + event_groups_group_uuid TEXT REFERENCES resource_group_mapping (resource_uuid), + approved_by_list_uuid TEXT REFERENCES group_id_resource_list_mapping (uuid), + visibility TEXT NOT NULL, -- Accepts 'public' or 'private'. + announce_at INTEGER NOT NULL, -- UTC milliseconds. + discord_channel_id TEXT, -- Discord channel ID. + discord_message_id TEXT, -- Discord message ID. If present, the announcement has been posted. + UNIQUE (id) + ) \ No newline at end of file