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
        [schema_type(column), prepare_column_options(column)]
11 12
      end

13
      def column_spec_for_primary_key(column)
14
        return {} if default_primary_key?(column)
15
        spec = { id: schema_type(column).inspect }
16
        spec.merge!(prepare_column_options(column).except!(:null))
17 18
      end

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

25 26 27 28 29 30 31 32 33 34 35
        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
36 37

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

40
        spec[:null] = "false" unless column.null
41

42 43 44 45
        if collation = schema_collation(column)
          spec[:collation] = collation
        end

46
        spec[:comment] = column.comment.inspect if column.comment.present?
47

48 49 50 51 52
        spec
      end

      # Lists the valid migration options
      def migration_keys
R
Ryuta Kamizono 已提交
53
        [:limit, :precision, :scale, :default, :null, :collation, :comment]
54
      end
55 56 57

      private

58 59 60
        def default_primary_key?(column)
          schema_type(column) == :integer
        end
61

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

70 71 72 73
        def schema_limit(column)
          limit = column.limit unless column.bigint?
          limit.inspect if limit && limit != native_database_types[column.type][:limit]
        end
74

75 76 77
        def schema_precision(column)
          column.precision.inspect if column.precision
        end
78

79 80 81
        def schema_scale(column)
          column.scale.inspect if column.scale
        end
82

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

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

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