Reputation: 832
I am trying to catch an error while running a rails migration (rake db:migrate
) so that it can fail silently.
I want to enable the RDKit extension on my PostgreSQL database, but only if it is available in the PostgreSQL installation. My rescue
code is being run, however rake is still aborted. I tried this:
class EnableExtensionRdkit < ActiveRecord::Migration
def up
begin
ActiveRecord::Base.connection.execute("CREATE EXTENSION rdkit;")
rescue => 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
But I get this error:
$ 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'
Upvotes: 1
Views: 1303
Reputation: 832
I found a way to make it stop failing. I suspected that the keyword could be 'transaction'. So I added the disable_ddl_transaction!()
to the above EnableExtensionRdkit
class. This fixed the problem and left me with the desired output:
$ 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.0875s) ==================================
Hope someone can use this!
Upvotes: 2