If you are switching from the default YAML serializer to PostgreSQL JSON/JSONB, you must migrate existing data.
Option 1: Direct Migration (Slow but safe)
Loop through records and update them using YAML.load into a temporary column, then rename the columns.
add_column :versions, :new_object, :jsonb
PaperTrail::Version.where.not(object: nil).find_each do |version|
version.update_column(:new_object, YAML.load(version.object))
end
remove_column :versions, :object
rename_column :versions, :new_object, :object
Option 2: Background Migration (Faster for large datasets)
- Rename the existing
object column to old_object. - Add a new
object column with type jsonb. - Use a background script to convert records from
old_object (YAML) to object (JSON). - Remove
old_object once complete.
# Background script example
PaperTrail::Version.where.not(old_object: nil).find_each do |version|
version.update_columns old_object: nil, object: YAML.load(version.old_object)
end