Skip to content

Instantly share code, notes, and snippets.

@joshmvandercom
Created November 16, 2016 20:20
Show Gist options
  • Save joshmvandercom/fff8f00362c21d520aec4abc04ddd06b to your computer and use it in GitHub Desktop.
Save joshmvandercom/fff8f00362c21d520aec4abc04ddd06b to your computer and use it in GitHub Desktop.
Fixing out of sync postgres
task :sync_tables => :environment do
ActiveRecord::Base.connection.tables.each do |table|
unless ['schema_migrations', 'ar_internal_metadata'].include?(table)
sql = "SELECT setval('#{table}_id_seq', COALESCE((SELECT MAX(id)+1 FROM #{table}), 1), false);"
ActiveRecord::Base.connection.execute(sql)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment