Skip to content

Instantly share code, notes, and snippets.

@awongCM
Forked from seyhunak/reset.rb
Created December 15, 2017 07:50
Show Gist options
  • Save awongCM/9e453aa417f7ed8e0f0ce07b507a239e to your computer and use it in GitHub Desktop.
Save awongCM/9e453aa417f7ed8e0f0ce07b507a239e 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