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

View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.1].define(version: 2026_04_09_091202) do
ActiveRecord::Schema[7.1].define(version: 2026_04_10_092753) do
# These extensions should be enabled to support this database
enable_extension "pg_stat_statements"
enable_extension "pg_trgm"
@@ -329,6 +329,7 @@ ActiveRecord::Schema[7.1].define(version: 2026_04_09_091202) do
t.datetime "updated_at", null: false
t.integer "status", default: 1, null: false
t.string "documentable_type"
t.boolean "edited", default: false, null: false
t.index ["account_id"], name: "index_captain_assistant_responses_on_account_id"
t.index ["assistant_id"], name: "index_captain_assistant_responses_on_assistant_id"
t.index ["documentable_id", "documentable_type"], name: "idx_cap_asst_resp_on_documentable"
@@ -377,10 +378,14 @@ ActiveRecord::Schema[7.1].define(version: 2026_04_09_091202) do
t.datetime "updated_at", null: false
t.integer "status", default: 0, null: false
t.jsonb "metadata", default: {}
t.integer "sync_status"
t.datetime "last_synced_at"
t.datetime "last_sync_attempted_at"
t.index ["account_id"], name: "index_captain_documents_on_account_id"
t.index ["assistant_id", "external_link"], name: "index_captain_documents_on_assistant_id_and_external_link", unique: true
t.index ["assistant_id"], name: "index_captain_documents_on_assistant_id"
t.index ["status"], name: "index_captain_documents_on_status"
t.index ["account_id", "sync_status"], name: "index_captain_documents_on_account_id_and_sync_status"
end
create_table "captain_inboxes", force: :cascade do |t|