Add migration
[akkoma] / priv / repo / migrations / 20191009154608_copy_users_info_fields_to_users.exs
index 6b096a9626237ccb4c5ab6c151969a36081b0238..9dd27511cce153ae4067a2032589f24a959c0c8d 100644 (file)
@@ -1,6 +1,8 @@
-defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
+defmodule Pleroma.Repo.Migrations.CopyUsersInfoFieldsToUsers do
   use Ecto.Migration
 
+  @jsonb_array_default "'[]'::jsonb"
+
   @info_fields [
     :banner,
     :background,
@@ -28,9 +30,6 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
     :settings,
     :magic_key,
     :uri,
-    :topic,
-    :hub,
-    :salmon,
     :hide_followers_count,
     :hide_follows_count,
     :hide_followers,
@@ -45,6 +44,7 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
     :fields,
     :raw_fields,
     :discoverable,
+    :invisible,
     :skip_thread_containment,
     :notification_settings
   ]
@@ -80,6 +80,7 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
     :hide_follows,
     :hide_favorites,
     :discoverable,
+    :invisible,
     :skip_thread_containment
   ]
 
@@ -121,9 +122,6 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
       add(:settings, :map, default: nil)
       add(:magic_key, :text, default: nil)
       add(:uri, :text, default: nil)
-      add(:topic, :text, default: nil)
-      add(:hub, :text, default: nil)
-      add(:salmon, :text, default: nil)
       add(:hide_followers_count, :boolean, default: false, null: false)
       add(:hide_follows_count, :boolean, default: false, null: false)
       add(:hide_followers, :boolean, default: false, null: false)
@@ -133,25 +131,29 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
       add(:pinned_activities, {:array, :text}, default: [])
       add(:email_notifications, :map, default: %{"digest" => false})
       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(:fields, {:array, :map}, default: nil)
-      add(:raw_fields, {:array, :map}, default: [])
+      add(:fields, :map, default: fragment(@jsonb_array_default))
+      add(:raw_fields, :map, default: fragment(@jsonb_array_default))
       add(:discoverable, :boolean, default: false, null: false)
+      add(:invisible, :boolean, default: false, null: false)
       add(:notification_settings, :map, default: %{})
       add(:skip_thread_containment, :boolean, default: false, null: false)
     end
 
-    if direction == :up do
+    if direction() == :up do
       for f <- @info_fields do
         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
           f in @jsonb_fields ->
-            execute("#{set_field} info->'#{f}'")
+            execute("#{set_field} #{jsonb}")
 
           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 ->
             execute("#{set_field} (info->>'#{f}')::int")
@@ -160,7 +162,7 @@ defmodule Pleroma.Repo.Migrations.CopyUsersInfoaddsToUsers do
             execute("#{set_field} coalesce((info->>'#{f}')::boolean, false)")
 
           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 ->
             execute("#{set_field} info->>'#{f}'")