Skip to content

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

Backfill accepted_traffic_until for active subscriptions (#3736)

hq18f6ec252e91f
diff --git a/priv/repo/migrations/20240129102900_migrate_accepted_traffic_until.exs b/priv/repo/migrations/20240129102900_migrate_accepted_traffic_until.exs
new file mode 100644
index 0000000000..b8324018a9
--- /dev/null
+++ b/priv/repo/migrations/20240129102900_migrate_accepted_traffic_until.exs
@@ -0,0 +1,27 @@
+defmodule Plausible.Repo.Migrations.MigrateAcceptedTrafficUntil do
+ use Ecto.Migration
+
+ def up do
+ # all the non-free, active subscriptions
+ execute """
+ UPDATE users u1
+ SET accept_traffic_until = s.next_bill_date + 30
+ FROM users u2
+ INNER JOIN subscriptions s ON u2.id = s.user_id
+ WHERE
+ u1.id = u2.id
+ AND
+ s.user_id = u1.id
+ AND
+ s.paddle_plan_id != 'free_10k'
+ AND
+ s.status = 'active'
+ AND
+ u1.accept_traffic_until <= s.next_bill_date
+ """
+ end
+
+ def down do
+ raise "irreversible"
+ end
+end