Skip to content

Instantly share code, notes, and snippets.

@seyhunak
Created February 24, 2014 08:28
Show Gist options
  • Save seyhunak/9183850 to your computer and use it in GitHub Desktop.
Save seyhunak/9183850 to your computer and use it in GitHub Desktop.
Rails - Reset sequence based on ActiveRecord connection adapter
# Interest.destroy_all
case ActiveRecord::Base.connection.adapter_name
when 'SQLite'
update_seq_sql = "update sqlite_sequence set seq = 0 where name = 'interests';"
ActiveRecord::Base.connection.execute(update_seq_sql)
when 'PostgreSQL'
ActiveRecord::Base.connection.reset_pk_sequence!('interests')
when 'Mysql2'
update_seq_sql = "ALTER TABLE interests AUTO_INCREMENT = 1;"
ActiveRecord::Base.connection.execute(update_seq_sql)
puts "Table 'interests' sequence resetted..."
else
raise "Task not implemented for this DB adapter"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment