Module: ClickHouse::ReplicatedTableEnginePatcher

Defined in:
lib/click_house/replicated_table_engine_patcher.rb

Class Method Summary collapse

Class Method Details

.patch_replicated(statement) ⇒ Object



5
6
7
# File 'lib/click_house/replicated_table_engine_patcher.rb', line 5

def self.patch_replicated(statement)
  statement.gsub(/(Engine\s*=\s*)(\w*MergeTree)\b/i, '\1Replicated\2')
end

.unpatch_replicated(statement) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/click_house/replicated_table_engine_patcher.rb', line 9

def self.unpatch_replicated(statement)
  statement.split("\n").map do |line|
    # Remove the "Replicated" prefix.
    # Remove the first 2 arguments: keeper path and replica name
    # Example:
    # ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{uuid}/{shard}', '{replica}', updated_at, deleted)
    # Becomes:
    # ENGINE = ReplacingMergeTree(updated_at, deleted)
    line.gsub(
      /\bENGINE\s*=\s*Replicated(\w*?MergeTree)\s*\(\s*[^,]+,\s*[^,]+(?:,\s*([^)]*))?\)/i
    ) { "ENGINE = #{Regexp.last_match(1)}#{Regexp.last_match(2) ? "(#{Regexp.last_match(2)})" : ''}" }
  end.join("\n")
end