schema_dumper.rb 2.9 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
      def column_spec(column)
10 11 12
        spec = Hash[prepare_column_options(column).map { |k, v| [k, "#{k}: #{v}"] }]
        spec[:name] = column.name.inspect
        spec[:type] = schema_type(column).to_s
13 14 15
        spec
      end

16
      def column_spec_for_primary_key(column)
17
        return {} if default_primary_key?(column)
18
        spec = { id: schema_type(column).inspect }
19
        spec.merge!(prepare_column_options(column))
20 21
      end

A
amitkumarsuroliya 已提交
22
      # This can be overridden on an Adapter level basis to support other
23
      # extended datatypes (Example: Adding an array option in the
24
      # PostgreSQL::ColumnDumper)
25
      def prepare_column_options(column)
26
        spec = {}
27

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

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

43 44
        spec[:null] = 'false' unless column.null

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 60 61 62
      def default_primary_key?(column)
        schema_type(column) == :integer
      end

63
      def schema_type(column)
64 65 66 67 68
        if column.bigint?
          :bigint
        else
          column.type
        end
69 70
      end

71
      def schema_limit(column)
72
        limit = column.limit unless column.bigint?
73
        limit.inspect if limit && limit != native_database_types[column.type][:limit]
74 75 76 77 78 79 80 81 82 83
      end

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

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

84
      def schema_default(column)
85
        type = lookup_cast_type_from_column(column)
86
        default = type.deserialize(column.default)
87 88 89
        if default.nil?
          schema_expression(column)
        else
90
          type.type_cast_for_schema(default)
91 92
        end
      end
93

94 95 96 97
      def schema_expression(column)
        "-> { #{column.default_function.inspect} }" if column.default_function
      end

98 99 100
      def schema_collation(column)
        column.collation.inspect if column.collation
      end
101 102 103
    end
  end
end