Class: ClickhouseActiverecord::InternalMetadata

Inherits:
ActiveRecord::InternalMetadata
  • Object
show all
Defined in:
lib/clickhouse-activerecord/migration.rb

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



46
47
48
# File 'lib/clickhouse-activerecord/migration.rb', line 46

def [](key)
  final.where(key: key).pluck(:value).first
end

.[]=(key, value) ⇒ Object



39
40
41
42
43
44
# File 'lib/clickhouse-activerecord/migration.rb', line 39

def []=(key, value)
  row = final.find_by(key: key)
  if row.nil? || row.value != value
    create!(key: key, value: value)
  end
end

.create_tableObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/clickhouse-activerecord/migration.rb', line 50

def create_table
  return if table_exists?

  key_options = connection.internal_string_options_for_primary_key
  table_options = {
    id: false,
    options: connection.adapter_name.downcase == 'clickhouse' ? 'ReplacingMergeTree(created_at) PARTITION BY key ORDER BY key' : '',
    if_not_exists: true
  }
  full_config = connection.instance_variable_get(:@full_config) || {}

  if full_config[:distributed_service_tables]
    table_options.merge!(with_distributed: table_name, sharding_key: 'cityHash64(created_at)')

    distributed_suffix = "_#{full_config[:distributed_service_tables_suffix] || 'distributed'}"
  end

  connection.create_table(table_name + distributed_suffix.to_s, **table_options) do |t|
    t.string :key, **key_options
    t.string :value
    t.timestamps
  end
end