我正在尝试在运行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'
答案 0 :(得分:1)
我找到了让它停止失败的方法。我怀疑关键字可能是&#39; transaction&#39;。所以我将disable_ddl_transaction!()
添加到上面的EnableExtensionRdkit
课程中。这解决了问题并给我留下了所需的输出:
$ 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) ==================================
希望有人可以使用它!