-
Notifications
You must be signed in to change notification settings - Fork 83
Open
Labels
Description
This is more an FYI for others and maybe a feature request.
When you change the column name of a logidze backed model, the history won't change the keys to reflect the new column name (I'm not sure it should). It took me a bit of time to figure out a good way to rewrite history so I thought I'd share.
def convert_keys_in_history(record, old_key, new_key)
record.reload_log_data
updated_data = record.log_data.data.deep_dup
updated_data["h"].each do |elem|
if elem["c"] && elem["c"][old_key]
elem["c"][new_key] = elem["c"].delete(old_key)
end
end
record.update_columns(log_data: updated_data)
end
Post.find_each do |post|
convert_keys_in_history(post, "old_title_column_name", "new_title_column_name")
endOne might expect history to be immutable so it may not be worth adding functionality to make the above simpler.
palkan