Skip to content

Instantly share code, notes, and snippets.

@patorash
Last active February 18, 2021 03:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save patorash/9fee8342168a9712ec3be626a77c78b9 to your computer and use it in GitHub Desktop.
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