Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

push the mysql add_column up to the abstract adapter

  • Loading branch information...
commit 2ac300ba7cd867c6606b7d7fb458b4a0412c766d 1 parent 739a720
Aaron Patterson tenderlove authored
4 activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
View
@@ -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
@@ -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
14 activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
View
@@ -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)
@@ -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
25 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
View
@@ -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
@@ -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
14 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
View
@@ -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)
Please sign in to comment.
Something went wrong with that request. Please try again.