diff options
-rw-r--r-- | config/environments/test.rb | 2 | ||||
-rw-r--r-- | lib/patches/fixtures_constraint_disabling.rb | 21 |
2 files changed, 23 insertions, 0 deletions
diff --git a/config/environments/test.rb b/config/environments/test.rb index 8d6041ad9..be28c3df6 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,5 +1,7 @@ # Settings specified here will take precedence over those in config/environment.rb +require 'lib/patches/fixtures_constraint_disabling' + # The test environment is used exclusively to run your application's # test suite. You never need to work with it otherwise. Remember that # your test database is "scratch space" for the test suite and is wiped diff --git a/lib/patches/fixtures_constraint_disabling.rb b/lib/patches/fixtures_constraint_disabling.rb new file mode 100644 index 000000000..7d97e81f7 --- /dev/null +++ b/lib/patches/fixtures_constraint_disabling.rb @@ -0,0 +1,21 @@ +# An alternative way of disabling foreign keys in fixture loading in Postgres and +# does not require superuser permissions +# http://kopongo.com/2008/7/25/postgres-ri_constrainttrigger-error +require 'active_record/connection_adapters/postgresql_adapter' +module ActiveRecord + module ConnectionAdapters + class PostgreSQLAdapter < AbstractAdapter + def disable_referential_integrity(&block) + transaction { + begin + execute "SET CONSTRAINTS ALL DEFERRED" + yield + ensure + execute "SET CONSTRAINTS ALL IMMEDIATE" + end + } + end + end + end +end + |