Skip to content

Instantly share code, notes, and snippets.

@ezza
Last active October 7, 2024 22:56
Show Gist options
  • Save ezza/f541d7eb7484e5a832cb0cc293a6c3f3 to your computer and use it in GitHub Desktop.
Save ezza/f541d7eb7484e5a832cb0cc293a6c3f3 to your computer and use it in GitHub Desktop.
Count how many times each key has changed in your jsonb papertrail storage
# If you use the default table name
PaperTrail::Version.connection.execute(<<-SQL
select k as key, count(*)
from versions v cross join lateral
jsonb_object_keys(v.object_changes) k
group by k order by count(*) desc;
SQL
).to_a
# If your table name is papertrail_versions
PaperTrail::Version.connection.execute(<<-SQL
select k as key, count(*)
from papertrail_versions v cross join lateral
jsonb_object_keys(v.object_changes) k
group by k order by count(*) desc;
SQL
).to_a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment