2019-03-06 18:45:26 +01:00
|
|
|
defmodule Mobilizon.Repo.Migrations.MoveUserRoleToEnum do
|
|
|
|
use Ecto.Migration
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Users.UserRole
|
2019-03-06 18:45:26 +01:00
|
|
|
|
|
|
|
def up do
|
2019-09-07 19:54:11 +02:00
|
|
|
UserRole.create_type()
|
2019-03-06 18:45:26 +01:00
|
|
|
|
|
|
|
alter table(:users) do
|
2019-09-07 19:54:11 +02:00
|
|
|
add(:role_tmp, UserRole.type(), default: "user")
|
2019-03-06 18:45:26 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
execute("UPDATE users set role_tmp = 'user' where role = 0")
|
|
|
|
execute("UPDATE users set role_tmp = 'moderator' where role = 1")
|
|
|
|
execute("UPDATE users set role_tmp = 'administrator' where role = 2")
|
|
|
|
|
|
|
|
alter table(:users) do
|
|
|
|
remove(:role)
|
|
|
|
end
|
|
|
|
|
|
|
|
rename(table(:users), :role_tmp, to: :role)
|
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
|
|
|
alter table(:users) do
|
|
|
|
add(:role_tmp, :integer, default: 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
execute("UPDATE users set role_tmp = 0 where role = 'user'")
|
|
|
|
execute("UPDATE users set role_tmp = 1 where role = 'moderator'")
|
|
|
|
execute("UPDATE users set role_tmp = 2 where role = 'administrator'")
|
|
|
|
|
|
|
|
alter table(:users) do
|
|
|
|
remove(:role)
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
UserRole.drop_type()
|
2019-03-06 18:45:26 +01:00
|
|
|
|
|
|
|
rename(table(:users), :role_tmp, to: :role)
|
|
|
|
end
|
|
|
|
end
|