mobilizon/priv/repo/migrations/20190301141830_move_member_role_to_enum.exs

47 lines
1.3 KiB
Elixir
Raw Normal View History

2019-03-01 17:11:28 +01:00
defmodule Mobilizon.Repo.Migrations.MoveMemberRoleToEnum do
use Ecto.Migration
2019-09-09 00:52:49 +02:00
alias Mobilizon.Actors.MemberRole
2019-03-01 17:11:28 +01:00
def up do
2019-09-09 00:52:49 +02:00
MemberRole.create_type()
2019-03-01 17:11:28 +01:00
alter table(:members) do
2019-09-09 00:52:49 +02:00
add(:role_tmp, MemberRole.type(), default: "member")
2019-03-01 17:11:28 +01:00
end
execute("UPDATE members set role_tmp = 'member' where role = 0")
execute("UPDATE members set role_tmp = 'moderator' where role = 1")
execute("UPDATE members set role_tmp = 'creator' where role = 2")
execute("UPDATE members set role_tmp = 'not_approved' where approved is false")
alter table(:members) do
remove(:role)
remove(:approved)
end
rename(table(:members), :role_tmp, to: :role)
end
def down do
alter table(:members) do
add(:role_tmp, :integer, default: 0)
add(:approved, :boolean, default: true)
end
execute("UPDATE members set approved = false where role = 'not_approved'")
execute("UPDATE members set role_tmp = 0 where role = 'member' or role = 'not_approved'")
execute("UPDATE members set role_tmp = 1 where role = 'moderator'")
execute("UPDATE members set role_tmp = 2 where role = 'administrator' or role = 'creator'")
alter table(:members) do
remove(:role)
end
2019-09-09 00:52:49 +02:00
MemberRole.drop_type()
2019-03-01 17:11:28 +01:00
rename(table(:members), :role_tmp, to: :role)
end
end