Hi,
I previously had the below migration and schema with statuses [:start, :closed].
def up do
update_statuses([:open, :closed], :up)
end
def down do
update_statuses([:start, :closed], :down)
end
defp update_statuses(statuses, direction) do
create_type(:new_statuses, types_to_be_used)
alter table(:posts) do
add :new_status, :new_statuses, null: true, default: nil
end
flush()
["UPDATE posts SET new_status = 'open' WHERE status = 'start'",
"UPDATE posts SET new_status = 'closed' WHERE status = 'closed'"]
|> Enum.each(fn query ->
Ecto.Adapters.SQL.query(MyApp.Repo, query, [])
end)
alter table(:posts) do
remove :status
end
drop_type(:statuses)
rename_type(
:new_statuses,
:statuses
)
rename table(:posts), :new_status, to: :status
end
But after this migration is run and deployed, I updated schema with statuses [:open, :closed] and when I run mix ecto.reset
,
I get error like this
QUERY ERROR db=0.0ms
UPDATE statuses SET new_status = 'open' WHERE status = 'start'
15:25:42.182 [info] alter table posts
** (Postgrex.Error) ERROR 25P02 (in_failed_sql_transaction) current transaction is aborted, commands ignored until end of transaction block
query: ALTER TABLE "posts" DROP COLUMN "status"
Any idea why this is happening and how we can avoid this?