class ActiveRecord::ConnectionAdapters::CrateAdapter::TableDefinition

Public Instance Methods

array(name, options = {}) click to toggle source
# File lib/active_record/connection_adapters/crate_adapter.rb, line 194
def array(name, options = {})
  array_type = options.delete(:array_type)
  raise "Array columns must specify an :array_type (e.g. array_type: :string)" unless array_type.present?
  column name, "array(#{array_type})", options.merge(array: true)
end
column(name, type = nil, options = {}) click to toggle source
Calls superclass method
# File lib/active_record/connection_adapters/crate_adapter.rb, line 177
def column(name, type = nil, options = {})
  super
  column = self[name]
  column.array = options[:array]
  column.object = options[:object]
  self
end
object(name, options = {}) click to toggle source
# File lib/active_record/connection_adapters/crate_adapter.rb, line 185
def object(name, options = {})
  schema_behaviour = options.delete(:object_schema_behaviour)
  type = schema_behaviour ? "object(#{schema_behaviour})" : schema_behaviour
  schema = options.delete(:object_schema)
  type = "#{type} as (#{object_schema_to_string(schema)})" if schema

  column name, type, options.merge(object: true)
end
primary_key(name, type = :primary_key, options = {}) click to toggle source

Crate doesn’t support auto incrementing, therefore we need to manually set a primary key. You need to assure that you always provide an unique id. This might be done via the SecureRandom.uuid method and a before_save callback, for instance.

# File lib/active_record/connection_adapters/crate_adapter.rb, line 172
def primary_key(name, type = :primary_key, options = {})
  options[:primary_key] = true
  column name, "STRING PRIMARY KEY", options
end

Private Instance Methods

create_column_definition(name, type) click to toggle source
# File lib/active_record/connection_adapters/crate_adapter.rb, line 202
def create_column_definition(name, type)
  ColumnDefinition.new name, type
end
object_schema_to_string(s) click to toggle source
# File lib/active_record/connection_adapters/crate_adapter.rb, line 206
def object_schema_to_string(s)
  ary = []
  s.each_pair do |k, v|
    if v.is_a?(Symbol)
      ary << "#{k} #{v}"
    elsif v.is_a?(Hash)
      a = "array(#{v[:array]})"
      ary << "#{k} #{a}"
    end
  end
  ary.join(', ')
end