From 98fd480e41ec3432332fc9d0064eed425666c433 Mon Sep 17 00:00:00 2001 From: Martin Honermeyer Date: Mon, 16 Jul 2018 12:59:06 +0200 Subject: [PATCH] Fix deferring constraints when there are none in the database --- lib/serialization_helper.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/serialization_helper.rb b/lib/serialization_helper.rb index 1aebb7d..7dace06 100644 --- a/lib/serialization_helper.rb +++ b/lib/serialization_helper.rb @@ -125,17 +125,15 @@ def self.defer_fk_constraints(&block) case database_type when :postgresql # make all fk constraints deferrable - fk_constraints = [] tables.each do |table| fk_constraints_on_table = ActiveRecord::Base.connection.foreign_keys(table) fk_constraints_on_table.each do |fk_constraint| quoted_table_name = SerializationHelper::Utils.quote_table(table) ActiveRecord::Base.connection.execute("ALTER TABLE #{quoted_table_name} ALTER CONSTRAINT #{fk_constraint.name} DEFERRABLE INITIALLY IMMEDIATE") end - fk_constraints += fk_constraints_on_table end # defer all fk constraints - ActiveRecord::Base.connection.execute("SET CONSTRAINTS #{fk_constraints.collect(&:name).join(',')} DEFERRED") + ActiveRecord::Base.connection.execute("SET CONSTRAINTS ALL DEFERRED") yield block when :mysql ActiveRecord::Base.connection.execute("SET foreign_key_checks = 0")