[#1304] Fixed null::jsonb handling in User.Info migration.

This commit is contained in:
Ivan Tashkinov 2019-10-23 17:15:48 +03:00
parent 7c7f90bc4f
commit 11cd994425
2 changed files with 12 additions and 7 deletions

View file

@ -100,7 +100,7 @@ defmodule Pleroma.User do
field(:mascot, :map, default: nil) field(:mascot, :map, default: nil)
field(:emoji, {:array, :map}, default: []) field(:emoji, {:array, :map}, default: [])
field(:pleroma_settings_store, :map, default: %{}) field(:pleroma_settings_store, :map, default: %{})
field(:fields, {:array, :map}, default: nil) field(:fields, {:array, :map}, default: [])
field(:raw_fields, {:array, :map}, default: []) field(:raw_fields, {:array, :map}, default: [])
field(:discoverable, :boolean, default: false) field(:discoverable, :boolean, default: false)
field(:invisible, :boolean, default: false) field(:invisible, :boolean, default: false)

View file

@ -1,6 +1,8 @@
defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
use Ecto.Migration use Ecto.Migration
@jsonb_array_default "'[]'::jsonb"
@info_fields [ @info_fields [
:banner, :banner,
:background, :background,
@ -129,10 +131,10 @@ def change do
add(:pinned_activities, {:array, :text}, default: []) add(:pinned_activities, {:array, :text}, default: [])
add(:email_notifications, :map, default: %{"digest" => false}) add(:email_notifications, :map, default: %{"digest" => false})
add(:mascot, :map, default: nil) add(:mascot, :map, default: nil)
add(:emoji, {:array, :map}, default: []) add(:emoji, :map, default: fragment(@jsonb_array_default))
add(:pleroma_settings_store, :map, default: %{}) add(:pleroma_settings_store, :map, default: %{})
add(:fields, {:array, :map}, default: nil) add(:fields, :map, default: fragment(@jsonb_array_default))
add(:raw_fields, {:array, :map}, default: []) add(:raw_fields, :map, default: fragment(@jsonb_array_default))
add(:discoverable, :boolean, default: false, null: false) add(:discoverable, :boolean, default: false, null: false)
add(:invisible, :boolean, default: false, null: false) add(:invisible, :boolean, default: false, null: false)
add(:notification_settings, :map, default: %{}) add(:notification_settings, :map, default: %{})
@ -143,12 +145,15 @@ def change do
for f <- @info_fields do for f <- @info_fields do
set_field = "update users set #{f} =" set_field = "update users set #{f} ="
# Coercion of null::jsonb to NULL
jsonb = "case when info->>'#{f}' IS NULL then null else info->'#{f}' end"
cond do cond do
f in @jsonb_fields -> f in @jsonb_fields ->
execute("#{set_field} info->'#{f}'") execute("#{set_field} #{jsonb}")
f in @array_jsonb_fields -> f in @array_jsonb_fields ->
execute("#{set_field} ARRAY(SELECT jsonb_array_elements(info->'#{f}'))") execute("#{set_field} coalesce(#{jsonb}, #{@jsonb_array_default})")
f in @int_fields -> f in @int_fields ->
execute("#{set_field} (info->>'#{f}')::int") execute("#{set_field} (info->>'#{f}')::int")
@ -157,7 +162,7 @@ def change do
execute("#{set_field} coalesce((info->>'#{f}')::boolean, false)") execute("#{set_field} coalesce((info->>'#{f}')::boolean, false)")
f in @array_text_fields -> f in @array_text_fields ->
execute("#{set_field} ARRAY(SELECT jsonb_array_elements_text(info->'#{f}'))") execute("#{set_field} ARRAY(SELECT jsonb_array_elements_text(#{jsonb}))")
true -> true ->
execute("#{set_field} info->>'#{f}'") execute("#{set_field} info->>'#{f}'")