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 index d9a93fe9a..c4a4c0d80 100644 --- a/api/migrations/2025-01-28-174921_adjust_messages_table/down.sql +++ b/api/migrations/2025-01-28-174921_adjust_messages_table/down.sql @@ -1 +1,11 @@ -- This file should undo anything in `up.sql` +-- Drop indexes +DROP INDEX messages_thread_id_idx; +DROP INDEX messages_created_by_idx; +DROP INDEX messages_created_at_idx; + +-- Drop new messages table +DROP TABLE messages; + +-- Rename deprecated table back to messages +ALTER TABLE messages_deprecated RENAME TO messages; 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 index b37f10d95..06f5ddfcd 100644 --- a/api/migrations/2025-01-28-174921_adjust_messages_table/up.sql +++ b/api/migrations/2025-01-28-174921_adjust_messages_table/up.sql @@ -1 +1,22 @@ -- Your SQL goes here +-- Rename existing messages table to messages_deprecated +ALTER TABLE messages RENAME TO messages_deprecated; + +-- Create new messages table with updated schema +CREATE TABLE messages ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + request TEXT NOT NULL, + response JSONB NOT NULL, + thread_id UUID NOT NULL REFERENCES threads(id), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + deleted_at TIMESTAMP WITH TIME ZONE, + created_by UUID NOT NULL REFERENCES users(id) +); + +-- Create indexes for common query patterns +CREATE INDEX messages_thread_id_idx ON messages(thread_id); +CREATE INDEX messages_created_by_idx ON messages(created_by); +CREATE INDEX messages_created_at_idx ON messages(created_at); + + diff --git a/api/migrations/2025-01-28-190057_adjust_threads_table/down.sql b/api/migrations/2025-01-28-190057_adjust_threads_table/down.sql new file mode 100644 index 000000000..c68552d1b --- /dev/null +++ b/api/migrations/2025-01-28-190057_adjust_threads_table/down.sql @@ -0,0 +1,11 @@ +-- This file should undo anything in `up.sql` +-- Drop indexes +DROP INDEX threads_organization_id_idx; +DROP INDEX threads_created_by_idx; +DROP INDEX threads_created_at_idx; + +-- Drop new threads table +DROP TABLE threads; + +-- Rename deprecated table back to threads +ALTER TABLE threads_deprecated RENAME TO threads; diff --git a/api/migrations/2025-01-28-190057_adjust_threads_table/up.sql b/api/migrations/2025-01-28-190057_adjust_threads_table/up.sql new file mode 100644 index 000000000..7ef5d0262 --- /dev/null +++ b/api/migrations/2025-01-28-190057_adjust_threads_table/up.sql @@ -0,0 +1,19 @@ +-- Your SQL goes here +-- Rename existing threads table to threads_deprecated +ALTER TABLE threads RENAME TO threads_deprecated; + +-- Create new threads table with updated schema +CREATE TABLE threads ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + title TEXT NOT NULL, + organization_id UUID NOT NULL REFERENCES organizations(id), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + deleted_at TIMESTAMP WITH TIME ZONE, + created_by UUID NOT NULL REFERENCES users(id) +); + +-- Create indexes for common query patterns +CREATE INDEX threads_organization_id_idx ON threads(organization_id); +CREATE INDEX threads_created_by_idx ON threads(created_by); +CREATE INDEX threads_created_at_idx ON threads(created_at); diff --git a/api/src/database/schema.rs b/api/src/database/schema.rs index c211606d7..fa6be816e 100644 --- a/api/src/database/schema.rs +++ b/api/src/database/schema.rs @@ -299,12 +299,25 @@ diesel::table! { } } +diesel::table! { + messages (id) { + id -> Uuid, + request -> Text, + response -> Jsonb, + thread_id -> Uuid, + created_at -> Timestamptz, + updated_at -> Timestamptz, + deleted_at -> Nullable, + created_by -> Uuid, + } +} + diesel::table! { use diesel::sql_types::*; use super::sql_types::MessageFeedbackEnum; use super::sql_types::VerificationEnum; - messages (id) { + messages_deprecated (id) { id -> Uuid, thread_id -> Uuid, sent_by -> Uuid, @@ -478,6 +491,18 @@ diesel::table! { diesel::table! { threads (id) { + id -> Uuid, + title -> Text, + organization_id -> Uuid, + created_at -> Timestamptz, + updated_at -> Timestamptz, + deleted_at -> Nullable, + created_by -> Uuid, + } +} + +diesel::table! { + threads_deprecated (id) { id -> Uuid, created_by -> Uuid, updated_by -> Uuid, @@ -573,11 +598,13 @@ diesel::joinable!(datasets_to_dataset_groups -> dataset_groups (dataset_group_id diesel::joinable!(datasets_to_dataset_groups -> datasets (dataset_id)); diesel::joinable!(datasets_to_permission_groups -> datasets (dataset_id)); diesel::joinable!(datasets_to_permission_groups -> permission_groups (permission_group_id)); -diesel::joinable!(messages -> datasets (dataset_id)); -diesel::joinable!(messages -> threads (thread_id)); -diesel::joinable!(messages -> users (sent_by)); +diesel::joinable!(messages -> threads_deprecated (thread_id)); +diesel::joinable!(messages -> users (created_by)); +diesel::joinable!(messages_deprecated -> datasets (dataset_id)); +diesel::joinable!(messages_deprecated -> threads_deprecated (thread_id)); +diesel::joinable!(messages_deprecated -> 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 -> messages_deprecated (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)); @@ -590,8 +617,10 @@ diesel::joinable!(terms -> organizations (organization_id)); diesel::joinable!(terms_to_datasets -> datasets (dataset_id)); diesel::joinable!(terms_to_datasets -> terms (term_id)); diesel::joinable!(threads -> organizations (organization_id)); +diesel::joinable!(threads -> users (created_by)); +diesel::joinable!(threads_deprecated -> organizations (organization_id)); diesel::joinable!(threads_to_dashboards -> dashboards (dashboard_id)); -diesel::joinable!(threads_to_dashboards -> threads (thread_id)); +diesel::joinable!(threads_to_dashboards -> threads_deprecated (thread_id)); diesel::joinable!(threads_to_dashboards -> users (added_by)); diesel::joinable!(user_favorites -> users (user_id)); diesel::joinable!(users_to_organizations -> organizations (organization_id)); @@ -614,6 +643,7 @@ diesel::allow_tables_to_appear_in_same_query!( datasets_to_permission_groups, entity_relationship, messages, + messages_deprecated, messages_to_files, metric_files, organizations, @@ -626,6 +656,7 @@ diesel::allow_tables_to_appear_in_same_query!( terms, terms_to_datasets, threads, + threads_deprecated, threads_to_dashboards, user_favorites, users,