Files
leadchat/db/migrate/20250116061033_convert_document_to_polymorphic_association.rb
Pranav 1528473dd0 fix(ce): Check the edition before running the data migration (#10728)
Fixes #10721

This PR addresses an issue where the data migration in the CE edition
was failing due to missing models. The change ensures that the migration
process runs only when the deployment is using the EE image.
2025-01-21 10:00:37 +05:30

36 lines
1.3 KiB
Ruby

class ConvertDocumentToPolymorphicAssociation < ActiveRecord::Migration[7.0]
def up
add_column :captain_assistant_responses, :documentable_type, :string
# rubocop:disable Rails/SkipsModelValidations
if ChatwootApp.enterprise?
Captain::AssistantResponse
.where
.not(document_id: nil)
.update_all(documentable_type: 'Captain::Document')
end
# rubocop:enable Rails/SkipsModelValidations
remove_index :captain_assistant_responses, :document_id if index_exists?(
:captain_assistant_responses, :document_id
)
rename_column :captain_assistant_responses, :document_id, :documentable_id
add_index :captain_assistant_responses, [:documentable_id, :documentable_type],
name: 'idx_cap_asst_resp_on_documentable'
end
def down
if index_exists?(
:captain_assistant_responses, [:documentable_id, :documentable_type], name: 'idx_cap_asst_resp_on_documentable'
)
remove_index :captain_assistant_responses, name: 'idx_cap_asst_resp_on_documentable'
end
rename_column :captain_assistant_responses, :documentable_id, :document_id
remove_column :captain_assistant_responses, :documentable_type
add_index :captain_assistant_responses, :document_id unless index_exists?(
:captain_assistant_responses, :document_id
)
end
end