added migrate old settings to new
authorMaksim Pechnikov <parallel588@gmail.com>
Sat, 25 Jul 2020 04:09:09 +0000 (07:09 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Sat, 25 Jul 2020 06:16:43 +0000 (09:16 +0300)
priv/repo/migrations/20200724133313_move_welcome_settings.exs [new file with mode: 0644]
test/migrations/20200724133313_move_welcome_settings_test.exs [new file with mode: 0644]

diff --git a/priv/repo/migrations/20200724133313_move_welcome_settings.exs b/priv/repo/migrations/20200724133313_move_welcome_settings.exs
new file mode 100644 (file)
index 0000000..323a8fc
--- /dev/null
@@ -0,0 +1,94 @@
+defmodule Pleroma.Repo.Migrations.MoveWelcomeSettings do
+  use Ecto.Migration
+
+  alias Pleroma.ConfigDB
+
+  @old_keys [:welcome_user_nickname, :welcome_message]
+
+  def up do
+    with {:ok, config, {keep_values, move_values}} <- get_old_values() do
+      insert_welcome_settings(move_values)
+      update_instance_config(config, keep_values)
+    end
+  end
+
+  def down do
+    with {:ok, welcome_config, revert_values} <- get_revert_values() do
+      revert_instance_config(revert_values)
+      Pleroma.Repo.delete(welcome_config)
+    end
+  end
+
+  defp insert_welcome_settings([_ | _] = values) do
+    unless String.trim(values[:welcome_message]) == "" do
+      config_values = [
+        direct_message: %{
+          enabled: true,
+          sender_nickname: values[:welcome_user_nickname],
+          message: values[:welcome_message]
+        },
+        email: %{
+          enabled: false,
+          sender: nil,
+          subject: "Welcome to <%= instance_name %>",
+          html: "Welcome to <%= instance_name %>",
+          text: "Welcome to <%= instance_name %>"
+        }
+      ]
+
+      {:ok, _} =
+        %ConfigDB{}
+        |> ConfigDB.changeset(%{group: :pleroma, key: :welcome, value: config_values})
+        |> Pleroma.Repo.insert()
+    end
+
+    :ok
+  end
+
+  defp insert_welcome_settings(_), do: :noop
+
+  defp revert_instance_config(%{} = revert_values) do
+    values = [
+      welcome_user_nickname: revert_values[:sender_nickname],
+      welcome_message: revert_values[:message]
+    ]
+
+    ConfigDB.update_or_create(%{group: :pleroma, key: :instance, value: values})
+  end
+
+  defp revert_instance_config(_), do: :noop
+
+  defp update_instance_config(config, values) do
+    {:ok, _} =
+      config
+      |> ConfigDB.changeset(%{value: values})
+      |> Pleroma.Repo.update()
+
+    :ok
+  end
+
+  defp get_revert_values do
+    config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+
+    cond do
+      is_nil(config) -> {:noop, nil, nil}
+      true -> {:ok, config, config.value[:direct_message]}
+    end
+  end
+
+  defp get_old_values do
+    config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+
+    cond do
+      is_nil(config) ->
+        {:noop, config, {}}
+
+      is_binary(config.value[:welcome_message]) ->
+        {:ok, config,
+         {Keyword.drop(config.value, @old_keys), Keyword.take(config.value, @old_keys)}}
+
+      true ->
+        {:ok, config, {Keyword.drop(config.value, @old_keys), []}}
+    end
+  end
+end
diff --git a/test/migrations/20200724133313_move_welcome_settings_test.exs b/test/migrations/20200724133313_move_welcome_settings_test.exs
new file mode 100644 (file)
index 0000000..739f245
--- /dev/null
@@ -0,0 +1,140 @@
+defmodule Pleroma.Repo.Migrations.MoveWelcomeSettingsTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+  import Pleroma.Tests.Helpers
+  alias Pleroma.ConfigDB
+
+  setup_all do: require_migration("20200724133313_move_welcome_settings")
+
+  describe "up/0" do
+    test "converts welcome settings", %{migration: migration} do
+      insert(:config,
+        group: :pleroma,
+        key: :instance,
+        value: [
+          welcome_message: "Test message",
+          welcome_user_nickname: "jimm",
+          name: "Pleroma"
+        ]
+      )
+
+      migration.up()
+      instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+      welcome_config = ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+
+      assert instance_config.value == [name: "Pleroma"]
+
+      assert welcome_config.value == [
+               direct_message: %{
+                 enabled: true,
+                 message: "Test message",
+                 sender_nickname: "jimm"
+               },
+               email: %{
+                 enabled: false,
+                 html: "Welcome to <%= instance_name %>",
+                 sender: nil,
+                 subject: "Welcome to <%= instance_name %>",
+                 text: "Welcome to <%= instance_name %>"
+               }
+             ]
+    end
+
+    test "does nothing when message empty", %{migration: migration} do
+      insert(:config,
+        group: :pleroma,
+        key: :instance,
+        value: [
+          welcome_message: "",
+          welcome_user_nickname: "jimm",
+          name: "Pleroma"
+        ]
+      )
+
+      migration.up()
+      instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+      refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+      assert instance_config.value == [name: "Pleroma"]
+    end
+
+    test "does nothing when welcome_message not set", %{migration: migration} do
+      insert(:config,
+        group: :pleroma,
+        key: :instance,
+        value: [welcome_user_nickname: "jimm", name: "Pleroma"]
+      )
+
+      migration.up()
+      instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+      refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+      assert instance_config.value == [name: "Pleroma"]
+    end
+  end
+
+  describe "down/0" do
+    test "revert new settings to old when instance setting not exists", %{migration: migration} do
+      insert(:config,
+        group: :pleroma,
+        key: :welcome,
+        value: [
+          direct_message: %{
+            enabled: true,
+            message: "Test message",
+            sender_nickname: "jimm"
+          },
+          email: %{
+            enabled: false,
+            html: "Welcome to <%= instance_name %>",
+            sender: nil,
+            subject: "Welcome to <%= instance_name %>",
+            text: "Welcome to <%= instance_name %>"
+          }
+        ]
+      )
+
+      migration.down()
+
+      refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+      instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+
+      assert instance_config.value == [
+               welcome_user_nickname: "jimm",
+               welcome_message: "Test message"
+             ]
+    end
+
+    test "revert new settings to old when instance setting exists", %{migration: migration} do
+      insert(:config, group: :pleroma, key: :instance, value: [name: "Pleroma App"])
+
+      insert(:config,
+        group: :pleroma,
+        key: :welcome,
+        value: [
+          direct_message: %{
+            enabled: true,
+            message: "Test message",
+            sender_nickname: "jimm"
+          },
+          email: %{
+            enabled: false,
+            html: "Welcome to <%= instance_name %>",
+            sender: nil,
+            subject: "Welcome to <%= instance_name %>",
+            text: "Welcome to <%= instance_name %>"
+          }
+        ]
+      )
+
+      migration.down()
+
+      refute ConfigDB.get_by_params(%{group: :pleroma, key: :welcome})
+      instance_config = ConfigDB.get_by_params(%{group: :pleroma, key: :instance})
+
+      assert instance_config.value == [
+               name: "Pleroma App",
+               welcome_user_nickname: "jimm",
+               welcome_message: "Test message"
+             ]
+    end
+  end
+end