Package: bitsmiths-audit
BITSMITHS AUDITThis is the second tier base/core Bitsmiths python library. The purpose of this product is provide field level row
auditing for any tables or objects within a product.This auditing product is used by our own open-source products as well as commercial products which require detailed
auditing. The nice thing is, that if you are using any Bitsmiths products down the line, then you already have a
working auditing solution within your project that is ready to go.Notethat we package the generated Mettle code in this package, which means that the Mettle version required in this module is important.Tables & SetupAuditis designed to use a relational database with a schema/namespace ofaudit. You will need to create this database schema yourself.
It also requires four relational database tables.The package provides the SQL code to create these tables. There are two ways to access the table creation SQL.You can runbs-audit -g postgresqlat the command line, this will print all the SQL tostdout.You can import the bs_audit module and get the SQL as shown below:importbs_auditprint(bs_audit.get_table_sql('postgresql'))Note!Currently onlypostgresqlSQL is added to the package. If you want us to support another database let
us know and we will put it into the development pipeline.Library ObjectsThese are the objects in the audit product with a short description of their intended purpose.AuditTheAuditclass (as well as it'sAuditAsynccounter part forasynccode) is the method in which one uses
the audit product. Each time you make an insert, update, or delete to a table, you can choose to audit that table
using this class.There are a couple of presumptions that theAuditobject makes:That you are auditing an object that inherits frommettle.io.ISerializable. This is because audit will use the_deserialize()method to read the objects fields.That the name of the auditing object has been entered into theaudit.cfgtable.That you are also using the auditing object from within some kind ofAutoTransaction - see bitsmiths-lib.AutoTransaction.Below is an example of intended use:We assume the Pod object contains an instance of theAuditobject.We assume the Pod object contains an instance of a database connection.We assume thebrecis a mettle table record of some kind.We assume the user identifier has been configured in the pod, and theAudit.cfgtable has been configured to use themodified_bycolumn.importcopyfrommettle.libimportAutoTransactiondefsave_bank_record(self,brec:tBank):withAutoTransaction(self.pod.audit,self.pod)asat:bank_tbl=self.pod.dao.dBank(self.pod.dbcon)brec.modified_by=self.pod.usr_idifbank_tbl.try_select_one_deft(brec.id):orig=copy.copy(bank_tbl.rec)bank_tbl.update(brec)self.pod.audit.diff(orig,bank_tbl.rec)# audits the difference between the original and new recordelse:bank_tbl.insert(brec)self.pod.audit.diff(None,bank_tbl.rec)# audits a brand new record.at.commit()TableWatchThis is a simply object that is designed to check on an interval for table changes. You may want to implement to update
internal caches or something if a table has row changes. Assumes that you are using theAuditobjects whenever you
do insert, update, or deletes to table rows else it won't know.Change History2.2.2TypeDescriptionFixFixed asqlerror on the postgres trigger script.2.2.1TypeDescriptionNewUpgraded to usebitsmiths-mettleversion 2.2.32.2.0TypeDescriptionBreakingUpgraded to usebitsmiths-mettleversion 2.2.22.1.6TypeDescriptionNewImplemented auditing auto triggersNewUsing the new python fetch method frombitsmiths-mettle(2.1.14)BreakingTable changes and new triggers required on the database (seeversion_2_1_6_upgrade.postgresql.sqlin the repo)NewImplemented a new function to read audit trails easier.Example of audit trail query/function:select*fromaudit.show_trail('{"config"}',null,'{"value"}','2022-05-01',null,'{"U"}');2.1.5TypeDescriptionNewImplemented the dataclass feature from the latest mettle (2.1.13) version.2.1.4TypeDescriptionBugFixed a regression bug in base_audit.search method that was raising SQL exceptions.2.1.3TypeDescriptionBugForced the truncation of long column values when doing auditing.BugAuditing of column values now works with derived dictionary and list types.2.1.0TypeDescriptionBreakingRename from triceraclops to auditBreakingMoved the dao import method from util.py to theinit.py.NewPulled in the mettle via PYPI, updated project to cater for new changes in mettle.NewYou can now read the database creation from the package, also added an entry point for this.