aboutsummaryrefslogtreecommitdiffstats
path: root/lib/patches/fixtures_constraint_disabling.rb
blob: 7d97e81f7d1afc10fcf2ab895b67e4b149ee96b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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