diff --git a/db/migrate/20230602120613_create_conversation_with_labels.rb b/db/migrate/20230602120613_create_conversation_with_labels.rb new file mode 100644 index 000000000..fb0855e87 --- /dev/null +++ b/db/migrate/20230602120613_create_conversation_with_labels.rb @@ -0,0 +1,8 @@ +class CreateConversationWithLabels < ActiveRecord::Migration[7.0] + def change + create_view :conversation_with_labels + + add_index :conversation_with_labels, :id, unique: true + add_index :conversation_with_labels, :account_id + end +end diff --git a/db/views/conversation_with_labels_v01.sql b/db/views/conversation_with_labels_v01.sql new file mode 100644 index 000000000..9ec0e5340 --- /dev/null +++ b/db/views/conversation_with_labels_v01.sql @@ -0,0 +1,11 @@ +SELECT + conversations.*, + array_agg(tags.name) as labels_array +FROM + conversations + LEFT JOIN taggings ON taggings.taggable_id = conversations.id + AND taggings.taggable_type = 'Conversation' + AND taggings.context = 'labels' + LEFT JOIN tags ON tags.id = taggings.tag_id +GROUP BY + conversations.id; \ No newline at end of file