Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
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.exdeleted file mode 100644index 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-enddiff --git a/lib/plausible_release.ex b/lib/plausible_release.exindex 9ed38f7aec..766873ff20 100644--- a/lib/plausible_release.ex+++ b/lib/plausible_release.ex@@ -78,6 +78,35 @@ defmodule Plausible.Release doApplication.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 dodefp prepare doIO.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 migrationsdiff --git a/test/plausible/release_test.exs b/test/plausible/release_test.exsindex d77d84b2d7..c1410594e2 100644--- a/test/plausible/release_test.exs+++ b/test/plausible/release_test.exs@@ -27,4 +27,9 @@ defmodule Plausible.ReleaseTest dorefute Release.should_be_first_launch?()endend++ test "dump_plans/0 inserts plans" do+ Release.dump_plans()+ assert Plausible.Repo.aggregate("plans", :count) == 54+ endend