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

exclude-in-api r2024.02.13-349
Karl-Aksel Puulmann 3 years ago committed by GitHub
parent f3509f2a17
commit f5e15941de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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

@ -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

@ -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
Loading…
Cancel
Save