Sha256: a769b3a770b52846fecf17cef647757303dd3fc8b7177cc7a75d69dd5e100eb3

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

# The PostgresSQL Adapter's ReferentialIntegrity module can disable and
# re-enable foreign key constraints by disabling all table triggers. Since
# triggers are not available in CockroachDB, we have to remove foreign keys and
# re-add them via the ActiveRecord API.
#
# This module is commonly used to load test fixture data without having to worry
# about the order in which that data is loaded.
module ActiveRecord
  module ConnectionAdapters
    module CockroachDB
      module ReferentialIntegrity
        def disable_referential_integrity
          foreign_keys = tables.map { |table| foreign_keys(table) }.flatten

          foreign_keys.each do |foreign_key|
            remove_foreign_key(foreign_key.from_table, name: foreign_key.options[:name])
          end

          yield

          foreign_keys.each do |foreign_key|
            begin
              add_foreign_key(foreign_key.from_table, foreign_key.to_table, **foreign_key.options)
            rescue ActiveRecord::StatementInvalid => error
              if error.cause.class == PG::DuplicateObject
                # This error is safe to ignore because the yielded caller
                # already re-added the foreign key constraint.
              else
                raise error
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
activerecord-cockroachdb-adapter-6.1.11 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.10 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.9 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.8 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.7 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.6 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.5 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb
activerecord-cockroachdb-adapter-6.1.4 lib/active_record/connection_adapters/cockroachdb/referential_integrity.rb