Module: ArJdbc::MySQL::ColumnExtensions

Included in:
ActiveRecord::ConnectionAdapters::MysqlColumn
Defined in:
lib/arjdbc/mysql/adapter.rb

Instance Method Summary collapse

Instance Method Details

#extract_default(default) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/arjdbc/mysql/adapter.rb', line 22

def extract_default(default)
  if sql_type =~ /blob/i || type == :text
    if default.blank?
      return null ? nil : ''
    else
      raise ArgumentError, "#{type} columns cannot have a default value: #{default.inspect}"
    end
  elsif missing_default_forged_as_empty_string?(default)
    nil
  else
    super
  end
end

#extract_limit(sql_type) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/arjdbc/mysql/adapter.rb', line 51

def extract_limit(sql_type)
  case sql_type
  when /blob|text/i
    case sql_type
    when /tiny/i
      255
    when /medium/i
      16777215
    when /long/i
      2147483647 # mysql only allows 2^31-1, not 2^32-1, somewhat inconsistently with the tiny/medium/normal cases
    else
      nil # we could return 65535 here, but we leave it undecorated by default
    end
  when /^enum/i;     255
  when /^bigint/i;    8
  when /^int/i;       4
  when /^mediumint/i; 3
  when /^smallint/i;  2
  when /^tinyint/i;   1
  when /^(bool|date|float|int|time)/i
    nil
  else
    super
  end
end

#has_default?Boolean

Returns:

  • (Boolean)


36
37
38
39
# File 'lib/arjdbc/mysql/adapter.rb', line 36

def has_default?
  return false if sql_type =~ /blob/i || type == :text #mysql forbids defaults on blob and text columns
  super
end

#missing_default_forged_as_empty_string?(default) ⇒ Boolean

MySQL misreports NOT NULL column default when none is given. We can’t detect this for columns which may have a legitimate ” default (string) but we can for others (integer, datetime, boolean, and the rest).

Test whether the column has default ”, is not null, and is not a type allowing default ”.

Returns:

  • (Boolean)


84
85
86
# File 'lib/arjdbc/mysql/adapter.rb', line 84

def missing_default_forged_as_empty_string?(default)
  type != :string && !null && default == ''
end

#simplified_type(field_type) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/arjdbc/mysql/adapter.rb', line 41

def simplified_type(field_type)
  case field_type
  when /tinyint\(1\)|bit/i then :boolean
  when /enum/i             then :string
  when /year/i             then :integer
  else
    super
  end
end