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

Ticket #5470: activerecord_ddl_transactions.diff

File activerecord_ddl_transactions.diff, 2.8 kB (added by adamwiggins, 2 years ago)

Version of original patch which does not disrupt behavior for non-DDL-transactional-capable databases

  • activerecord/lib/active_record/connection_adapters/abstract_adapter.rb

    old new  
    4848        true 
    4949      end 
    5050 
     51      # Does this adapter support DDL rollbacks in transactions?  That is, would 
     52      # CREATE TABLE or ALTER TABLE get rolled back by a transaction?  PostgreSQL, 
     53      # SQL Server, and others support this.  MySQL and others do not. 
     54      def supports_ddl_transactions? 
     55        false 
     56      end 
     57       
    5158      # Should primary key values be selected from their corresponding 
    5259      # sequence before the insert statement?  If true, next_sequence_value 
    5360      # is called before each insert to set the record's primary key. 
  • activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

    old new  
    107107        true 
    108108      end 
    109109 
     110      def supports_ddl_transactions? 
     111        true 
     112      end 
     113 
    110114      def table_alias_length 
    111115        63 
    112116      end 
  • activerecord/lib/active_record/migration.rb

    old new  
    331331        next if irrelevant_migration?(version) 
    332332 
    333333        Base.logger.info "Migrating to #{migration_class} (#{version})" 
    334         migration_class.migrate(@direction) 
    335         set_schema_version(version) 
     334 
     335        begin 
     336          ddl_transaction do 
     337            migration_class.migrate(@direction) 
     338            set_schema_version(version) 
     339          end 
     340        rescue => e 
     341          canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : "" 
     342          raise("An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}") 
     343          break unless @direction == :down 
     344        end 
    336345      end 
    337346    end 
    338347 
     
    347356 
    348357        down? ? migrations.sort.reverse : migrations.sort 
    349358      end 
     359 
     360      # Wrap the migration in a transaction only if supported by the adapter. 
     361      def ddl_transaction(&block) 
     362        if Base.connection.supports_ddl_transactions? 
     363          Base.transaction { block.call } 
     364        else 
     365          block.call 
     366        end 
     367      end 
    350368       
    351369      def assert_unique_migration_version(migrations, version) 
    352370        if !migrations.empty? && migrations.transpose.first.include?(version)