Skip to content

Instantly share code, notes, and snippets.

@baburdick
Created September 8, 2011 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baburdick/1202236 to your computer and use it in GitHub Desktop.
Save baburdick/1202236 to your computer and use it in GitHub Desktop.
properly_disable_ri
# Hack to disable_referential_integrity for postgres users with sane
# permissions, uses deferral of constraints instead of disablement of the
# superuser-owned per-table triggers. Allows testing with fixtures which
# otherwise produce RI_ConstraintTrigger Errors.
#
# source: http://kopongo.com/2008/7/25/postgres-ri_constrainttrigger-error
# ref: https://github.com/matthuhiggins/foreigner/issues/61
module ActiveRecord
module ConnectionAdapters
class PostgreSQLAdapter < AbstractAdapter
alias_method :supports_deferring_all_constraints?, :supports_disable_referential_integrity?
def defer_all_constraints(&block)
return unless supports_deferring_all_constraints?
transaction do
begin
execute "SET CONSTRAINTS ALL DEFERRED"
yield
ensure
execute "SET CONSTRAINTS ALL IMMEDIATE"
end
end
end
alias_method :disable_triggers_on_each_table, :disable_referential_integrity
alias_method :disable_referential_integrity, :defer_all_constraints
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment