Class: ClickhouseRuby::ActiveRecord::SchemaCreation

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse_ruby/active_record/schema_statements.rb

Overview

Schema creation for ClickHouse DDL statements

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ SchemaCreation

Returns a new instance of SchemaCreation.



600
601
602
# File 'lib/clickhouse_ruby/active_record/schema_statements.rb', line 600

def initialize(adapter)
  @adapter = adapter
end

Instance Method Details

#accept(table_definition) ⇒ String

Generate CREATE TABLE SQL from a TableDefinition

Parameters:

Returns:

  • (String)

    the CREATE TABLE SQL



608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# File 'lib/clickhouse_ruby/active_record/schema_statements.rb', line 608

def accept(table_definition)
  columns_sql = table_definition.columns.map do |col|
    column_sql(col)
  end.join(",\n  ")

  engine = table_definition.options[:engine] || 'MergeTree'
  order_by = table_definition.options[:order_by]
  partition_by = table_definition.options[:partition_by]
  primary_key = table_definition.options[:primary_key]
  settings = table_definition.options[:settings]

  sql = "CREATE TABLE #{@adapter.quote_table_name(table_definition.name)} (\n  #{columns_sql}\n)"
  sql += "\nENGINE = #{engine}"
  sql += "\nORDER BY (#{order_by})" if order_by
  sql += "\nPARTITION BY #{partition_by}" if partition_by
  sql += "\nPRIMARY KEY (#{primary_key})" if primary_key
  sql += "\nSETTINGS #{settings}" if settings

  sql
end