Skip to content

Commit 0c7ce8d

Browse files
committed
Make the category/group name unique index case insensitive
1 parent 96f5402 commit 0c7ce8d

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

lib/sanbase/metric/category/metric_group.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ defmodule Sanbase.Metric.Category.MetricGroup do
6262
def create(attrs) do
6363
%__MODULE__{}
6464
|> changeset(attrs)
65-
|> Repo.insert()
65+
|> Repo.insert(on_conflict: :nothing)
6666
end
6767

6868
@doc """

lib/sanbase/metric/category/scripts/copy_categories.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule Sanbase.Metric.Category.Scripts.CopyCategories do
3434
case map do
3535
%{metric_registry_id: metric_registry_id} when is_integer(metric_registry_id) ->
3636
Sanbase.Metric.Category.create_mapping(%{
37+
# metric: map.metric,
3738
metric_registry_id: metric_registry_id,
3839
category_id: category_id,
3940
group_id: group_id,
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
defmodule Sanbase.Repo.Migrations.ImproveCategorizationUniqueIndex do
2+
use Ecto.Migration
3+
4+
def change do
5+
drop(unique_index(:metric_categories, [:name]))
6+
drop(unique_index(:metric_groups, [:name, :category_id]))
7+
8+
create(
9+
unique_index(:metric_categories, ["(lower(name))"], name: :metric_categories_name_index)
10+
)
11+
12+
create(
13+
unique_index(:metric_groups, ["(lower(name))", :category_id],
14+
name: :metric_groups_name_category_id_index
15+
)
16+
)
17+
end
18+
end

priv/repo/structure.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8343,7 +8343,7 @@ CREATE INDEX menus_user_id_index ON public.menus USING btree (user_id);
83438343
-- Name: metric_categories_name_index; Type: INDEX; Schema: public; Owner: -
83448344
--
83458345

8346-
CREATE UNIQUE INDEX metric_categories_name_index ON public.metric_categories USING btree (name);
8346+
CREATE UNIQUE INDEX metric_categories_name_index ON public.metric_categories USING btree (lower((name)::text));
83478347

83488348

83498349
--
@@ -8371,7 +8371,7 @@ CREATE UNIQUE INDEX metric_category_mappings_module_metric_category_id_group_id_
83718371
-- Name: metric_groups_name_category_id_index; Type: INDEX; Schema: public; Owner: -
83728372
--
83738373

8374-
CREATE UNIQUE INDEX metric_groups_name_category_id_index ON public.metric_groups USING btree (name, category_id);
8374+
CREATE UNIQUE INDEX metric_groups_name_category_id_index ON public.metric_groups USING btree (lower((name)::text), category_id);
83758375

83768376

83778377
--
@@ -11147,3 +11147,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20251023083446);
1114711147
INSERT INTO public."schema_migrations" (version) VALUES (20251023114153);
1114811148
INSERT INTO public."schema_migrations" (version) VALUES (20251027142731);
1114911149
INSERT INTO public."schema_migrations" (version) VALUES (20251027154645);
11150+
INSERT INTO public."schema_migrations" (version) VALUES (20251030132017);

0 commit comments

Comments
 (0)