diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-03-16 17:04:04 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-03-22 16:22:01 -0700 |
commit | 2ac300ba7cd867c6606b7d7fb458b4a0412c766d (patch) | |
tree | d0620f78b2d0bc7dd180127e48421004d1f4f43b /activerecord/lib | |
parent | 739a72036162b4057ceb4de6950624c0c0ed93b4 (diff) | |
download | rails-2ac300ba7cd867c6606b7d7fb458b4a0412c766d.tar.gz rails-2ac300ba7cd867c6606b7d7fb458b4a0412c766d.tar.bz2 rails-2ac300ba7cd867c6606b7d7fb458b4a0412c766d.zip |
push the mysql add_column up to the abstract adapter
Diffstat (limited to 'activerecord/lib')
4 files changed, 35 insertions, 22 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb index 962c63effd..bd7e5be567 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -15,7 +15,7 @@ module ActiveRecord # 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 @@ module ActiveRecord column.scale = options[:scale] column.default = options[:default] column.null = options[:null] + column.first = options[:first] + column.after = options[:after] column end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 3dc284b4ce..0ebe46be55 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -115,14 +115,13 @@ module ActiveRecord 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 @@ module ActiveRecord def add_column_options!(column_sql, column_options) @conn.add_column_options! column_sql, column_options + column_sql end end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index f88f5742a8..b1baf24cb6 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -3,6 +3,27 @@ require 'arel/visitors/bind_visitor' 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 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb index df2315d0ff..923241b89f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb @@ -4,16 +4,10 @@ module ActiveRecord 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) |