Skip to content

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

time-on-page: site.legacy_time_on_page migration (#5214)

Karl-Aksel Puulmann4c05c978708c
diff --git a/priv/repo/migrations/20250318131615_site_legacy_time_on_page_cutoff.exs b/priv/repo/migrations/20250318131615_site_legacy_time_on_page_cutoff.exs
new file mode 100644
index 0000000000..dd5e1e4aff
--- /dev/null
+++ b/priv/repo/migrations/20250318131615_site_legacy_time_on_page_cutoff.exs
@@ -0,0 +1,30 @@
+defmodule Plausible.Repo.Migrations.SiteLegacyTimeOnPageCutoff do
+ use Ecto.Migration
+ use Plausible
+
+ def change do
+ alter table(:sites) do
+ # New sites will have new time-on-page enabled by default.
+ add :legacy_time_on_page_cutoff, :date,
+ default: fragment("to_date('1970-01-01', 'YYYY-MM-DD')")
+ end
+
+ if Application.get_env(:plausible, :is_selfhost) do
+ # On self-hosted, new time-on-page will be populated during first deploy.
+ execute(
+ fn ->
+ repo().query!("UPDATE sites SET legacy_time_on_page_cutoff = ?", [Date.utc_today()])
+ end,
+ &pass/0
+ )
+ else
+ # On cloud, existing sites will not have legacy_time_on_page_cutoff set. This will be populated by a cron instead.
+ execute(
+ fn -> repo().query!("UPDATE sites SET legacy_time_on_page_cutoff = NULL") end,
+ &pass/0
+ )
+ end
+ end
+
+ defp pass(), do: nil
+end