Skip to content

Commit

Permalink
push the mysql add_column up to the abstract adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Mar 22, 2013
1 parent 739a720 commit 2ac300b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class IndexDefinition < Struct.new(:table, :name, :unique, :columns, :lengths, :
# are typically created by methods in TableDefinition, and added to the
# +columns+ attribute of said TableDefinition object, in order to be used
# for generating a number of table creation or table changing SQL statements.
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null) #:nodoc:
class ColumnDefinition < Struct.new(:name, :type, :limit, :precision, :scale, :default, :null, :first, :after) #:nodoc:
def string_to_binary(value)
value
end
Expand Down Expand Up @@ -273,6 +273,8 @@ def new_column_definition(name, type, options) # :nodoc:
column.scale = options[:scale]
column.default = options[:default]
column.null = options[:null]
column.first = options[:first]
column.after = options[:after]
column
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,13 @@ def accept(o)

def visit_AlterTable(o)
sql = "ALTER TABLE #{quote_table_name(o.name)} "
sql << o.adds.map { |col| visit_AddColumn col }.join(' ')
end

o.adds.each do |col|
sql_type = type_to_sql(col.type.to_sym, col.limit, col.precision, col.scale)
sql << "ADD #{quote_column_name(col.name)} #{sql_type}"
add_column_options!(sql, column_options(col))
end

sql
def visit_AddColumn(o)
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale)
sql = "ADD #{quote_column_name(o.name)} #{sql_type}"
add_column_options!(sql, column_options(o))
end

def visit_ColumnDefinition(o)
Expand Down Expand Up @@ -162,6 +161,7 @@ def type_to_sql(type, limit, precision, scale)

def add_column_options!(column_sql, column_options)
@conn.add_column_options! column_sql, column_options
column_sql
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
module ActiveRecord
module ConnectionAdapters
class AbstractMysqlAdapter < AbstractAdapter
class SchemaCreation < AbstractAdapter::SchemaCreation
private

def visit_AddColumn(o)
add_column_position!(super, o)
end

def add_column_position!(sql, column)
if column.first
sql << " FIRST"
elsif column.after
sql << " AFTER #{quote_column_name(column.after)}"
end
sql
end
end

def schema_creation
SchemaCreation.new self
end

class Column < ConnectionAdapters::Column # :nodoc:
attr_reader :collation, :strict

Expand Down Expand Up @@ -459,10 +480,6 @@ def rename_table(table_name, new_name)
rename_table_indexes(table_name, new_name)
end

def add_column(table_name, column_name, type, options = {})
execute("ALTER TABLE #{quote_table_name(table_name)} #{add_column_sql(table_name, column_name, type, options)}")
end

def change_column_default(table_name, column_name, default)
column = column_for(table_name, column_name)
change_column table_name, column_name, column.sql_type, :default => default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ class PostgreSQLAdapter < AbstractAdapter
class SchemaCreation < AbstractAdapter::SchemaCreation
private

def visit_AlterTable(o)
sql = "ALTER TABLE #{quote_table_name(o.name)} "

o.adds.each do |col|
sql_type = type_to_sql(col.type.to_sym, col.limit, col.precision, col.scale)
sql << "ADD COLUMN #{quote_column_name(col.name)} #{sql_type}"
add_column_options!(sql, column_options(col))
end

sql
def visit_AddColumn(o)
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale)
sql = "ADD COLUMN #{quote_column_name(o.name)} #{sql_type}"
add_column_options!(sql, column_options(o))
end

def add_column_options!(sql, options)
Expand Down

0 comments on commit 2ac300b

Please sign in to comment.