schema_dumper.rb 3.0 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).except!(:null))
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
        spec[:null] = "false" unless column.null
44

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

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

51 52 53 54 55
        spec
      end

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

      private

61 62 63 64
      def default_primary_key?(column)
        schema_type(column) == :integer
      end

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

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

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

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

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

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

100 101 102
      def schema_collation(column)
        column.collation.inspect if column.collation
      end
103 104 105
    end
  end
end