# frozen_string_literal: true ############################################################################## # Foreign Key Plugin ############################################################################## RSpec.configure do |config| config.around(:each, :disable_foreign_keys => lambda { |v| !!v }) do |example| options = example.metadata[:disable_foreign_keys] options = case options when Array { :tables => options.map(&:to_s) } when String, Symbol { :tables => [options.to_s] } when FalseClass { :tables => [] } when TrueClass { :tables => :all } end foreign_key_names_sql = <<~HEREDOC SELECT "constraint_column_usage"."constraint_name", "pg_class"."relname" AS "local_table_name", "constraint_column_usage"."table_name" AS "foreign_table_name", "constraint_column_usage"."column_name" AS "foreign_column_name" FROM "information_schema"."constraint_column_usage" INNER JOIN "pg_constraint" ON "constraint_column_usage"."constraint_name" = "pg_constraint"."conname" INNER JOIN "pg_class" ON "pg_class"."oid" = "pg_constraint"."conrelid" WHERE "pg_constraint"."contype" = 'f' AND "pg_constraint"."condeferrable" = 't' HEREDOC unless options[:tables] == :all foreign_key_names_sql += 'AND (' table_constraint_sql = options[:tables].map do |table_name| %Q{"pg_class"."relname" = '#{table_name}'} end foreign_key_names_sql += table_constraint_sql.join(' OR ') foreign_key_names_sql += ')' end foreign_keys_to_disable = ActiveRecord::Base.connection.select_values(foreign_key_names_sql).join(',') ActiveRecord::Base.connection.execute('SET CONSTRAINTS %s DEFERRED' % foreign_keys_to_disable) example.run end end