Class: Utf8mb4rails::Migrator::ColumnInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/utf8mb4rails/migrator/column_info.rb

Overview

Abstraction for dealing with column information received from the database

Constant Summary collapse

TEXT_TYPES =
%w(CHAR VARCHAR TINYTEXT TEXT MEDIUMTEXT LONGTEXT).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(info) ⇒ ColumnInfo

Receives a hash with (:type, :default, :max_length, :charset)



11
12
13
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 11

def initialize(info)
  @info = info
end

Instance Attribute Details

#infoObject (readonly)

Returns the value of attribute info.



8
9
10
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 8

def info
  @info
end

Instance Method Details

#default_value_for_sqlObject

Returns String : the sql part of the default value of the column definition.

Returns:

  • String : the sql part of the default value of the column definition



29
30
31
32
33
34
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 29

def default_value_for_sql
  info_default = info[:default]
  return nil unless info_default

  "default '#{info_default}'"
end

#new_type_for_sqlObject

Returns String : the sql part of the new type of the column definition.

Returns:

  • String : the sql part of the new type of the column definition



21
22
23
24
25
26
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 21

def new_type_for_sql
  info_type = info[:type]
  return "#{info_type}(#{info[:max_length]})" if info_type =~ /CHAR/

  info_type
end

#text_column?Boolean

Returns Bool : True if the column is of a text type.

Returns:

  • (Boolean)

    Bool : True if the column is of a text type



37
38
39
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 37

def text_column?
  TEXT_TYPES.include?(info[:type])
end

#utf8mb4?Boolean

Returns Bool : True if the column is in utf8mb4.

Returns:

  • (Boolean)

    Bool : True if the column is in utf8mb4



16
17
18
# File 'lib/utf8mb4rails/migrator/column_info.rb', line 16

def utf8mb4?
  info[:charset] =~ /utf8mb4/
end