Sha256: 8c04d645f64e14b7b92150ffed0bb7d0f5e1ae9bd0f82a4767167399523f3032

Contents?: true

Size: 1.41 KB

Versions: 54

Compression:

Stored size: 1.41 KB

Contents

module ActiveRecord
  module ConnectionAdapters
    module PostgreSQL
      module ReferentialIntegrity # :nodoc:
        def supports_disable_referential_integrity? # :nodoc:
          true
        end

        def disable_referential_integrity # :nodoc:
          if supports_disable_referential_integrity?
            original_exception = nil

            begin
              transaction(requires_new: true) do
                execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";"))
              end
            rescue ActiveRecord::ActiveRecordError => e
              original_exception = e
            end

            begin
              yield
            rescue ActiveRecord::InvalidForeignKey => e
              warn <<-WARNING
WARNING: Rails was not able to disable referential integrity.

This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.

    cause: #{original_exception.try(:message)}

              WARNING
              raise e
            end

            begin
              transaction(requires_new: true) do
                execute(tables.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";"))
              end
            rescue ActiveRecord::ActiveRecordError
            end
          else
            yield
          end
        end
      end
    end
  end
end

Version data entries

54 entries across 54 versions & 4 rubygems

Version Path
activerecord-5.1.7 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.7.rc1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.6.2 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.7.2 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.6.1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.7.1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.6 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.7 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.5 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.5.rc1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.4 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.6 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.4.rc1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.6.rc1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.3 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.3.rc3 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.5 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.5.rc2 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.1.3.rc2 lib/active_record/connection_adapters/postgresql/referential_integrity.rb
activerecord-5.0.5.rc1 lib/active_record/connection_adapters/postgresql/referential_integrity.rb