Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
Fix deferring constraints when there are none in the database
Browse files Browse the repository at this point in the history
Martin Honermeyer committed Jul 16, 2018
1 parent 0bfcbb3 commit 98fd480
Showing 1 changed file with 1 addition and 3 deletions.
4 changes: 1 addition & 3 deletions lib/serialization_helper.rb
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 98fd480

Please sign in to comment.