Skip to content

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

Add migration for minmax indexes on session timestamps (#3772)

Karl-Aksel Puulmannf5e15941deb8
diff --git a/lib/plausible/migration_utils.ex b/lib/plausible/migration_utils.ex
new file mode 100644
index 0000000000..a4daa72e22
--- /dev/null
+++ b/lib/plausible/migration_utils.ex
@@ -0,0 +1,12 @@
+defmodule Plausible.MigrationUtils do
+ @moduledoc """
+ Base module for to use in Clickhouse migrations
+ """
+
+ def on_cluster_statement(table) do
+ case Plausible.IngestRepo.query("SELECT 1 FROM system.replicas WHERE table = '#{table}'") do
+ {:ok, %{rows: []}} -> ""
+ {:ok, _} -> "ON CLUSTER '{cluster}'"
+ end
+ end
+end
diff --git a/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exs b/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exs
index f462786b32..853cd4988f 100644
--- a/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exs
+++ b/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exs
@@ -14,17 +14,11 @@ defmodule Plausible.IngestRepo.Migrations.DisableDeduplicationWindowForImports d
)
def up do
- cluster_query = "SELECT 1 FROM system.replicas WHERE table = 'imported_visitors'"
-
- cluster? =
- case Ecto.Adapters.SQL.query(Plausible.IngestRepo, cluster_query) do
- {:ok, %{rows: []}} -> false
- {:ok, _} -> true
- end
+ on_cluster = Plausible.MigrationUtils.on_cluster_statement("imported_visitors")
for table <- @import_tables do
execute """
- ALTER TABLE #{table} #{if cluster?, do: "ON CLUSTER '{cluster}'"} MODIFY SETTING replicated_deduplication_window = 0
+ ALTER TABLE #{table} #{on_cluster} MODIFY SETTING replicated_deduplication_window = 0
"""
end
end
diff --git a/priv/ingest_repo/migrations/20240209085338_minmax_index_session_timestamp.exs b/priv/ingest_repo/migrations/20240209085338_minmax_index_session_timestamp.exs
new file mode 100644
index 0000000000..f7c2a8d4a7
--- /dev/null
+++ b/priv/ingest_repo/migrations/20240209085338_minmax_index_session_timestamp.exs
@@ -0,0 +1,25 @@
+defmodule Plausible.IngestRepo.Migrations.MinmaxIndexSessionTimestamp do
+ use Ecto.Migration
+
+ def up do
+ execute """
+ ALTER TABLE sessions_v2
+ #{Plausible.MigrationUtils.on_cluster_statement("sessions_v2")}
+ ADD INDEX IF NOT EXISTS minmax_timestamp timestamp
+ TYPE minmax GRANULARITY 1
+ """
+
+ execute """
+ ALTER TABLE sessions_v2
+ MATERIALIZE INDEX minmax_timestamp
+ """
+ end
+
+ def down do
+ execute """
+ ALTER TABLE sessions_v2
+ #{Plausible.MigrationUtils.on_cluster_statement("sessions_v2")}
+ DROP INDEX IF EXISTS minmax_timestamp
+ """
+ end
+end