Skip to content

Commit

Permalink
invoke Item.update_all_items_cid/0 from seeds.exs #410
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Aug 27, 2023
1 parent 9573f49 commit 666c198
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -440,4 +440,30 @@ defmodule App.Item do
# Sort list by position so the lowest position is displayed first:
|> Enum.sort_by(fn i -> i.position end, :asc)
end

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Below this point is item transition code that will be DELETED! #
# We just need it to update all existing items to add cid ... #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

@doc """
`update_all_items_cid/0` updates all `item` records with a `cid` value.
This will not be needed once all records are transitioned.
"""
def update_all_items_cid do
items = list_items()

Enum.each(items, fn i ->
item = %{
person_id: i.person_id,
status: i.status,
text: i.text,
id: i.id
}

i
|> changeset(Map.put(item, :cid, Cid.cid(item)))
|> Repo.update()
end)
end
end
9 changes: 7 additions & 2 deletions priv/repo/migrations/20230824153220_add_cid.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
defmodule App.Repo.Migrations.AddCidToItem do
defmodule App.Repo.Migrations.AddCid do
use Ecto.Migration

def change do


def up do
alter table(:items) do
add(:cid, :string)
end
# https://stackoverflow.com/questions/36723407/how-to-run-updating-in-migration-for-ecto
flush()

end
end
2 changes: 2 additions & 0 deletions priv/repo/seeds.exs
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,6 @@ if env == :test || env == :dev do

# Add items to lists:
App.ListItems.add_all_items_to_all_list_for_person_id(person_id)

Item.update_all_items_cid()
end

0 comments on commit 666c198

Please sign in to comment.