diff --git a/lib/plausible/data_migration/populate_event_session_columns.ex b/lib/plausible/data_migration/populate_event_session_columns.ex index 659ca8c83186..b5c68e7c495e 100644 --- a/lib/plausible/data_migration/populate_event_session_columns.ex +++ b/lib/plausible/data_migration/populate_event_session_columns.ex @@ -22,7 +22,7 @@ defmodule Plausible.DataMigration.PopulateEventSessionColumns do } def run(opts \\ []) do - cluster? = Plausible.MigrationUtils.clustered_table?("sessions_v2") + cluster? = Plausible.MigrationUtils.cluster_name() {:ok, _} = run_sql("create-sessions-dictionary", @@ -50,7 +50,7 @@ defmodule Plausible.DataMigration.PopulateEventSessionColumns do end def kill(opts \\ []) do - cluster? = Plausible.MigrationUtils.clustered_table?("events_v2") + cluster? = Plausible.MigrationUtils.cluster_name() report_progress(opts) diff --git a/lib/plausible/data_migration/versioned_sessions.ex b/lib/plausible/data_migration/versioned_sessions.ex index 09ece08bafc7..7369e44a899f 100644 --- a/lib/plausible/data_migration/versioned_sessions.ex +++ b/lib/plausible/data_migration/versioned_sessions.ex @@ -21,7 +21,7 @@ defmodule Plausible.DataMigration.VersionedSessions do unique_suffix = Timex.now() |> Timex.format!(@suffix_format) - cluster? = Plausible.MigrationUtils.clustered_table?("sessions_v2") + cluster? = Plausible.MigrationUtils.cluster_name() cluster_name = if cluster? do diff --git a/lib/plausible/migration_utils.ex b/lib/plausible/migration_utils.ex index cfe3d729660c..4940ddd023d0 100644 --- a/lib/plausible/migration_utils.ex +++ b/lib/plausible/migration_utils.ex @@ -3,24 +3,24 @@ defmodule Plausible.MigrationUtils do Base module for to use in Clickhouse migrations """ - def on_cluster_statement(table) do - if(clustered_table?(table), do: "ON CLUSTER '#{cluster_name()}'", else: "") - end - - def clustered_table?(table) do - case Plausible.IngestRepo.query("SELECT 1 FROM system.replicas WHERE table = '#{table}'") do - {:ok, %{rows: []}} -> false - {:ok, _} -> true + def on_cluster_statement(_table) do + case cluster_name() do + {:ok, cluster} -> "ON CLUSTER '#{cluster}'" + _ -> "" end end def cluster_name do - case Plausible.IngestRepo.query( - "SELECT cluster FROM system.clusters where cluster = '#{Plausible.IngestRepo.config()[:database]}' limit 1;" - ) do - {:ok, %{rows: [[cluster]]}} -> cluster - {:ok, _} -> raise "No cluster defined." - {:error, _} -> raise "Cluster not found" + try do + case Plausible.IngestRepo.query( + "SELECT cluster FROM system.clusters where cluster = '#{Plausible.IngestRepo.config()[:database]}' limit 1;" + ) do + {:ok, %{rows: [[cluster]]}} -> cluster + {:ok, _} -> false + {:error, _} -> false + end + rescue + _ -> false end end end diff --git a/priv/ingest_repo/migrations/20240423094014_add_imported_custom_events.exs b/priv/ingest_repo/migrations/20240423094014_add_imported_custom_events.exs index 501be9a4cc02..6a76f8167d29 100644 --- a/priv/ingest_repo/migrations/20240423094014_add_imported_custom_events.exs +++ b/priv/ingest_repo/migrations/20240423094014_add_imported_custom_events.exs @@ -4,7 +4,7 @@ defmodule Plausible.IngestRepo.Migrations.AddImportedCustomEvents do def change do # NOTE: Using another table for determining cluster presence on_cluster = Plausible.MigrationUtils.on_cluster_statement("imported_pages") - cluster? = Plausible.MigrationUtils.clustered_table?("imported_pages") + cluster? = Plausible.MigrationUtils.cluster_name() cluster_name = if cluster? do @@ -14,7 +14,7 @@ defmodule Plausible.IngestRepo.Migrations.AddImportedCustomEvents do end settings = - if Plausible.MigrationUtils.clustered_table?("imported_pages") do + if Plausible.MigrationUtils.cluster_name() do """ ENGINE = ReplicatedMergeTree('/clickhouse/#{cluster_name}/tables/{shard}/{database}/imported_custom_events', '{replica}') ORDER BY (site_id, import_id, date, name)