Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6956: change_column_default_to_null.diff

File change_column_default_to_null.diff, 5.4 kB (added by dcmanges, 2 years ago)

includes tests

  • test/migration_test.rb

    old new  
    433433      Person.reset_column_information 
    434434      assert !Person.new.administrator? 
    435435    end 
     436     
     437    def test_change_column_with_nil_default 
     438      Person.connection.add_column "people", "contributor", :boolean, :default => true 
     439      Person.reset_column_information 
     440      assert Person.new.contributor? 
     441       
     442      assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil } 
     443      Person.reset_column_information 
     444      assert !Person.new.contributor? 
     445      assert_nil Person.new.contributor 
     446    end 
    436447 
    437448    def test_add_table 
    438449      assert !Reminder.table_exists? 
  • lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    276276      end             
    277277     
    278278      def add_column_options!(sql, options) #:nodoc: 
    279         sql << " DEFAULT #{quote(options[:default], options[:column])}" unless options[:default].nil? 
     279        sql << " DEFAULT #{quote(options[:default], options[:column])}" if options.include?(:default) && !(options[:default].nil? && options[:null] == false) 
    280280        sql << " NOT NULL" if options[:null] == false 
    281281      end 
    282282 
  • lib/active_record/connection_adapters/sqlite_adapter.rb

    old new  
    267267          definition[column_name].instance_eval do 
    268268            self.type    = type 
    269269            self.limit   = options[:limit] if options[:limit] 
    270             self.default = options[:default] unless options[:default].nil? 
     270            self.default = options[:default] if options.include?(:default) && !(options[:default].nil? && options[:null] == false) 
    271271          end 
    272272        end 
    273273      end 
  • lib/active_record/connection_adapters/frontbase_adapter.rb

    old new  
    800800            default_value = options[:default] == 0 ? quoted_false : quoted_true 
    801801          end 
    802802        end 
    803         sql << " DEFAULT #{default_value}" unless options[:default].nil? 
     803        sql << " DEFAULT #{default_value}" if options.include?(:default) && !(options[:default].nil? && options[:null] == false) 
    804804        sql << " NOT NULL" if options[:null] == false 
    805805      end 
    806806 
  • lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    345345          rename_column(table_name, "#{column_name}_ar_tmp", column_name) 
    346346          commit_db_transaction 
    347347        end 
    348         change_column_default(table_name, column_name, options[:default]) unless options[:default].nil? 
     348        change_column_default(table_name, column_name, options[:default]) if options.include?(:default) && !(options[:default].nil? && options[:null] == false) 
    349349      end 
    350350 
    351351      def change_column_default(table_name, column_name, default) #:nodoc: 
    352         execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT '#{default}'
     352        execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}
    353353      end 
    354354 
    355355      def rename_column(table_name, column_name, new_column_name) #:nodoc: 
  • lib/active_record/connection_adapters/mysql_adapter.rb

    old new  
    364364      end 
    365365 
    366366      def change_column(table_name, column_name, type, options = {}) #:nodoc: 
    367         if options[:default].nil? 
     367        unless options.include?(:default) 
    368368          options[:default] = select_one("SHOW COLUMNS FROM #{table_name} LIKE '#{column_name}'")["Default"] 
    369369        end 
    370370         
  • lib/active_record/connection_adapters/sqlserver_adapter.rb

    old new  
    458458       
    459459      def change_column(table_name, column_name, type, options = {}) #:nodoc: 
    460460        sql_commands = ["ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"] 
    461         unless options[:default].nil? 
     461        if options.include?(:default) 
    462462          remove_default_constraint(table_name, column_name) 
    463463          sql_commands << "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{quote(options[:default])} FOR #{column_name}" 
    464464        end