Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 09f081d

Browse files
committed
DEV: Add unique constraint to the name column of the ai_tools table
1 parent daad959 commit 09f081d

File tree

4 files changed

+27
-16
lines changed

4 files changed

+27
-16
lines changed

app/models/ai_tool.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class AiTool < ActiveRecord::Base
4-
validates :name, presence: true, length: { maximum: 100 }
4+
validates :name, presence: true, length: { maximum: 100 }, uniqueness: true
55
validates :tool_name, presence: true, length: { maximum: 100 }
66
validates :description, presence: true, length: { maximum: 1000 }
77
validates :summary, presence: true, length: { maximum: 255 }

config/locales/client.en.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ en:
211211
tools:
212212
back: "Back"
213213
short_title: "Tools"
214-
new: "New tool"
214+
new: "New Tool"
215215
name: "Name"
216216
name_help: "Name will show up in the Discourse UI and is the short identifier you will use to find the tool in various settings, it should be distinct (it is required)"
217217
tool_name: "Tool Name"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
class AddUniqueConstraintToAiTools < ActiveRecord::Migration[7.1]
3+
def up
4+
# We need to remove duplicates before adding the unique constraint
5+
execute <<~SQL
6+
WITH duplicates AS (
7+
SELECT name, COUNT(*) as count, MIN(id) as keeper_id
8+
FROM ai_tools
9+
GROUP BY name
10+
HAVING COUNT(*) > 1
11+
)
12+
UPDATE ai_tools AS p
13+
SET name = CONCAT(p.name, p.id)
14+
FROM duplicates d
15+
WHERE p.name = d.name
16+
AND p.id != d.keeper_id;
17+
SQL
18+
19+
add_index :ai_personas, :name, unique: true, if_not_exists: true
20+
end
21+
22+
def down
23+
remove_index :ai_personas, :name, if_exists: true
24+
end
25+
end

spec/requests/admin/ai_tools_controller_spec.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,6 @@
138138
end
139139
end
140140

141-
describe "GET #check_name" do
142-
it "call ToolNameChecker and returns the result" do
143-
allow(DiscourseAi::ToolNameChecker).to receive(:new).and_return(
144-
instance_double(DiscourseAi::ToolNameChecker, check: { available: true }),
145-
)
146-
147-
get "/admin/plugins/discourse-ai/ai-tools/check-name.json?tool_name=TestTool"
148-
149-
expect(DiscourseAi::ToolNameChecker).to have_received(:new).with("TestTool")
150-
expect(response).to be_successful
151-
expect(response.parsed_body).to eq("available" => true)
152-
end
153-
end
154-
155141
describe "#test" do
156142
it "runs an existing tool and returns the result" do
157143
post "/admin/plugins/discourse-ai/ai-tools/test.json",

0 commit comments

Comments
 (0)