Sha256: 887c274bef01fc0a284e05a8c3e2bbda094d561a6e39ad666e0e701295c51018
Contents?: true
Size: 796 Bytes
Versions: 95
Compression:
Stored size: 796 Bytes
Contents
namespace :caboose do desc "Corrects any sequences in tables" task :correct_sequences => :environment do c = ActiveRecord::Base.connection c.tables.each do |tbl| next if !c.column_exists? tbl, :id rows = c.execute("select max(id) from #{tbl}") max = rows[0]['max'] max = max.to_i if !max.nil? rows = c.execute("select nextval('#{tbl}_id_seq')") nextval = rows[0]['nextval'] nextval = nextval.to_i if !nextval.nil? next if max.nil? || nextval.nil? next if nextval >= max # If nextval is lower than the max id, then fix it. puts "Correcting sequence for #{tbl}..." c.execute("select setval('#{tbl}_id_seq', (select max(id) from #{tbl}))") end end end
Version data entries
95 entries across 95 versions & 1 rubygems