Skip to content

Commit

Permalink
Merge pull request #14 from coingaming/fix/migration_utils
Browse files Browse the repository at this point in the history
fix: 🐛 cluster detection
  • Loading branch information
karlosmid authored Jan 8, 2025
2 parents 8e3d971 + 7f44487 commit 5677716
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/plausible/data_migration/versioned_sessions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions lib/plausible/migration_utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 5677716

Please sign in to comment.