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) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 9
def column_spec(column)
  spec = Hash[prepare_column_options(column).map { |k, v| [k, "#{k}: #{v}"] }]
  spec[:name] = column.name.inspect
  spec[:type] = schema_type(column).to_s
  spec
end
column_spec_for_primary_key(column) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 16
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))
end
migration_keys() Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 55
def migration_keys
  [:name, :limit, :precision, :scale, :default, :null, :collation, :comment]
end

Lists the valid migration options

prepare_column_options(column) Show source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_dumper.rb, line 25
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

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

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.