aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/abstract
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2013-03-16 16:01:03 -0700
committerAaron Patterson <aaron.patterson@gmail.com>2013-03-22 16:22:00 -0700
commitf20b2f4e54d27f7f98ca537ac69b1be8f88fc2a5 (patch)
tree4e32a1164fd0ed376a8f4d85afc7c390574e4594 /activerecord/lib/active_record/connection_adapters/abstract
parentf84cf418f607c69d3e71fc6e7a161d224fc7fb86 (diff)
downloadrails-f20b2f4e54d27f7f98ca537ac69b1be8f88fc2a5.tar.gz
rails-f20b2f4e54d27f7f98ca537ac69b1be8f88fc2a5.tar.bz2
rails-f20b2f4e54d27f7f98ca537ac69b1be8f88fc2a5.zip
push alter table add column sql in to the schema modification visitor
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/abstract')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb29
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb10
2 files changed, 30 insertions, 9 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 922a544e81..7f0be28080 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -262,12 +262,7 @@ module ActiveRecord
end
alias :belongs_to :references
- private
- def create_column_definition(name, type)
- ColumnDefinition.new name, type
- end
-
- def new_column_definition(name, type, options)
+ def new_column_definition(name, type, options) # :nodoc:
column = create_column_definition name, type
limit = options.fetch(:limit) do
native[type][:limit] if native[type].is_a?(Hash)
@@ -281,6 +276,11 @@ module ActiveRecord
column
end
+ private
+ def create_column_definition(name, type)
+ ColumnDefinition.new name, type
+ end
+
def primary_key_column_name
primary_key_column = columns.detect { |c| c.primary_key? }
primary_key_column && primary_key_column.name
@@ -291,6 +291,23 @@ module ActiveRecord
end
end
+ class AlterTable # :nodoc:
+ attr_reader :add
+
+ def initialize(td)
+ @td = td
+ @add = nil
+ end
+
+ def name; @td.name; end
+
+ def add_column(name, type, options)
+ name = name.to_s
+ type = type.to_sym
+ @add = @td.new_column_definition(name, type, options)
+ end
+ end
+
# Represents an SQL table in an abstract way for updating a table.
# Also see TableDefinition and SchemaStatements#create_table
#
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index 08e8d3ca20..6631a278ab 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -355,9 +355,9 @@ module ActiveRecord
# Adds a new column to the named table.
# See TableDefinition#column for details of the options you can use.
def add_column(table_name, column_name, type, options = {})
- add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
- add_column_options!(add_column_sql, options)
- execute(add_column_sql)
+ at = create_alter_table table_name
+ at.add_column(column_name, type, options)
+ execute schema_creation.accept at
end
# Removes the given columns from the table definition.
@@ -829,6 +829,10 @@ module ActiveRecord
TableDefinition.new native_database_types, name, temporary, options
end
+ def create_alter_table(name)
+ AlterTable.new create_table_definition(name, false, {})
+ end
+
def update_table_definition(table_name, base)
Table.new(table_name, base)
end