add db migrations for ammo group to pack and ammo type class

This commit is contained in:
shibao 2023-03-30 19:50:36 -04:00
parent b122253b9b
commit 6ed3312ea8
3 changed files with 125 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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