rails/rails — pull request #8498 by garysweaver (2012-12-12 18:30:23+00:00)
Title: postgres disable_referential_integrity fix for running as non-admin

Was unable to use `database_cleaner` in CI environment without this change. Right now Rails 3.2.9 and master both just try to disable all triggers including the RI_ConstraintTriggers which it cannot, then it tries to enable the RI_ConstraintTriggers in the ensure, which also fails. With this change it will have more executes in the disabling while it attempts to disable trigger all, falling back to disable trigger user, and then will only undo what it did if it fails. Just as it was before, this will still enable disabled triggers that could be checked via the pg_trigger table, but that is a much more difficult problem, and this solution works in the originally intended manner, albeit with more executes that could slow it down. The alternative would be to use pg_trigger table to find out what triggers can and need to be disabled, but that is also more complex, so the nasty catch-the-error method is an interim workaround for simplicity.