Last active
February 25, 2022 20:02
-
-
Save joseramonc/664fe7a7357b5806758581eab9fc45b8 to your computer and use it in GitHub Desktop.
Script to reset all tables primary keys for a rails app
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
ActiveRecord::Base.connection.tables.each do |table_name| | |
next if table_name == 'ar_internal_metadata' | |
next unless ActiveRecord::Base.connection.columns(table_name).map(&:name).include?('id') | |
ActiveRecord::Base.connection.execute "SELECT setval('#{table_name}_id_seq', (SELECT MAX(id) FROM #{table_name}));" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment