Skip to content

Instantly share code, notes, and snippets.

View borama's full-sized avatar
🥕
Ready for some carrot

Matouš Borák borama

🥕
Ready for some carrot
View GitHub Profile
@borama
borama / check_auto_increment_columns_rake.rb
Last active November 9, 2018 09:22
Rake task that warns you when an auto_increment column reaches a maximum value threshold
desc "Warn if an auto_increment column is reaching max value limit"
task check_auto_increment_columns_overflow: :environment do
threshold_percent = (ENV["THRESHOLD"] || "90").to_i
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.columns(table).each do |column|
next unless column.type == :integer && column.extra.to_s =~ /auto_increment/
auto_increment = ActiveRecord::Base.connection.select_one("SHOW TABLE STATUS LIKE '#{table}'")["Auto_increment"]
max_value = column.cast_type.send(:max_value)