Class: ClickhouseActiverecord::SchemaMigration

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

Class Method Summary collapse

Class Method Details

.all_versionsObject



30
31
32
# File 'lib/clickhouse-activerecord/migration.rb', line 30

def all_versions
  final.where(active: 1).order(:version).pluck(:version)
end

.create_tableObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clickhouse-activerecord/migration.rb', line 8

def create_table
  return if table_exists?

  version_options = connection.internal_string_options_for_primary_key
  table_options = {
    id: false, options: 'ReplacingMergeTree(ver) ORDER BY (version)', 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(version)')

    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 :version, **version_options
    t.column :active, 'Int8', null: false, default: '1'
    t.datetime :ver, null: false, default: -> { 'now()' }
  end
end