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

@@ -5,6 +5,7 @@
# id :bigint not null, primary key
# answer :text not null
# documentable_type :string
# edited :boolean default(FALSE), not null
# embedding :vector(1536)
# question :string not null
# status :integer default("approved"), not null
@@ -35,6 +36,7 @@ class Captain::AssistantResponse < ApplicationRecord
before_validation :ensure_account
before_validation :ensure_status
before_validation :mark_as_edited, on: :update
after_commit :update_response_embedding
scope :ordered, -> { order(created_at: :desc) }
@@ -55,6 +57,10 @@ class Captain::AssistantResponse < ApplicationRecord
self.status ||= :approved
end
def mark_as_edited
self.edited = true if question_changed? || answer_changed?
end
def ensure_account
self.account = assistant&.account
end

View File

@@ -2,20 +2,24 @@
#
# Table name: captain_documents
#
# id :bigint not null, primary key
# content :text
# external_link :string not null
# metadata :jsonb
# name :string
# status :integer default("in_progress"), not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# assistant_id :bigint not null
# id :bigint not null, primary key
# content :text
# external_link :string not null
# last_sync_attempted_at :datetime
# last_synced_at :datetime
# metadata :jsonb
# name :string
# status :integer default("in_progress"), not null
# sync_status :integer
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# assistant_id :bigint not null
#
# Indexes
#
# index_captain_documents_on_account_id (account_id)
# index_captain_documents_on_account_id_and_sync_status (account_id,sync_status)
# index_captain_documents_on_assistant_id (assistant_id)
# index_captain_documents_on_assistant_id_and_external_link (assistant_id,external_link) UNIQUE
# index_captain_documents_on_status (status)
@@ -44,6 +48,8 @@ class Captain::Document < ApplicationRecord
available: 1
}
enum :sync_status, { syncing: 0, synced: 1, failed: 2 }, prefix: :sync
before_create :ensure_within_plan_limit
after_create_commit :enqueue_crawl_job
after_create_commit :update_document_usage
@@ -68,6 +74,22 @@ class Captain::Document < ApplicationRecord
pdf_file.blob.byte_size if pdf_file.attached?
end
def content_fingerprint
metadata&.dig('content_fingerprint')
end
def content_fingerprint=(value)
self.metadata = (metadata || {}).merge('content_fingerprint' => value)
end
def last_sync_error_code
metadata&.dig('last_sync_error_code')
end
def last_sync_error_code=(value)
self.metadata = (metadata || {}).merge('last_sync_error_code' => value)
end
def openai_file_id
metadata&.dig('openai_file_id')
end