nick name
unread,Jul 22, 2011, 2:29:20 AM7/22/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to web...@googlegroups.com
Attached you'll find my first go at a database audit module for web2py. It will log every change to a table. The crud versioning system is nice, but it only versions stuff that happens on crud. If you insert/modify records not through crud, you'd need to manually update it -- the attached code will do that automatically for you.
The app I'm developing needs a searchable, browseable audit of all changes to database, for which simple SQL logging is not sufficient.
WARNING: only very lightly tested code, probably doing everything the wrong way. Use at your own risk.
To use: place audit.py in your models directory; then, after defining a table with
mytable = db.define_table('mytable', ....)
call:
with_audit(mytable)
It will create a table called 'audit_mytable', which has in addition to all your fields, 4 more: oid (original id), time, user, and action (insert/update/delete)
any modification to the original table will result in a record being added; inserted records are copied after insertion; updated records are copied after update; deleted records are copied before delete.
KNOWN ISSUES:
* audit table is placed on the same adapter as the audited table - I want to allow the audit to be on a different db/dal/adapter (needs distributed transaction to work properly!)
* old id is always integer for lack of a better type; it should be a "weak" reference type
* reference types are still reference type, so when deleting linked records, "on delete cascade" will remove audit records, this is bad! other integrity constraints (notnull, validators etc.) are not copied to audit table -- only references.
* action type is int, no way to specify efficient char(1) in web2py
* audit happens within same transaction as original update, so you commit or rollback both -- I think that's expected behaviour.
* On first use, it patches the adapter *class*. This means that, unlike usual web2py debugging, if you edit audit.py, you'll have to restart web2py. Writing it now, I can't think of a good reason why I did that rather than patch the adapter *instance* -- but that's the case for the attached code.
A better solution would probably be to add pre/post hooks to insert/update/delete in BaseAdapter, but for now the audit.py approach seems to work on 1.95 - 1.97