Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add column comment from I18n
class AddColumnCommentFromI18n < ActiveRecord::Migration[6.0]
def up
table_with_columns.each do |table_name, columns|
columns.map(&:name).each do |column_name|
next unless I18n.exists?("activerecord.attributes.#{table_name.singularize}.#{column_name}")
change_column_comment(
table_name,
column_name,
I18n.t("activerecord.attributes.#{table_name.singularize}.#{column_name}"),
)
end
end
end
def down
table_with_columns.each do |table_name, columns|
columns.map(&:name).each do |column_name|
next unless I18n.exists?("activerecord.attributes.#{table_name.singularize}.#{column_name}")
change_column_comment(
table_name,
column_name,
nil,
)
end
end
end
private
def table_with_columns
system_view_names = %w(
pg_stat_statements
geography_columns
geometry_columns
raster_columns
raster_overviews
)
tables = ApplicationRecord.connection.tables
views = ApplicationRecord.connection.views - system_view_names
(tables + views).each_with_object({}) { |table, hash| hash[table] = ApplicationRecord.connection.columns(table) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment