In an extension, what is the best way to create a DB table that has a matching logging table? Do we just have to literally create a matching new table with triggers?
There must be a better way?
CiviCRM Stack Exchange is a question and answer site for administrators and users of the CiviCRM Constituent Relationship Management software. It only takes a minute to sign up.
Sign up to join this communityI happen to be doing this in an extension at the moment... I had the table created normally and stuck
// Make sure the log table is created if required
$schema = new CRM_Logging_Schema();
$schema->fixSchemaDifferences();
at the end of the civicrm_enable hook. Unless I take care of this, if I install the extension with logging already enabled and try to create one of my new entity with drush right away, I get an error about the log table not existing.
Per Lobo's comment - the logging triggers & tables are automatically created. But, from experience sometimes you have to turn logging off & on to get the tables created. The triggers seem to create & cause lots of errors which stop when you turn logging off & on.
Obviously the function called by turning logging on is more thorough than the enable extension function
All tables that start with "civicrm_" are logged. tables with _temp and _cache in their names are skipped (and a few other tables).
You can check the code at CRM/Logging/Schema.php
Might be good to consider using hook(s) in multiple places so extensions can add to the list of tables to be logged and excluded