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
This commit is contained in:
dal 2025-01-28 11:21:51 -07:00
parent f1879dc15c
commit 9624bc33ad
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
9 changed files with 86 additions and 17 deletions

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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();

View File

@ -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;

View File

@ -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);

View File

@ -0,0 +1 @@
-- This file should undo anything in `up.sql`

View File

@ -0,0 +1 @@
-- Your SQL goes here

View File

@ -112,6 +112,21 @@ diesel::table! {
}
}
diesel::table! {
dashboard_files (id) {
id -> Uuid,
name -> Varchar,
file_name -> Varchar,
content -> Jsonb,
filter -> Nullable<Varchar>,
organization_id -> Uuid,
created_by -> Uuid,
created_at -> Timestamptz,
updated_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
}
}
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<Bool>,
evaluation_obj -> Nullable<Jsonb>,
evaluation_summary -> Nullable<Text>,
evaluation_score -> Nullable<Float8>,
organization_id -> Uuid,
created_by -> Uuid,
created_at -> Timestamptz,
updated_at -> Timestamptz,
deleted_at -> Nullable<Timestamptz>,
}
}
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,