module ActiveRecord::ConnectionAdapters::PostgreSQL::ReferentialIntegrity

def all_foreign_keys_valid? # :nodoc:

:nodoc:
def all_foreign_keys_valid? # :nodoc:
  sql = <<~SQL
    do $$
      declare r record;
    BEGIN
    FOR r IN (
      SELECT FORMAT(
        'UPDATE pg_constraint SET convalidated=false WHERE conname = ''%I'' AND connamespace::regnamespace = ''%I''::regnamespace; ALTER TABLE %I.%I VALIDATE CONSTRAINT %I;',
        constraint_name,
        table_schema,
        table_schema,
        table_name,
        constraint_name
      ) AS constraint_check
      FROM information_schema.table_constraints WHERE constraint_type = 'FOREIGN KEY'
    )
      LOOP
        EXECUTE (r.constraint_check);
      END LOOP;
    END;
    $$;
  SQL
  begin
    transaction(requires_new: true) do
      execute(sql)
    end
    true
  rescue ActiveRecord::StatementInvalid
    false
  end
end

def disable_referential_integrity # :nodoc:

:nodoc:
:nodoc:
def disable_referential_integrity # :nodoc:
  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.
most likely caused due to missing permissions.
eds superuser privileges to disable referential integrity.
e: #{original_exception&.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
end