Created
February 21, 2012 01:36
-
-
Save cherring/1872841 to your computer and use it in GitHub Desktop.
Set sequences with a rake task
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
| 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