Add ConfigDB migration
authorMark Felder <feld@feld.me>
Thu, 14 Jan 2021 19:53:35 +0000 (13:53 -0600)
committerMark Felder <feld@feld.me>
Thu, 14 Jan 2021 19:53:55 +0000 (13:53 -0600)
priv/repo/migrations/20210113225652_deprecate_public_endpoint.exs [new file with mode: 0644]
test/pleroma/repo/migrations/public_endpoint_deprecation_test.exs [new file with mode: 0644]

diff --git a/priv/repo/migrations/20210113225652_deprecate_public_endpoint.exs b/priv/repo/migrations/20210113225652_deprecate_public_endpoint.exs
new file mode 100644 (file)
index 0000000..d2e6e3c
--- /dev/null
@@ -0,0 +1,57 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.DeprecatePublicEndpoint do
+  use Ecto.Migration
+
+  def up do
+    with %Pleroma.ConfigDB{} = s3_config <-
+           Pleroma.ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Uploaders.S3}),
+         %Pleroma.ConfigDB{} = upload_config <-
+           Pleroma.ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Upload}) do
+      public_endpoint = s3_config.value[:public_endpoint]
+
+      if !is_nil(public_endpoint) do
+        upload_value = upload_config.value |> Keyword.merge(base_url: public_endpoint)
+
+        upload_config
+        |> Ecto.Changeset.change(value: upload_value)
+        |> Pleroma.Repo.update()
+
+        s3_value = s3_config.value |> Keyword.delete(:public_endpoint)
+
+        s3_config
+        |> Ecto.Changeset.change(value: s3_value)
+        |> Pleroma.Repo.update()
+      end
+    else
+      _ -> :ok
+    end
+  end
+
+  def down do
+    with %Pleroma.ConfigDB{} = upload_config <-
+           Pleroma.ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Upload}),
+         %Pleroma.ConfigDB{} = s3_config <-
+           Pleroma.ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Uploaders.S3}) do
+      base_url = upload_config.value[:base_url]
+
+      if !is_nil(base_url) do
+        s3_value = s3_config.value |> Keyword.merge(public_endpoint: base_url)
+
+        s3_config
+        |> Ecto.Changeset.change(value: s3_value)
+        |> Pleroma.Repo.update()
+
+        upload_value = upload_config.value |> Keyword.delete(:base_url)
+
+        upload_config
+        |> Ecto.Changeset.change(value: upload_value)
+        |> Pleroma.Repo.update()
+      end
+    else
+      _ -> :ok
+    end
+  end
+end
diff --git a/test/pleroma/repo/migrations/public_endpoint_deprecation_test.exs b/test/pleroma/repo/migrations/public_endpoint_deprecation_test.exs
new file mode 100644 (file)
index 0000000..b68d24b
--- /dev/null
@@ -0,0 +1,60 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.DeprecatePublicEndpointTest do
+  use Pleroma.DataCase
+  import Pleroma.Factory
+  import Pleroma.Tests.Helpers
+  alias Pleroma.ConfigDB
+
+  setup do: clear_config(Pleroma.Upload)
+  setup do: clear_config(Pleroma.Uploaders.S3)
+  setup_all do: require_migration("20210113225652_deprecate_public_endpoint")
+
+  test "up/0 migrates public_endpoint to base_url", %{migration: migration} do
+    s3_values = [
+      public_endpoint: "https://coolhost.com/",
+      bucket: "secret_bucket"
+    ]
+
+    insert(:config, group: :pleroma, key: Pleroma.Uploaders.S3, value: s3_values)
+
+    upload_values = [
+      uploader: Pleroma.Uploaders.S3
+    ]
+
+    insert(:config, group: :pleroma, key: Pleroma.Upload, value: upload_values)
+
+    migration.up()
+
+    assert [bucket: "secret_bucket"] ==
+             ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Uploaders.S3}).value
+
+    assert [uploader: Pleroma.Uploaders.S3, base_url: "https://coolhost.com/"] ==
+             ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Upload}).value
+  end
+
+  test "down/0 reverts base_url to public_endpoint", %{migration: migration} do
+    s3_values = [
+      bucket: "secret_bucket"
+    ]
+
+    insert(:config, group: :pleroma, key: Pleroma.Uploaders.S3, value: s3_values)
+
+    upload_values = [
+      uploader: Pleroma.Uploaders.S3,
+      base_url: "https://coolhost.com/"
+    ]
+
+    insert(:config, group: :pleroma, key: Pleroma.Upload, value: upload_values)
+
+    migration.down()
+
+    assert [bucket: "secret_bucket", public_endpoint: "https://coolhost.com/"] ==
+             ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Uploaders.S3}).value
+
+    assert [uploader: Pleroma.Uploaders.S3] ==
+             ConfigDB.get_by_params(%{group: :pleroma, key: Pleroma.Upload}).value
+  end
+end