title: Catching SQL errors during rails migrations - in order to fail silently
body: <p>I am trying to catch an error while running a rails migration (<code>rake db:migrate</code>) so that it can fail silently.</p>
  
  <p>I want to enable the RDKit extension on my PostgreSQL database, but only if it is available in the PostgreSQL installation. My <code>rescue</code> code is being run, however rake is still aborted. I tried this:</p>
  
  <pre><code>class EnableExtensionRdkit &lt; ActiveRecord::Migration
    def up
      begin
        ActiveRecord::Base.connection.execute("CREATE EXTENSION rdkit;")
      rescue =&gt; error
        p "NO RDKit SUPPORT due to exception: "+error.to_s
    end
    def down
      ActiveRecord::Base.connection.execute("DROP EXTENSION IF EXISTS rdkit;")
    end
  end
  </code></pre>
  
  <p>But I get this error:</p>
  
  <pre><code>$ rake db:migrate
  ==  EnableExtensionRdkit: migrating ===========================================
  NO RDKit SUPPORT due to exception: PG::UndefinedFile: ERROR:  could not access file "$libdir/rdkit": No such file or directory
  : CREATE EXTENSION rdkit;
  ==  EnableExtensionRdkit: migrated (0.0955s) ==================================
  
  rake aborted!
  StandardError: An error has occurred, this and all later migrations canceled:
  
  PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
  :             SELECT attr.attname
              FROM pg_attribute attr
              INNER JOIN pg_constraint cons ON attr.attrelid = cons.conrelid AND attr.attnum = cons.conkey[1]
              WHERE cons.contype = 'p'
                AND cons.conrelid = '"schema_migrations"'::regclass
  /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:774:in `async_exec'
  /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:774:in `exec_no_cache'
  /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
  /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
  /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
  </code></pre>