From f26ca7da9f66ce4458499f6338162945369f1cbe Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Thu, 2 Mar 2023 12:01:57 +0200 Subject: [PATCH] Update native stats start date when transferring (#2719) --- lib/plausible/site.ex | 4 ++++ lib/plausible/site/admin.ex | 4 +++- test/plausible/site/admin_test.exs | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index 97397ffbf..ca466ce06 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -87,6 +87,10 @@ defmodule Plausible.Site do change(site, stats_start_date: val) end + def set_native_stats_start_at(site, val) do + change(site, native_stats_start_at: val) + end + def start_import(site, start_date, end_date, imported_source, status \\ "importing") do change(site, imported_data: %{ diff --git a/lib/plausible/site/admin.ex b/lib/plausible/site/admin.ex index 42b5b484a..c2f29053e 100644 --- a/lib/plausible/site/admin.ex +++ b/lib/plausible/site/admin.ex @@ -96,7 +96,9 @@ defmodule Plausible.SiteAdmin do start_date = Plausible.Stats.Clickhouse.pageview_start_date_local(from_site) {:ok, _} = - Plausible.Site.set_stats_start_date(to_site, start_date) + to_site + |> Plausible.Site.set_stats_start_date(start_date) + |> Plausible.Site.set_native_stats_start_at(from_site.native_stats_start_at) |> Repo.update() :ok diff --git a/test/plausible/site/admin_test.exs b/test/plausible/site/admin_test.exs index 1c3bba27c..9a3490689 100644 --- a/test/plausible/site/admin_test.exs +++ b/test/plausible/site/admin_test.exs @@ -62,6 +62,17 @@ defmodule Plausible.SiteAdminTest do assert Repo.reload(to_site).stats_start_date == ~D[2022-01-01] end + test "updates native_stats_start_date on based on the from_site record" do + from_site = insert(:site, native_stats_start_at: ~N[2022-01-01 01:00:00]) + to_site = insert(:site) + + populate_stats(from_site, [build(:pageview, timestamp: ~N[2022-01-01 13:21:00])]) + + SiteAdmin.transfer_data([from_site], %{"domain" => to_site.domain}) + + assert Repo.reload(to_site).native_stats_start_at == ~N[2022-01-01 01:00:00] + end + test "session_transfer_query" do actual = SiteAdmin.session_transfer_query("from.com", "to.com")