Created
September 8, 2011 00:12
-
-
Save baburdick/1202236 to your computer and use it in GitHub Desktop.
properly_disable_ri
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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