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
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.exnew file mode 100644index 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+enddiff --git a/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exs b/priv/ingest_repo/migrations/20231017073642_disable_deduplication_window_for_imports.exsindex 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 doexecute """- 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"""endenddiff --git a/priv/ingest_repo/migrations/20240209085338_minmax_index_session_timestamp.exs b/priv/ingest_repo/migrations/20240209085338_minmax_index_session_timestamp.exsnew file mode 100644index 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