Module: CoreExtensions::ActiveRecord::InternalMetadata

Defined in:
lib/core_extensions/active_record/internal_metadata.rb

Instance Method Summary collapse

Instance Method Details

#create_tableObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/core_extensions/active_record/internal_metadata.rb', line 5

def create_table
  return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter)
  return if !enabled? || table_exists?

  key_options = connection.internal_string_options_for_primary_key
  table_options = {
    id: false,
    options: 'ReplacingMergeTree(created_at) PARTITION BY key ORDER BY key',
    if_not_exists: true
  }
  full_config = connection.instance_variable_get(:@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'}"
  else
    distributed_suffix = ''
  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