Sha256: 4843b13cb8442cb9177800fae651a842655cb084709f5a25c8a3ef77470a1256

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

# 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

ActiveRecord::Base

class ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter < ::ActiveRecord::ConnectionAdapters::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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails3_pg_deferred_constraints-0.1.0 lib/rails3_pg_deferred_constraints/postgresql_adapter.rb
rails3_pg_deferred_constraints-0.0.4 lib/rails3_pg_deferred_constraints/postgresql_adapter.rb
rails3_pg_deferred_constraints-0.0.3 lib/rails3_pg_deferred_constraints/postgresql_adapter.rb
rails3_pg_deferred_constraints-0.0.2 lib/rails3_pg_deferred_constraints/postgresql_adapter.rb