我试图在运行 rails 迁移 ( rake db:migrate
) 时捕获错误,以便它可以静默失败。
我想在我的 PostgreSQL 数据库上启用 RDKit 扩展,但前提是它在 PostgreSQL 安装中可用。我的rescue
代码正在运行,但是 rake 仍然中止。我试过这个:
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
但我得到这个错误:
$ 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'