Last active
February 18, 2021 03:00
-
-
Save patorash/9fee8342168a9712ec3be626a77c78b9 to your computer and use it in GitHub Desktop.
Add column comment from I18n
This file contains hidden or 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
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