Created
December 5, 2022 18:57
-
-
Save flozano/5d560689c8373e3e50ff5fb421d63ff4 to your computer and use it in GitHub Desktop.
[Defer all foreign keys and validations in RoR after dumping seed]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Assuming 'base.rb' contains a seed dump (https://github.com/rroblak/seed_dump) | |
# Disable validations | |
ApplicationRecord.class_eval do | |
def valid?(a=1) | |
true | |
end | |
end | |
# Not sure if the transactional code is needed... | |
ActiveRecord::Base.transaction do | |
ActiveRecord::Base.connection.execute('set session_replication_role = replica') | |
load "#{Rails.root}/db/seeds/base.rb" | |
ActiveRecord::Base.connection.execute('set session_replication_role = default') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment