Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Move Mix.Task.DumpPlans to Plausible.Release (#3544)

Vinicius Brasilaf0b97e68ae8
diff --git a/lib/mix/tasks/dump_plans.ex b/lib/mix/tasks/dump_plans.ex
deleted file mode 100644
index 53041ca57a..0000000000
--- a/lib/mix/tasks/dump_plans.ex
+++ /dev/null
@@ -1,43 +0,0 @@
-defmodule Mix.Tasks.DumpPlans do
- @moduledoc """
- This task dumps plan information from the JSON files to the `plans` table in
- PostgreSQL for internal use. This task deletes existing records and
- (re)inserts the list of plans.
- """
-
- use Mix.Task
- require Logger
-
- @table "plans"
-
- def run(_args) do
- Mix.Task.run("app.start")
-
- Plausible.Repo.delete_all(@table)
-
- plans =
- Plausible.Billing.Plans.all()
- |> Plausible.Billing.Plans.with_prices()
- |> Enum.map(&Map.from_struct/1)
- |> Enum.map(&prepare_for_dump/1)
-
- {count, _} = Plausible.Repo.insert_all(@table, plans)
-
- Logger.info("Inserted #{count} plans")
- end
-
- defp prepare_for_dump(plan) do
- monthly_cost = plan.monthly_cost && Money.to_decimal(plan.monthly_cost)
- yearly_cost = plan.yearly_cost && Money.to_decimal(plan.yearly_cost)
- {:ok, features} = Plausible.Billing.Ecto.FeatureList.dump(plan.features)
- {:ok, team_member_limit} = Plausible.Billing.Ecto.Limit.dump(plan.team_member_limit)
-
- plan
- |> Map.drop([:id])
- |> Map.put(:kind, Atom.to_string(plan.kind))
- |> Map.put(:monthly_cost, monthly_cost)
- |> Map.put(:yearly_cost, yearly_cost)
- |> Map.put(:features, features)
- |> Map.put(:team_member_limit, team_member_limit)
- end
-end
diff --git a/lib/plausible_release.ex b/lib/plausible_release.ex
index 9ed38f7aec..766873ff20 100644
--- a/lib/plausible_release.ex
+++ b/lib/plausible_release.ex
@@ -78,6 +78,35 @@ defmodule Plausible.Release do
Application.put_env(:ua_inspector, :database_path, priv_dir)
end
+ def dump_plans() do
+ prepare()
+
+ Repo.delete_all("plans")
+
+ plans =
+ Plausible.Billing.Plans.all()
+ |> Plausible.Billing.Plans.with_prices()
+ |> Enum.map(fn plan ->
+ plan = Map.from_struct(plan)
+
+ monthly_cost = plan.monthly_cost && Money.to_decimal(plan.monthly_cost)
+ yearly_cost = plan.yearly_cost && Money.to_decimal(plan.yearly_cost)
+ {:ok, features} = Plausible.Billing.Ecto.FeatureList.dump(plan.features)
+ {:ok, team_member_limit} = Plausible.Billing.Ecto.Limit.dump(plan.team_member_limit)
+
+ plan
+ |> Map.drop([:id])
+ |> Map.put(:kind, Atom.to_string(plan.kind))
+ |> Map.put(:monthly_cost, monthly_cost)
+ |> Map.put(:yearly_cost, yearly_cost)
+ |> Map.put(:features, features)
+ |> Map.put(:team_member_limit, team_member_limit)
+ end)
+
+ {count, _} = Repo.insert_all("plans", plans)
+ IO.puts("Inserted #{count} plans")
+ end
+
##############################
defp repos do
@@ -142,7 +171,7 @@ defmodule Plausible.Release do
defp prepare do
IO.puts("Loading #{@app}..")
# Load the code for myapp, but don't start it
- :ok = Application.load(@app)
+ :ok = Application.ensure_loaded(@app)
IO.puts("Starting dependencies..")
# Start apps necessary for executing migrations
diff --git a/test/plausible/release_test.exs b/test/plausible/release_test.exs
index d77d84b2d7..c1410594e2 100644
--- a/test/plausible/release_test.exs
+++ b/test/plausible/release_test.exs
@@ -27,4 +27,9 @@ defmodule Plausible.ReleaseTest do
refute Release.should_be_first_launch?()
end
end
+
+ test "dump_plans/0 inserts plans" do
+ Release.dump_plans()
+ assert Plausible.Repo.aggregate("plans", :count) == 54
+ end
end