discourse-ai/db/migrate/20241023041242_add_unique_constraint_to_ai_tools.rb
Hoa Nguyen b60926c6e6
FEATURE: Tool name validation (#842)
* FEATURE: Tool name validation

- Add unique index to the name column of the ai_tools table
- correct our tests for AiToolController
- tool_name field which will be used to represent to LLM
- Add tool_name to Tools's presets
- Add duplicate tools validation for AiPersona
- Add unique constraint to the name column of the ai_tools table

* DEV: Validate duplicate tool_name between builin tools and custom tools

* lint

* chore: fix linting

* fix conlict mistakes

* chore: correct icon class

* chore: fix failed specs

* Add max_length to tool_name

* chore: correct the option name

* lintings

* fix lintings
2025-02-07 14:34:47 +11:00

26 lines
671 B
Ruby

# frozen_string_literal: true
class AddUniqueConstraintToAiTools < ActiveRecord::Migration[7.1]
def up
# We need to remove duplicates before adding the unique constraint
execute <<~SQL
WITH duplicates AS (
SELECT name, COUNT(*) as count, MIN(id) as keeper_id
FROM ai_tools
GROUP BY name
HAVING COUNT(*) > 1
)
UPDATE ai_tools AS p
SET name = CONCAT(p.name, p.id)
FROM duplicates d
WHERE p.name = d.name
AND p.id != d.keeper_id;
SQL
add_index :ai_personas, :name, unique: true, if_not_exists: true
end
def down
remove_index :ai_personas, :name, if_exists: true
end
end