I'm trying to make a site like Wikipedia or Google Docs, with an audit history of every change made to certain tables. In other words, I want to set a trigger on table foo, so that every time the bar column changes, it adds a "diff" of the changes to a foo_audits table.
By "diff", I mean like the Linux tool diff, git diff, or any similar change tracker, where if the text "foo bar" was changed to "foo qux", the "diff" would be something like:
+ qux
- foo
(Except with the line/column numbers of the change added, using the standard diff format.)
Is there any way to generate such a diff in Postgresql?
P.S. A pure PostgreSQL solution would be nice, but I'd also be open to using another language. For instance, Node JS has a diff package that would do exactly what I want ... but I have no idea how to use it from inside a database trigger.
P.P.S. I found this code, for what looks like a diff function ... but it's not a free-standing "give me two strings and I'll give you a diff" function; it instead INSERTs the diff into a table. If anyone could help explain how I can extract the "diff" part of that script into its own function (sadly I'm not good at programming PostgreSQL), that would also solve things.
Basically I don't care how it works, I'm just looking for any solution that lets me diff inside the database ... and any help would be greatly appreciated.
--with-pythonoption and/or you can install theplpythonuextension (u= untrusted). Never done it - will have to shortly, but if you're proficient in Python, might be worth a shot?