Class: ActiveRecord::ConnectionAdapters::Clickhouse::SchemaCreation

Inherits:
ConnectionAdapters::SchemaCreation
  • Object
show all
Defined in:
lib/active_record/connection_adapters/clickhouse/schema_creation.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_as_clause!(create_sql, options) ⇒ Object



60
61
62
63
64
65
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 60

def add_as_clause!(create_sql, options)
  return unless options.as

  assign_database_to_subquery!(options.as) if options.view
  create_sql << " AS #{to_sql(options.as)}"
end

#add_column_options!(sql, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 20

def add_column_options!(sql, options)
  if options[:value]
    sql.gsub!(/\s+(.*)/, " \\1(#{options[:value]})")
  end
  if options[:fixed_string]
    sql.gsub!(/\s+(.*)/, " FixedString(#{options[:fixed_string]})")
  end
  if options[:null] || options[:null].nil?
    sql.gsub!(/\s+(.*)/, ' Nullable(\1)')
  end
  if options[:low_cardinality]
    sql.gsub!(/\s+(.*)/, ' LowCardinality(\1)')
  end
  if options[:array]
    sql.gsub!(/\s+(.*)/, ' Array(\1)')
  end
  if options[:map]
    sql.gsub!(/\s+(.*)/, ' Map(String, \1)')
  end
  sql.gsub!(/(\sString)\(\d+\)/, '\1')
  sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
  sql
end

#add_table_options!(create_sql, options) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 44

def add_table_options!(create_sql, options)
  opts = options[:options]
  if options.respond_to?(:options)
    # rails 6.1
    opts ||= options.options
  end

  if opts.present?
    create_sql << " ENGINE = #{opts}"
  else
    create_sql << " ENGINE = Log()"
  end

  create_sql
end

#add_to_clause!(create_sql, options) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 79

def add_to_clause!(create_sql, options)
  # If you do not specify a database explicitly, ClickHouse will use the "default" database.
  return unless options.to

  match = options.to.match(/(?<database>.+(?=\.))?(?<table_name>.+)/i)
  return unless match
  return if match[:database]

  create_sql << "TO #{current_database}.#{match[:table_name].sub('.', '')}"
end

#assign_database_to_subquery!(subquery) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 67

def assign_database_to_subquery!(subquery)
  # If you do not specify a database explicitly, ClickHouse will use the "default" database.
  return unless subquery

  match = subquery.match(/(?<=from)[^.\w]+(?<database>\w+(?=\.))?(?<table_name>[.\w]+)/i)
  return unless match
  return if match[:database]

  subquery[match.begin(:table_name)...match.end(:table_name)] =
    "#{current_database}.#{match[:table_name].sub('.', '')}"
end

#current_databaseObject



155
156
157
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 155

def current_database
  ActiveRecord::Base.connection_db_config.database
end

#table_modifier_in_create(o) ⇒ Object

Returns any SQL string to go between CREATE and TABLE. May be nil.



115
116
117
118
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 115

def table_modifier_in_create(o)
  " TEMPORARY" if o.temporary
  " MATERIALIZED" if o.materialized
end

#visit_AddColumnDefinition(o) ⇒ Object



14
15
16
17
18
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 14

def visit_AddColumnDefinition(o)
  sql = +"ADD COLUMN #{accept(o.column)}"
  sql << " AFTER " + quote_column_name(o.column.options[:after]) if o.column.options.key?(:after)
  sql
end

#visit_ChangeColumnDefinition(o) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 120

def visit_ChangeColumnDefinition(o)
  column = o.column
  column.sql_type = type_to_sql(column.type, **column.options)
  options = column_options(column)

  quoted_column_name = quote_column_name(o.name)
  type = column.sql_type
  type = "Nullable(#{type})" if options[:null]
  change_column_sql = +"MODIFY COLUMN #{quoted_column_name} #{type}"

  if options.key?(:default)
    quoted_default = quote_default_expression(options[:default], column)
    change_column_sql << " DEFAULT #{quoted_default}"
  end

  change_column_sql
end

#visit_CreateIndexDefinition(o) ⇒ Object



151
152
153
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 151

def visit_CreateIndexDefinition(o)
  visit_IndexDefinition(o.index, true)
end

#visit_IndexDefinition(o, create = false) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 138

def visit_IndexDefinition(o, create = false)
  sql = create ? ["ALTER TABLE #{quote_table_name(o.table)} ADD"] : []
  sql << "INDEX"
  sql << "IF NOT EXISTS" if o.if_not_exists
  sql << "IF EXISTS" if o.if_exists
  sql << "#{quote_column_name(o.name)} (#{o.expression}) TYPE #{o.type}"
  sql << "GRANULARITY #{o.granularity}" if o.granularity
  sql << "FIRST #{quote_column_name(o.first)}" if o.first
  sql << "AFTER #{quote_column_name(o.after)}" if o.after

  sql.join(' ')
end

#visit_TableDefinition(o) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_record/connection_adapters/clickhouse/schema_creation.rb', line 90

def visit_TableDefinition(o)
  create_sql = +"CREATE#{table_modifier_in_create(o)} #{o.view ? "VIEW" : "TABLE"} "
  create_sql << "IF NOT EXISTS " if o.if_not_exists
  create_sql << "#{quote_table_name(o.name)} "
  add_as_clause!(create_sql, o) if o.as && !o.view
  add_to_clause!(create_sql, o) if o.materialized

  statements = o.columns.map { |c| accept c }
  statements << accept(o.primary_keys) if o.primary_keys

  if supports_indexes_in_create?
    indexes = o.indexes.map do |expression, options|
      accept(@conn.add_index_options(o.name, expression, **options))
    end
    statements.concat(indexes)
  end

  create_sql << "(#{statements.join(', ')})" if statements.present?
  # Attach options for only table or materialized view without TO section
  add_table_options!(create_sql, o) if !o.view || o.view && o.materialized && !o.to
  add_as_clause!(create_sql, o) if o.as && o.view
  create_sql
end