Module: ActiveRecord::ConnectionAdapters::PostgreSQL::ReferentialIntegrity

Included in:
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
Defined in:
lib/active_record/connection_adapters/postgresql/referential_integrity.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#disable_referential_integrityObject

:nodoc:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_record/connection_adapters/postgresql/referential_integrity.rb', line 9

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: Rails was not able to disable referential integrity.\n\nThis is most likely caused due to missing permissions.\nRails needs superuser privileges to disable referential integrity.\n\n    cause: \#{original_exception.try(:message)}\n\n      WARNING\n      raise e\n    end\n\n    begin\n      transaction(requires_new: true) do\n        execute(tables.collect { |name| \"ALTER TABLE \#{quote_table_name(name)} ENABLE TRIGGER ALL\" }.join(\";\"))\n      end\n    rescue ActiveRecord::ActiveRecordError\n    end\n  else\n    yield\n  end\nend\n"

#supports_disable_referential_integrity?Boolean

:nodoc:

Returns:

  • (Boolean)


5
6
7
# File 'lib/active_record/connection_adapters/postgresql/referential_integrity.rb', line 5

def supports_disable_referential_integrity? # :nodoc:
  true
end