schema_dumper.rb 1.7 KB
Newer Older
1 2 3 4 5 6
module ActiveRecord
  module ConnectionAdapters
    module MySQL
      module ColumnDumper
        def column_spec_for_primary_key(column)
          spec = {}
7 8 9 10 11
          if column.bigint?
            spec[:id] = ':bigint'
            spec[:default] = schema_default(column) || 'nil' unless column.auto_increment?
            spec[:unsigned] = 'true' if column.unsigned?
          elsif column.auto_increment?
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
            spec[:unsigned] = 'true' if column.unsigned?
            return if spec.empty?
          else
            spec[:id] = column.type.inspect
            spec.merge!(prepare_column_options(column).delete_if { |key, _| [:name, :type, :null].include?(key) })
          end
          spec
        end

        def prepare_column_options(column)
          spec = super
          spec[:unsigned] = 'true' if column.unsigned?
          spec
        end

        def migration_keys
          super + [:unsigned]
        end

        private

33 34 35 36 37 38 39 40
        def schema_type(column)
          if column.sql_type == 'tinyblob'
            'blob'
          else
            super
          end
        end

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        def schema_limit(column)
          super unless column.type == :boolean
        end

        def schema_precision(column)
          super unless /time/ === column.sql_type && column.precision == 0
        end

        def schema_collation(column)
          if column.collation && table_name = column.instance_variable_get(:@table_name)
            @table_collation_cache ||= {}
            @table_collation_cache[table_name] ||= select_one("SHOW TABLE STATUS LIKE '#{table_name}'")["Collation"]
            column.collation.inspect if column.collation != @table_collation_cache[table_name]
          end
        end
      end
    end
  end
end