Skip to content

Instantly share code, notes, and snippets.

@cherring
Created February 21, 2012 01:36
Show Gist options
  • Select an option

  • Save cherring/1872841 to your computer and use it in GitHub Desktop.

Select an option

Save cherring/1872841 to your computer and use it in GitHub Desktop.
Set sequences with a rake task
namespace :db do
desc "Set Sequences to the correct value"
task :set_sequences => [:environment] do
tables = ActiveRecord::Base.connection.tables
tables.reject! { |t| t =~ /schema_migrations/ }
tables.each do |table|
max_id = table.classify.constantize.maximum(:id) || 1
puts "Setting id sequence for #{table} to #{max_id}"
ActiveRecord::Base.connection.execute("select setval('#{table}_id_seq', #{max_id})")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment