From 4f7043d1d720ae5d0ab9791a8c2ea32460cbd6cb Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 25 Oct 2024 16:58:39 +0300 Subject: [PATCH] DEV: Fix mismatched column types The primary key is usually a bigint column, but the foreign key columns are usually of integer type. This can lead to issues when joining these columns due to mismatched types and different value ranges. This was using a temporary plugin / test API to make tests pass, but it is safe to alter "ai_document_fragment_embeddings" and "rag_document_fragments" tables because they usually have less than 1M rows and migration is going to be fast. Depending on the size of the community, "classification_results" table may have more than 1M rows and the migration will lock the table for a longer time. However, classification runs in background jobs and they will be automatically retried if they fail due to the lock, which makes it acceptable. --- db/migrate/20241025135522_alter_ai_ids_to_bigint.rb | 13 +++++++++++++ spec/plugin_helper.rb | 12 +----------- 2 files changed, 14 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20241025135522_alter_ai_ids_to_bigint.rb diff --git a/db/migrate/20241025135522_alter_ai_ids_to_bigint.rb b/db/migrate/20241025135522_alter_ai_ids_to_bigint.rb new file mode 100644 index 000000000..b94d0b4d3 --- /dev/null +++ b/db/migrate/20241025135522_alter_ai_ids_to_bigint.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AlterAiIdsToBigint < ActiveRecord::Migration[7.1] + def up + change_column :ai_document_fragment_embeddings, :rag_document_fragment_id, :bigint + change_column :classification_results, :target_id, :bigint + change_column :rag_document_fragments, :target_id, :bigint + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index 0a2038342..2c7d9d7aa 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -17,14 +17,4 @@ def assign_fake_provider_to(setting_name) end end -RSpec.configure do |config| - config.include DiscourseAi::ChatBotHelper - - config.before(:suite) do - if defined?(migrate_column_to_bigint) - migrate_column_to_bigint(RagDocumentFragment, :target_id) - migrate_column_to_bigint("ai_document_fragment_embeddings", "rag_document_fragment_id") - migrate_column_to_bigint(ClassificationResult, :target_id) - end - end -end +RSpec.configure { |config| config.include DiscourseAi::ChatBotHelper }