schema_dumper.rb 2.8 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
    module ColumnDumper
9 10
      def column_spec(column)
        spec = prepare_column_options(column)
11
        (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k}: ")}
12 13 14
        spec
      end

15 16
      def column_spec_for_primary_key(column)
        return if column.type == :integer
17
        spec = { id: schema_type(column).inspect }
18 19 20
        spec.merge!(prepare_column_options(column).delete_if { |key, _| [:name, :type].include?(key) })
      end

A
amitkumarsuroliya 已提交
21
      # This can be overridden on an Adapter level basis to support other
22
      # extended datatypes (Example: Adding an array option in the
23
      # PostgreSQL::ColumnDumper)
24
      def prepare_column_options(column)
25 26
        spec = {}
        spec[:name]      = column.name.inspect
27
        spec[:type]      = schema_type(column).to_s
28 29
        spec[:null]      = 'false' unless column.null

30 31 32 33 34 35 36 37 38 39 40
        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
41 42 43 44

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

45 46 47 48
        if collation = schema_collation(column)
          spec[:collation] = collation
        end

49 50 51 52 53
        spec
      end

      # Lists the valid migration options
      def migration_keys
54
        [:name, :limit, :precision, :scale, :default, :null, :collation]
55
      end
56 57 58

      private

59
      def schema_type(column)
60
        column.type
61 62
      end

63
      def schema_limit(column)
64 65
        limit = column.limit
        limit.inspect if limit && limit != native_database_types[column.type][:limit]
66 67 68 69 70 71 72 73 74 75
      end

      def schema_precision(column)
        column.precision.inspect if column.precision
      end

      def schema_scale(column)
        column.scale.inspect if column.scale
      end

76
      def schema_default(column)
77
        type = lookup_cast_type_from_column(column)
78
        default = type.deserialize(column.default)
79 80 81
        if default.nil?
          schema_expression(column)
        else
82
          type.type_cast_for_schema(default)
83 84
        end
      end
85

86 87 88 89
      def schema_expression(column)
        "-> { #{column.default_function.inspect} }" if column.default_function
      end

90 91 92
      def schema_collation(column)
        column.collation.inspect if column.collation
      end
93 94 95
    end
  end
end