migrating task refactor
authorAlex S <alex.strizhakov@gmail.com>
Mon, 15 Jul 2019 12:45:27 +0000 (15:45 +0300)
committerAlex S <alex.strizhakov@gmail.com>
Mon, 15 Jul 2019 12:45:27 +0000 (15:45 +0300)
lib/mix/tasks/pleroma/config.ex
test/tasks/config_test.exs

index 4868866d6a6ad10630ad35ede42161cb4f1751d6..a7d0fac5da97d51b4c4015224af156595d49cd84 100644 (file)
@@ -28,6 +28,14 @@ defmodule Mix.Tasks.Pleroma.Config do
       |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
       |> Enum.each(fn {k, v} ->
         key = to_string(k) |> String.replace("Elixir.", "")
+
+        key =
+          if String.starts_with?(key, "Pleroma.") do
+            key
+          else
+            ":" <> key
+          end
+
         {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
         Mix.shell().info("#{key} is migrated.")
       end)
@@ -53,17 +61,9 @@ defmodule Mix.Tasks.Pleroma.Config do
 
       Repo.all(Config)
       |> Enum.each(fn config ->
-        mark =
-          if String.starts_with?(config.key, "Pleroma.") or
-               String.starts_with?(config.key, "Ueberauth"),
-             do: ",",
-             else: ":"
-
-        key = String.trim_leading(config.key, ":")
-
         IO.write(
           file,
-          "config :#{config.group}, #{key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
+          "config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\r\n"
         )
 
         if delete? do
index bbcc5721719a7de635ad95130d8f7d53df49ea3b..a9b79eb5b572a8bcf44fa1a137063059c7c807b8 100644 (file)
@@ -34,8 +34,8 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
 
     Mix.Tasks.Pleroma.Config.run(["migrate_to_db"])
 
-    first_db = Config.get_by_params(%{group: "pleroma", key: "first_setting"})
-    second_db = Config.get_by_params(%{group: "pleroma", key: "second_setting"})
+    first_db = Config.get_by_params(%{group: "pleroma", key: ":first_setting"})
+    second_db = Config.get_by_params(%{group: "pleroma", key: ":second_setting"})
     refute Config.get_by_params(%{group: "pleroma", key: "Pleroma.Repo"})
 
     assert Config.from_binary(first_db.value) == [key: "value", key2: [Pleroma.Repo]]
@@ -45,13 +45,13 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
   test "settings are migrated to file and deleted from db", %{temp_file: temp_file} do
     Config.create(%{
       group: "pleroma",
-      key: "setting_first",
+      key: ":setting_first",
       value: [key: "value", key2: [Pleroma.Activity]]
     })
 
     Config.create(%{
       group: "pleroma",
-      key: "setting_second",
+      key: ":setting_second",
       value: [key: "valu2", key2: [Pleroma.Repo]]
     })
 
@@ -61,7 +61,7 @@ defmodule Mix.Tasks.Pleroma.ConfigTest do
     assert File.exists?(temp_file)
     {:ok, file} = File.read(temp_file)
 
-    assert file =~ "config :pleroma, setting_first:"
-    assert file =~ "config :pleroma, setting_second:"
+    assert file =~ "config :pleroma, :setting_first,"
+    assert file =~ "config :pleroma, :setting_second,"
   end
 end