Created
January 28, 2022 18:19
-
-
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.
This file contains 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
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