feat: migrations for document auto-sync [AI-141] (#14041)

# Pull Request Template

## Description

Add migrations for document auto-sync

Fixes # (issue)

## Type of change

- [x] New feature (non-breaking change which adds functionality)

## How Has This Been Tested?
locally

## Checklist:

- [x] My code follows the style guidelines of this project
- [x] I have performed a self-review of my code
- [x] I have commented on my code, particularly in hard-to-understand
areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream
modules
This commit is contained in:
Aakash Bakhle
2026-04-15 17:56:10 +05:30
committed by GitHub
parent b96bf41234
commit 5264de24b0
8 changed files with 85 additions and 11 deletions

View File

@@ -0,0 +1,5 @@
class AddEditedToCaptainAssistantResponses < ActiveRecord::Migration[7.0]
def change
add_column :captain_assistant_responses, :edited, :boolean, default: false, null: false
end
end

View File

@@ -0,0 +1,11 @@
class AddSyncColumnsToCaptainDocuments < ActiveRecord::Migration[7.0]
def change
change_table :captain_documents, bulk: true do |t|
t.integer :sync_status
t.datetime :last_synced_at
t.datetime :last_sync_attempted_at
end
add_index :captain_documents, [:account_id, :sync_status]
end
end

View File

@@ -0,0 +1,20 @@
class BackfillEditedOnCaptainAssistantResponses < ActiveRecord::Migration[7.0]
def up
return unless ChatwootApp.enterprise?
# rubocop:disable Rails/SkipsModelValidations
# NOTE: Since there is no way of knowing currently which FAQs were edited by a human
# we use a heuristic based on time passed between created_at and updated_at.
# 15 days is arbitrary but seems reasonable for a user to go back and edit an FAQ.
Captain::AssistantResponse
.where('updated_at - created_at > make_interval(days := ?)', 15)
.in_batches(of: 1000) do |batch|
batch.update_all(edited: true)
end
# rubocop:enable Rails/SkipsModelValidations
end
def down
# no-op: rolling back migration of edited column will drop the edited column entirely
end
end