module ActiveRecord::ConnectionAdapters::ColumnDumper

The goal of this module is to move Adapter specific column definitions to the Adapter instead of having it in the schema dumper itself. This code represents the normal case. We can then redefine how certain data types may be handled in the schema dumper on the Adapter level by over-writing this code inside the database specific adapters

Public Instance Methods

column_spec(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 9
def column_spec(column)
  [schema_type_with_virtual(column), prepare_column_options(column)]
end
column_spec_for_primary_key(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 13
def column_spec_for_primary_key(column)
  return {} if default_primary_key?(column)
  spec = { id: schema_type(column).inspect }
  spec.merge!(prepare_column_options(column).except!(:null))
  spec[:default] ||= "nil" if explicit_primary_key_default?(column)
  spec
end
prepare_column_options(column) click to toggle source

This can be overridden on an Adapter level basis to support other extended datatypes (Example: Adding an array option in the PostgreSQL::ColumnDumper)

# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 24
def prepare_column_options(column)
  spec = {}

  if limit = schema_limit(column)
    spec[:limit] = limit
  end

  if precision = schema_precision(column)
    spec[:precision] = precision
  end

  if scale = schema_scale(column)
    spec[:scale] = scale
  end

  default = schema_default(column) if column.has_default?
  spec[:default] = default unless default.nil?

  spec[:null] = "false" unless column.null

  if collation = schema_collation(column)
    spec[:collation] = collation
  end

  spec[:comment] = column.comment.inspect if column.comment.present?

  spec
end

Private Instance Methods

default_primary_key?(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 61
def default_primary_key?(column)
  schema_type(column) == :bigint
end
explicit_primary_key_default?(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 65
def explicit_primary_key_default?(column)
  false
end
schema_collation(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 112
def schema_collation(column)
  column.collation.inspect if column.collation
end
schema_default(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 98
def schema_default(column)
  type = lookup_cast_type_from_column(column)
  default = type.deserialize(column.default)
  if default.nil?
    schema_expression(column)
  else
    type.type_cast_for_schema(default)
  end
end
schema_expression(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 108
def schema_expression(column)
  "-> { #{column.default_function.inspect} }" if column.default_function
end
schema_limit(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 85
def schema_limit(column)
  limit = column.limit unless column.bigint?
  limit.inspect if limit && limit != native_database_types[column.type][:limit]
end
schema_precision(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 90
def schema_precision(column)
  column.precision.inspect if column.precision
end
schema_scale(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 94
def schema_scale(column)
  column.scale.inspect if column.scale
end
schema_type(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 77
def schema_type(column)
  if column.bigint?
    :bigint
  else
    column.type
  end
end
schema_type_with_virtual(column) click to toggle source
# File lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 69
def schema_type_with_virtual(column)
  if supports_virtual_columns? && column.virtual?
    :virtual
  else
    schema_type(column)
  end
end