diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-21 13:22:52 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-12-21 13:22:52 -0800 |
commit | 68e91da765565f0c473463b0b47814592dea5de3 (patch) | |
tree | 7b97a7d3ac2788c3acc2da90245208e844621710 /activerecord/lib/active_record/connection_adapters | |
parent | 59ea907a30438a3aa458ef18f0ccb6ceadb8322d (diff) | |
parent | a81845f26864d076970e706863c766aead432672 (diff) | |
download | rails-68e91da765565f0c473463b0b47814592dea5de3.tar.gz rails-68e91da765565f0c473463b0b47814592dea5de3.tar.bz2 rails-68e91da765565f0c473463b0b47814592dea5de3.zip |
Merge pull request #8267 from marcandre/reversible_drop_table_etc
Reversible commands
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters')
3 files changed, 40 insertions, 28 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 73012834c9..b1ec33d06c 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb @@ -423,7 +423,7 @@ module ActiveRecord # t.remove(:qualification) # t.remove(:qualification, :experience) def remove(*column_names) - @base.remove_column(@table_name, *column_names) + @base.remove_columns(@table_name, *column_names) end # Removes the given index from the table. @@ -490,20 +490,8 @@ module ActiveRecord class_eval <<-EOV, __FILE__, __LINE__ + 1 def #{column_type}(*args) # def string(*args) options = args.extract_options! # options = args.extract_options! - column_names = args # column_names = args - type = :'#{column_type}' # type = :string - column_names.each do |name| # column_names.each do |name| - column = ColumnDefinition.new(@base, name.to_s, type) # column = ColumnDefinition.new(@base, name, type) - if options[:limit] # if options[:limit] - column.limit = options[:limit] # column.limit = options[:limit] - elsif native[type].is_a?(Hash) # elsif native[type].is_a?(Hash) - column.limit = native[type][:limit] # column.limit = native[type][:limit] - end # end - column.precision = options[:precision] # column.precision = options[:precision] - column.scale = options[:scale] # column.scale = options[:scale] - column.default = options[:default] # column.default = options[:default] - column.null = options[:null] # column.null = options[:null] - @base.add_column(@table_name, name, column.sql_type, options) # @base.add_column(@table_name, name, column.sql_type, options) + args.each do |name| # column_names.each do |name| + @base.add_column(@table_name, name, :#{column_type}, options) # @base.add_column(@table_name, name, :string, options) end # end end # end EOV 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 f1e42dfbbe..f2d6eb9d27 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -214,6 +214,17 @@ module ActiveRecord end end + # Drops the join table specified by the given arguments. + # See create_join_table for details. + # + # Although this command ignores the block if one is given, it can be helpful + # to provide one in a migration's +change+ method so it can be reverted. + # In that case, the block will be used by create_join_table. + def drop_join_table(table_1, table_2, options = {}) + join_table_name = find_join_table_name(table_1, table_2, options) + drop_table(join_table_name) + end + # A block for changing columns in +table+. # # # change_table() yields a Table instance @@ -294,6 +305,10 @@ module ActiveRecord end # Drops a table from the database. + # + # Although this command ignores +options+ and the block if one is given, it can be helpful + # to provide these in a migration's +change+ method so it can be reverted. + # In that case, +options+ and the block will be used by create_table. def drop_table(table_name, options = {}) execute "DROP TABLE #{quote_table_name(table_name)}" end @@ -306,14 +321,26 @@ module ActiveRecord execute(add_column_sql) end - # Removes the column(s) from the table definition. + # Removes the given columns from the table definition. # - # remove_column(:suppliers, :qualification) # remove_columns(:suppliers, :qualification, :experience) - def remove_column(table_name, *column_names) - columns_for_remove(table_name, *column_names).each {|column_name| execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{column_name}" } + def remove_columns(table_name, *column_names) + raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)") if column_names.empty? + column_names.each do |column_name| + remove_column(table_name, column_name) + end + end + + # Removes the column from the table definition. + # + # remove_column(:suppliers, :qualification) + # + # The +type+ and +options+ parameters will be ignored if present. It can be helpful + # to provide these in a migration's +change+ method so it can be reverted. + # In that case, +type+ and +options+ will be used by add_column. + def remove_column(table_name, column_name, type = nil, options = {}) + execute "ALTER TABLE #{quote_table_name(table_name)} DROP #{quote_column_name{column_name}}" end - alias :remove_columns :remove_column # Changes the column's definition according to the new options. # See TableDefinition#column for details of the options you can use. @@ -662,7 +689,8 @@ module ActiveRecord end def columns_for_remove(table_name, *column_names) - raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.blank? + ActiveSupport::Deprecation.warn("columns_for_remove is deprecated and will be removed in the future") + raise ArgumentError.new("You must specify at least one column name. Example: remove_columns(:people, :first_name)") if column_names.blank? column_names.map {|column_name| quote_column_name(column_name) } end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 8aa5707959..11e8197293 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -444,15 +444,11 @@ module ActiveRecord end end - def remove_column(table_name, *column_names) #:nodoc: - raise ArgumentError.new("You must specify at least one column name. Example: remove_column(:people, :first_name)") if column_names.empty? - column_names.each do |column_name| - alter_table(table_name) do |definition| - definition.columns.delete(definition[column_name]) - end + def remove_column(table_name, column_name, type = nil, options = {}) #:nodoc: + alter_table(table_name) do |definition| + definition.columns.delete(definition[column_name]) end end - alias :remove_columns :remove_column def change_column_default(table_name, column_name, default) #:nodoc: alter_table(table_name) do |definition| |