Skip to content

Updating history when column names changeΒ #246

@ScotterC

Description

@ScotterC

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")
end

One might expect history to be immutable so it may not be worth adding functionality to make the above simpler.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions