From 9624bc33ad97232b5bdc4411750f3caeff2a22b0 Mon Sep 17 00:00:00 2001 From: dal Date: Tue, 28 Jan 2025 11:21:51 -0700 Subject: [PATCH] feat: Add file junction table and update database schema - Created `messages_to_files` junction table to link messages with dashboard and metric files - Added foreign key constraints and indexes for efficient file-message relationships - Updated Diesel schema to include new `messages_to_files`, `dashboard_files`, and `metric_files` tables - Removed unnecessary timestamp triggers from migration files --- .../2025-01-27-235752_metric_files/down.sql | 3 -- .../2025-01-27-235752_metric_files/up.sql | 6 --- .../down.sql | 2 - .../2025-01-27-235858_dashboard_files/up.sql | 6 --- .../down.sql | 13 +++++ .../up.sql | 23 +++++++++ .../down.sql | 1 + .../up.sql | 1 + api/src/database/schema.rs | 48 +++++++++++++++++++ 9 files changed, 86 insertions(+), 17 deletions(-) create mode 100644 api/migrations/2025-01-28-174921_adjust_messages_table/down.sql create mode 100644 api/migrations/2025-01-28-174921_adjust_messages_table/up.sql diff --git a/api/migrations/2025-01-27-235752_metric_files/down.sql b/api/migrations/2025-01-27-235752_metric_files/down.sql index 6ee033ab0..1ea42075f 100644 --- a/api/migrations/2025-01-27-235752_metric_files/down.sql +++ b/api/migrations/2025-01-27-235752_metric_files/down.sql @@ -1,7 +1,4 @@ -- This file should undo anything in `up.sql` --- Drop trigger -DROP TRIGGER IF EXISTS set_timestamp ON metric_files; - -- Drop indexes DROP INDEX IF EXISTS metric_files_deleted_at_idx; DROP INDEX IF EXISTS metric_files_created_by_idx; diff --git a/api/migrations/2025-01-27-235752_metric_files/up.sql b/api/migrations/2025-01-27-235752_metric_files/up.sql index b55f01b44..c7f776cf3 100644 --- a/api/migrations/2025-01-27-235752_metric_files/up.sql +++ b/api/migrations/2025-01-27-235752_metric_files/up.sql @@ -19,9 +19,3 @@ CREATE TABLE metric_files ( CREATE INDEX metric_files_organization_id_idx ON metric_files(organization_id); CREATE INDEX metric_files_created_by_idx ON metric_files(created_by); CREATE INDEX metric_files_deleted_at_idx ON metric_files(deleted_at); - --- Add trigger to update updated_at timestamp -CREATE TRIGGER set_timestamp - BEFORE UPDATE ON metric_files - FOR EACH ROW - EXECUTE PROCEDURE trigger_set_timestamp(); diff --git a/api/migrations/2025-01-27-235858_dashboard_files/down.sql b/api/migrations/2025-01-27-235858_dashboard_files/down.sql index 3af8a2af4..68b663120 100644 --- a/api/migrations/2025-01-27-235858_dashboard_files/down.sql +++ b/api/migrations/2025-01-27-235858_dashboard_files/down.sql @@ -1,6 +1,4 @@ -- This file should undo anything in `up.sql` --- Drop trigger first -DROP TRIGGER IF EXISTS set_timestamp ON dashboard_files; -- Drop indexes DROP INDEX IF EXISTS dashboard_files_deleted_at_idx; diff --git a/api/migrations/2025-01-27-235858_dashboard_files/up.sql b/api/migrations/2025-01-27-235858_dashboard_files/up.sql index 1612abd75..7449518c6 100644 --- a/api/migrations/2025-01-27-235858_dashboard_files/up.sql +++ b/api/migrations/2025-01-27-235858_dashboard_files/up.sql @@ -16,9 +16,3 @@ CREATE TABLE dashboard_files ( CREATE INDEX dashboard_files_organization_id_idx ON dashboard_files(organization_id); CREATE INDEX dashboard_files_created_by_idx ON dashboard_files(created_by); CREATE INDEX dashboard_files_deleted_at_idx ON dashboard_files(deleted_at); - --- Add trigger to update updated_at timestamp -CREATE TRIGGER set_timestamp - BEFORE UPDATE ON dashboard_files - FOR EACH ROW - EXECUTE PROCEDURE trigger_set_timestamp(); diff --git a/api/migrations/2025-01-28-164827_file_junction_table/down.sql b/api/migrations/2025-01-28-164827_file_junction_table/down.sql index d9a93fe9a..9a7708a2b 100644 --- a/api/migrations/2025-01-28-164827_file_junction_table/down.sql +++ b/api/migrations/2025-01-28-164827_file_junction_table/down.sql @@ -1 +1,14 @@ -- This file should undo anything in `up.sql` +-- Drop indexes +DROP INDEX IF EXISTS messages_files_file_id_idx; +DROP INDEX IF EXISTS messages_files_message_id_idx; + +-- Drop foreign key constraints +ALTER TABLE messages_to_files +DROP CONSTRAINT IF EXISTS fk_dashboard_files; + +ALTER TABLE messages_to_files +DROP CONSTRAINT IF EXISTS fk_metric_files; + +-- Drop table +DROP TABLE IF EXISTS messages_to_files; diff --git a/api/migrations/2025-01-28-164827_file_junction_table/up.sql b/api/migrations/2025-01-28-164827_file_junction_table/up.sql index 8b1378917..31a81054a 100644 --- a/api/migrations/2025-01-28-164827_file_junction_table/up.sql +++ b/api/migrations/2025-01-28-164827_file_junction_table/up.sql @@ -1 +1,24 @@ +CREATE TABLE messages_to_files ( + id SERIAL PRIMARY KEY, + message_id UUID NOT NULL REFERENCES messages(id), + file_id UUID NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + UNIQUE(message_id, file_id) +); +-- Index for faster lookups by message_id +CREATE INDEX messages_files_message_id_idx ON messages_to_files(message_id); + +-- Add foreign key constraints for file_id to both metric_files and dashboard_files +ALTER TABLE messages_to_files +ADD CONSTRAINT fk_metric_files +FOREIGN KEY (file_id) +REFERENCES metric_files(id); + +ALTER TABLE messages_to_files +ADD CONSTRAINT fk_dashboard_files +FOREIGN KEY (file_id) +REFERENCES dashboard_files(id); + +-- Index for faster lookups by file_id +CREATE INDEX messages_files_file_id_idx ON messages_to_files(file_id); diff --git a/api/migrations/2025-01-28-174921_adjust_messages_table/down.sql b/api/migrations/2025-01-28-174921_adjust_messages_table/down.sql new file mode 100644 index 000000000..d9a93fe9a --- /dev/null +++ b/api/migrations/2025-01-28-174921_adjust_messages_table/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` diff --git a/api/migrations/2025-01-28-174921_adjust_messages_table/up.sql b/api/migrations/2025-01-28-174921_adjust_messages_table/up.sql new file mode 100644 index 000000000..b37f10d95 --- /dev/null +++ b/api/migrations/2025-01-28-174921_adjust_messages_table/up.sql @@ -0,0 +1 @@ +-- Your SQL goes here diff --git a/api/src/database/schema.rs b/api/src/database/schema.rs index 710a4789a..c211606d7 100644 --- a/api/src/database/schema.rs +++ b/api/src/database/schema.rs @@ -112,6 +112,21 @@ diesel::table! { } } +diesel::table! { + dashboard_files (id) { + id -> Uuid, + name -> Varchar, + file_name -> Varchar, + content -> Jsonb, + filter -> Nullable, + organization_id -> Uuid, + created_by -> Uuid, + created_at -> Timestamptz, + updated_at -> Timestamptz, + deleted_at -> Nullable, + } +} + diesel::table! { dashboard_versions (id) { id -> Uuid, @@ -315,6 +330,33 @@ diesel::table! { } } +diesel::table! { + messages_to_files (id) { + id -> Int4, + message_id -> Uuid, + file_id -> Uuid, + created_at -> Timestamp, + } +} + +diesel::table! { + metric_files (id) { + id -> Uuid, + name -> Varchar, + file_name -> Varchar, + content -> Jsonb, + verification -> Nullable, + evaluation_obj -> Nullable, + evaluation_summary -> Nullable, + evaluation_score -> Nullable, + organization_id -> Uuid, + created_by -> Uuid, + created_at -> Timestamptz, + updated_at -> Timestamptz, + deleted_at -> Nullable, + } +} + diesel::table! { organizations (id) { id -> Uuid, @@ -534,6 +576,9 @@ diesel::joinable!(datasets_to_permission_groups -> permission_groups (permission diesel::joinable!(messages -> datasets (dataset_id)); diesel::joinable!(messages -> threads (thread_id)); diesel::joinable!(messages -> users (sent_by)); +diesel::joinable!(messages_to_files -> dashboard_files (file_id)); +diesel::joinable!(messages_to_files -> messages (message_id)); +diesel::joinable!(messages_to_files -> metric_files (file_id)); diesel::joinable!(permission_groups -> organizations (organization_id)); diesel::joinable!(permission_groups_to_users -> permission_groups (permission_group_id)); diesel::joinable!(permission_groups_to_users -> users (user_id)); @@ -556,6 +601,7 @@ diesel::allow_tables_to_appear_in_same_query!( asset_permissions, collections, collections_to_assets, + dashboard_files, dashboard_versions, dashboards, data_sources, @@ -568,6 +614,8 @@ diesel::allow_tables_to_appear_in_same_query!( datasets_to_permission_groups, entity_relationship, messages, + messages_to_files, + metric_files, organizations, permission_groups, permission_groups_to_identities,