Skip to content

Instantly share code, notes, and snippets.

@ollietreend
Created January 28, 2022 18:19
Show Gist options
  • Save ollietreend/8705c528713e9e5521cae3b17b85a9e3 to your computer and use it in GitHub Desktop.
Save ollietreend/8705c528713e9e5521cae3b17b85a9e3 to your computer and use it in GitHub Desktop.
Find the records with the longest TEXT columns in a Rails + MySQL app's database tables.
ActiveRecord::Base.connection.tables.map { |table|
text_columns = ActiveRecord::Base.connection.columns(table).select { |c| c.type == :text }
text_columns.map { |col|
longest_record = ActiveRecord::Base.connection.exec_query("SELECT id, LENGTH(#{col.name}) as length FROM #{table} ORDER BY length DESC LIMIT 1").rows.first
{
table: table,
column: col.name,
record_id: longest_record.first,
length: longest_record.last,
}
}
}.reject(&:empty?).flatten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment