Class: ActiveRecord::ConnectionAdapters::MysqlBulkProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_schema_bulk_change/mysql_adapter.rb

Overview

The MysqlBulkProxy will collect changes to a Mysql table definition and execute them all in one SQL statement

Instance Method Summary collapse

Constructor Details

#initialize(table_name, base) ⇒ MysqlBulkProxy

Returns a new instance of MysqlBulkProxy.



10
11
12
13
14
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 10

def initialize(table_name, base)
  @table_name = table_name
  @base = base
  @statements = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object (private)



126
127
128
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 126

def method_missing(symbol, *args)
  @base.send symbol, *args
end

Instance Method Details

#add_column(table_name, column_name, type, options = {}) ⇒ Object



16
17
18
19
20
21
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 16

def add_column(table_name, column_name, type, options = {})
  check_table_name(table_name)
  alter_specification = "ADD #{@base.quote_column_name(column_name)} #{@base.type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  @base.add_column_options!(alter_specification, options)
  add_stmt(alter_specification)
end

#add_index(table_name, column_name, options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 23

def add_index(table_name, column_name, options = {})
  check_table_name(table_name)
  column_names = Array(column_name)
  index_name   = @base.index_name(@table_name, :column => column_names)

  if Hash === options # legacy support, since this param was a string
    index_type = options[:unique] ? "UNIQUE" : ""
    index_name = options[:name] || index_name
  else
    index_type = options
  end
  quoted_column_names = column_names.map { |e| @base.quote_column_name(e) }.join(", ")
  add_stmt("ADD #{index_type} INDEX #{@base.quote_column_name(index_name)}(#{quoted_column_names})")
end

#add_timestamps(table_name) ⇒ Object



38
39
40
41
42
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 38

def add_timestamps(table_name)
  check_table_name(table_name)
  add_column table_name, :created_at, :datetime
  add_column table_name, :updated_at, :datetime
end

#change_column(table_name, column_name, type, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 49

def change_column(table_name, column_name, type, options = {})
  check_table_name(table_name)
  column = @base.column_for(@table_name, column_name)

  unless @base.options_include_default?(options)
    options[:default] = column.default
  end

  unless options.has_key?(:null)
    options[:null] = column.null
  end

  change_column_sql = "CHANGE #{@base.quote_column_name(column_name)} #{@base.quote_column_name(column_name)} #{@base.type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
  @base.add_column_options!(change_column_sql, options)
  add_stmt(change_column_sql)
end

#change_column_default(table_name, column_name, default) ⇒ Object

:nodoc:



66
67
68
69
70
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 66

def change_column_default(table_name, column_name, default) #:nodoc:
  check_table_name(table_name)
  column = @base.column_for(@table_name, column_name)
  change_column(@table_name, column_name, column.sql_type, :default => default)
end

#nativeObject Also known as: native_database_types



111
112
113
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 111

def native
  @base.native_database_types
end

#remove_column(table_name, *column_names) ⇒ Object Also known as: remove_columns



87
88
89
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 87

def remove_column(table_name, *column_names)
  add_stmts(column_names.flatten.map { |column_name| "DROP COLUMN #{@base.quote_column_name(column_name)}"})
end

#remove_index(table_name, options = {}) ⇒ Object



92
93
94
95
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 92

def remove_index(table_name, options = {})
  check_table_name(table_name)
  add_stmt("DROP INDEX #{@base.quote_column_name(@base.index_name(@table_name, options))}")
end

#remove_timestamps(table_name) ⇒ Object



44
45
46
47
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 44

def remove_timestamps(table_name)
  remove_column table_name, :updated_at
  remove_column table_name, :created_at
end

#rename_column(table_name, column_name, new_column_name) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 72

def rename_column(table_name, column_name, new_column_name) #:nodoc:
  check_table_name(table_name)
  options = {}
  if column = @base.columns(table_name).find { |c| c.name == column_name.to_s }
    options[:default] = column.default
    options[:null] = column.null
  else
    raise ActiveRecordError, "No such column: #{table_name}.#{column_name}"
  end
  current_type = @base.select_one("SHOW COLUMNS FROM #{quote_table_name(table_name)} LIKE '#{column_name}'")["Type"]
  rename_column_sql = "CHANGE #{quote_column_name(column_name)} #{quote_column_name(new_column_name)} #{current_type}"
  @base.add_column_options!(rename_column_sql, options)
  add_stmt(rename_column_sql)
end

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



97
98
99
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 97

def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  @base.type_to_sql(type, limit, precision, scale)
end

#update_databaseObject

Commits the updates to the database and clears the cached statements



102
103
104
105
106
107
108
109
# File 'lib/mysql_schema_bulk_change/mysql_adapter.rb', line 102

def update_database
  unless @statements.empty?
    alter_table_sql = "ALTER TABLE #{@base.quote_table_name(@table_name)}"
    result = @base.execute(alter_table_sql + " " + @statements.join(', '))
    @statements.clear
    result
  end
end