audit-trigger icon indicating copy to clipboard operation
audit-trigger copied to clipboard

auditing changes to tables with hstore columns is not working

Open zam6ak opened this issue 7 years ago • 1 comments

If a table you wish to audit has an hstore column the changes recorded in changed_fields is not tracking properly.

consider a table with hstore column hprops with these initial values

-- add some data
INSERT INTO audit_test VALUES (default, 'apples', 'red', true, 123456789, 2.99, inet_client_addr(), hstore('"x"=>"30", "y"=>"c"'), json_object('{a, 1, b, "def", c, 3.5}'), jsonb_object('{zz, 2, xx, "geh", yy, 99.5}'));

Consider following 3 scenarios

-- case 1) update hstore column (works!)
-- "changed_fields":   ""hprops"=>"\"x\"=>\"30\", \"y\"=>\"fred\"""    -- Works if entire column changed 
UPDATE audit_test SET hprops = hstore('"x"=>"30", "y"=>"fred"')  WHERE id = 1   

-- case 2) add a key or update existing key in hstore column (does not work)
-- "changed_fields":   ""hprops"=>"\"x\"=>\"30\", \"y\"=>\"bob\"""
UPDATE audit_test SET hprops = hprops || hstore('"y"=>"bob"')  WHERE id = 1     

-- case 3) delete a key in hstore  (does not work)
-- "changed_fields":   ""hprops"=>"\"x\"=>\"30\"""
UPDATE audit_test SET hprops = delete(hprops, 'y')  WHERE id = 1  
  • In scenario 2, changed_fields should not contain 'x' key as it has not changed
  • In scenario 3, changed key should be 'y'

zam6ak avatar Jun 14 '17 19:06 zam6ak

Now that I had more time to think about this, this is probably not an issue... The mechanics of how hstore (and json(b) perhaps?) column is updated affects the "whole" colum. The fact that some key in hstore was added/changed/deleted would still result in entire column being updated. If this is correct, then this issue can be closed... Can anyone confirm this?

zam6ak avatar Jun 14 '17 21:06 zam6ak