schema_dumper.rb 1.5 KB
Newer Older
1 2 3 4 5
module ActiveRecord
  module ConnectionAdapters # :nodoc:
    # 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.
6
    # We can then redefine how certain data types may be handled in the schema dumper on the
C
Carson McDonald 已提交
7
    # Adapter level by over-writing this code inside the database specific adapters
8 9 10 11 12 13 14 15 16 17 18 19 20
    module ColumnDumper
      def column_spec(column, types)
        spec = prepare_column_options(column, types)
        (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.to_s}: ")}
        spec
      end

      # This can be overridden on a Adapter level basis to support other
      # extended datatypes (Example: Adding an array option in the
      # PostgreSQLAdapter)
      def prepare_column_options(column, types)
        spec = {}
        spec[:name]      = column.name.inspect
21 22
        spec[:type]      = column.type.to_s
        spec[:limit]     = column.limit.inspect if column.limit != types[column.type][:limit]
23 24 25
        spec[:precision] = column.precision.inspect if column.precision
        spec[:scale]     = column.scale.inspect if column.scale
        spec[:null]      = 'false' unless column.null
26
        spec[:default]   = column.type_cast_for_schema(column.default) if column.has_default?
27 28 29 30 31 32 33 34 35 36
        spec
      end

      # Lists the valid migration options
      def migration_keys
        [:name, :limit, :precision, :scale, :default, :null]
      end
    end
  end
end