From 6ed3312ea8be1ac462792fe48f8037a794fad567 Mon Sep 17 00:00:00 2001 From: shibao Date: Thu, 30 Mar 2023 19:50:36 -0400 Subject: [PATCH] add db migrations for ammo group to pack and ammo type class --- CHANGELOG.md | 3 +- .../20230329025816_rename_type_to_class.exs | 112 +++++++++++++++++- ...0329041109_rename_ammo_groups_to_packs.exs | 12 ++ 3 files changed, 125 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b1841ef..0ff83b41 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # v0.9.1 -- Rename ammo_type's type to class to avoid confusion +- Rename ammo type's "type" to "class" to avoid confusion +- Fixes ammo type search - Code quality improvements # v0.9.0 diff --git a/priv/repo/migrations/20230329025816_rename_type_to_class.exs b/priv/repo/migrations/20230329025816_rename_type_to_class.exs index 317622c6..c66ed1f6 100644 --- a/priv/repo/migrations/20230329025816_rename_type_to_class.exs +++ b/priv/repo/migrations/20230329025816_rename_type_to_class.exs @@ -1,7 +1,117 @@ defmodule Cannery.Repo.Migrations.RenameTypeToClass do use Ecto.Migration - def change do + def up do + drop index(:ammo_types, [:type]) + + flush() + rename table(:ammo_types), :type, to: :class + + alter table(:ammo_types) do + remove_if_exists :search, :tsvector + end + + flush() + + create index(:ammo_types, [:class]) + + execute """ + ALTER TABLE ammo_types + ADD COLUMN search tsvector + GENERATED ALWAYS AS ( + setweight(to_tsvector('english', coalesce("name", '')), 'A') || + setweight(to_tsvector('english', coalesce("desc", '')), 'B') || + setweight(to_tsvector('english', coalesce("class", '')), 'B') || + setweight(to_tsvector('english', coalesce("manufacturer", '')), 'C') || + setweight(to_tsvector('english', coalesce("upc", '')), 'C') || + setweight(to_tsvector('english', coalesce("bullet_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("bullet_core", '')), 'D') || + setweight(to_tsvector('english', coalesce("cartridge", '')), 'D') || + setweight(to_tsvector('english', coalesce("caliber", '')), 'D') || + setweight(to_tsvector('english', coalesce("case_material", '')), 'D') || + setweight(to_tsvector('english', coalesce("jacket_type", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("muzzle_velocity", '')), 'D') || + setweight(to_tsvector('english', coalesce("powder_type", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("powder_grains_per_charge", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("grains", '')), 'D') || + setweight(to_tsvector('english', coalesce("pressure", '')), 'D') || + setweight(to_tsvector('english', coalesce("primer_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("firing_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("wadding", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_material", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_size", '')), 'D') || + setweight(to_tsvector('english', coalesce("unfired_length", '')), 'D') || + setweight(to_tsvector('english', coalesce("brass_height", '')), 'D') || + setweight(to_tsvector('english', coalesce("chamber_size", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("load_grains", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_charge_weight", '')), 'D') || + setweight(to_tsvector('english', coalesce("dram_equivalent", '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("tracer", 'tracer', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("incendiary", 'incendiary', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("blank", 'blank', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("corrosive", 'corrosive', '')), 'D') || + setweight(to_tsvector('english', coalesce("manufacturer", '')), 'D') || + setweight(to_tsvector('english', coalesce("upc", '')), 'D') + ) STORED + """ + end + + def down do + drop index(:ammo_types, [:class]) + + flush() + + rename table(:ammo_types), :class, to: :type + + alter table(:ammo_types) do + remove_if_exists :search, :tsvector + end + + flush() + + create index(:ammo_types, [:type]) + + execute """ + ALTER TABLE ammo_types + ADD COLUMN search TSVECTOR + GENERATED ALWAYS AS ( + setweight(to_tsvector('english', coalesce("name", '')), 'A') || + setweight(to_tsvector('english', coalesce("desc", '')), 'B') || + setweight(to_tsvector('english', coalesce("type", '')), 'B') || + setweight(to_tsvector('english', coalesce("manufacturer", '')), 'C') || + setweight(to_tsvector('english', coalesce("upc", '')), 'C') || + setweight(to_tsvector('english', coalesce("bullet_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("bullet_core", '')), 'D') || + setweight(to_tsvector('english', coalesce("cartridge", '')), 'D') || + setweight(to_tsvector('english', coalesce("caliber", '')), 'D') || + setweight(to_tsvector('english', coalesce("case_material", '')), 'D') || + setweight(to_tsvector('english', coalesce("jacket_type", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("muzzle_velocity", '')), 'D') || + setweight(to_tsvector('english', coalesce("powder_type", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("powder_grains_per_charge", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("grains", '')), 'D') || + setweight(to_tsvector('english', coalesce("pressure", '')), 'D') || + setweight(to_tsvector('english', coalesce("primer_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("firing_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("wadding", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_type", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_material", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_size", '')), 'D') || + setweight(to_tsvector('english', coalesce("unfired_length", '')), 'D') || + setweight(to_tsvector('english', coalesce("brass_height", '')), 'D') || + setweight(to_tsvector('english', coalesce("chamber_size", '')), 'D') || + setweight(to_tsvector('english', immutable_to_string("load_grains", '')), 'D') || + setweight(to_tsvector('english', coalesce("shot_charge_weight", '')), 'D') || + setweight(to_tsvector('english', coalesce("dram_equivalent", '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("tracer", 'tracer', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("incendiary", 'incendiary', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("blank", 'blank', '')), 'D') || + setweight(to_tsvector('english', boolean_to_string("corrosive", 'corrosive', '')), 'D') || + setweight(to_tsvector('english', coalesce("manufacturer", '')), 'D') || + setweight(to_tsvector('english', coalesce("upc", '')), 'D') + ) STORED + """ end end diff --git a/priv/repo/migrations/20230329041109_rename_ammo_groups_to_packs.exs b/priv/repo/migrations/20230329041109_rename_ammo_groups_to_packs.exs index 840ce4c7..89c1a224 100644 --- a/priv/repo/migrations/20230329041109_rename_ammo_groups_to_packs.exs +++ b/priv/repo/migrations/20230329041109_rename_ammo_groups_to_packs.exs @@ -5,6 +5,9 @@ defmodule Cannery.Repo.Migrations.RenameAmmoGroupsToPacks do drop index(:ammo_groups, [:user_id], where: "count = 0", name: :empty_ammo_groups_index) drop index(:ammo_groups, [:user_id, :ammo_type_id]) drop index(:ammo_groups, [:user_id, :container_id]) + drop index(:ammo_groups, [:ammo_type_id]) + drop index(:ammo_groups, [:container_id]) + drop index(:ammo_groups, [:user_id]) flush() @@ -15,6 +18,9 @@ defmodule Cannery.Repo.Migrations.RenameAmmoGroupsToPacks do create index(:packs, [:user_id], where: "count = 0", name: :empty_packs_index) create index(:packs, [:user_id, :ammo_type_id]) create index(:packs, [:user_id, :container_id]) + create index(:packs, [:ammo_type_id]) + create index(:packs, [:container_id]) + create index(:packs, [:user_id]) rename table(:shot_groups), :ammo_group_id, to: :pack_id end @@ -23,6 +29,9 @@ defmodule Cannery.Repo.Migrations.RenameAmmoGroupsToPacks do drop index(:packs, [:user_id], where: "count = 0", name: :empty_packs_index) drop index(:packs, [:user_id, :ammo_type_id]) drop index(:packs, [:user_id, :container_id]) + drop index(:packs, [:ammo_type_id]) + drop index(:packs, [:container_id]) + drop index(:packs, [:user_id]) flush() @@ -33,6 +42,9 @@ defmodule Cannery.Repo.Migrations.RenameAmmoGroupsToPacks do create index(:ammo_groups, [:user_id], where: "count = 0", name: :empty_ammo_groups_index) create index(:ammo_groups, [:user_id, :ammo_type_id]) create index(:ammo_groups, [:user_id, :container_id]) + create index(:ammo_groups, [:ammo_type_id]) + create index(:ammo_groups, [:container_id]) + create index(:ammo_groups, [:user_id]) rename table(:shot_groups), :pack_id, to: :ammo_group_id end