Class: ActiveRecord::ConnectionAdapters::MysqlAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/gold_record/connection_adapters/mysql_adapter.rb

Instance Method Summary collapse

Instance Method Details

#change_integer_primary_key_to_uuid(table, column = :id) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/gold_record/connection_adapters/mysql_adapter.rb', line 25

def change_integer_primary_key_to_uuid(table, column = :id)
  table = table.to_s
  column = column.to_s
  execute "ALTER TABLE #{table} ADD COLUMN #{column}_new BINARY(16) FIRST"
  execute "UPDATE #{table} SET #{column}_new = #{column}"
  execute "ALTER TABLE #{table} DROP COLUMN #{column}"
  execute "ALTER TABLE #{table} CHANGE COLUMN #{column}_new #{column} BINARY(16) FIRST"
end

#change_integer_to_uuid(table, column = :id, primary_key = :id) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/gold_record/connection_adapters/mysql_adapter.rb', line 44

def change_integer_to_uuid(table, column = :id, primary_key = :id)
  table = table.to_s
  column = column.to_s
  after_clause = primary_key ? "AFTER #{primary_key}" : ""
  execute "ALTER TABLE #{table} ADD COLUMN #{column}_new BINARY(16)"
  execute "UPDATE #{table} SET #{column}_new = #{column}"
  execute "ALTER TABLE #{table} DROP COLUMN #{column}"
  execute "ALTER TABLE #{table} CHANGE COLUMN #{column}_new #{column} BINARY(16) #{after_clause}"
end

#change_uuid_to_integer(table, column = :id, primary_key = :id) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/gold_record/connection_adapters/mysql_adapter.rb', line 54

def change_uuid_to_integer(table, column = :id, primary_key = :id)
  table = table.to_s
  column = column.to_s
  after_clause = primary_key ? "AFTER #{primary_key}" : ""
  execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column}_old BINARY(16)"
  execute "ALTER TABLE #{table} ADD COLUMN #{column} int(11) #{after_clause}"
  execute "UPDATE #{table} SET #{column} = #{column}_old"
  execute "ALTER TABLE #{table} DROP COLUMN #{column}_old"
end

#change_uuid_to_integer_primary_key(table, column = :id) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/gold_record/connection_adapters/mysql_adapter.rb', line 34

def change_uuid_to_integer_primary_key(table, column = :id)
  table = table.to_s
  column = column.to_s
  execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column}_old BINARY(16)"
  execute "ALTER TABLE #{table} ADD COLUMN #{column} int(11)"
  execute "UPDATE #{table} SET #{column} = #{column}_old"
  execute "ALTER TABLE #{table} DROP COLUMN #{column}_old"
  execute "ALTER TABLE #{table} CHANGE COLUMN #{column} #{column} #{NATIVE_DATABASE_TYPES[:primary_key]} FIRST"
end

#type_to_sql(type, limit = nil, precision = nil, scale = nil) ⇒ Object

MySQL can store UUIDs in binary(16) instead of a blob column.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gold_record/connection_adapters/mysql_adapter.rb', line 8

def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  # New functionality to handle limit on binary columns
  return "binary(#{limit})" if type.to_s == 'binary' && !limit.blank?

  # Exact implementation from ActiveRecord 2.3.4
  return super unless type.to_s == 'integer'

  case limit
  when 1; 'tinyint'
  when 2; 'smallint'
  when 3; 'mediumint'
  when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
  when 5..8; 'bigint'
  else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  end
end