TechnologyRediscovery / codenforce

municipal code enforcement database application
GNU General Public License v3.0
2 stars 3 forks source link

Database last updated triggers #223

Open edarsow opened 2 years ago

edarsow commented 2 years ago

Goal

codeNforce users have repeatedly asked to view properties (in the database these are now only parcel records; property is deprecated), code enforcement cases, occupancy periods, and persons based on when they were last updated. These objects, however, are composed of a number of sub-objects, each of which is created from one or more records from one or more database tables. This issue/project is to design and implement a set of triggers in postgres which write the current timestamp to the parent objects any time a linked object is added, updated, or removed (deactivated)

Resources

Postgres Overview of trigger behavior

Database table and field conventions

All columns in all table which are foreign keys should contain an _ to signal that they are a FK. If that column is not an FK but does contain an _ this is an error and should be corrected with a patch.

The following sections are listed by the parent object whose feeder tables should each have a trigger on insert, update, or delete which write the current timestamp with now() and, if applicable, which also write the userid of the user doing the update. The userid doing the updating should be written to a field called lastupdatedby_userid. Your triggers could be very helpful by trickling the userid of the updator all the way to the parent, so if userid 106 updates a citation on cecase 102, the new cecase record with an ID of 102 would have a now() written to lastupdatedts and 106 in its lastupdatedby_userid.

Code Enforcement Case: cecase

This table has lots of feeders:

Occupancy periods represented by the table occperiod

An occupancy period a period of time associated with one and only one parcel that starts on a date and may or may not end on a future date to which we can grant permits, conduct inspections, and log events. Occupancy periods have an entire family of sub-tables that should trigger an update to occperiod's lastudpatedts and lastupdatedby_userid:

Properties represented by the table: parcel

Persons represented by the table: human

Note that the person table is completely and utterly deprecated. A human record has bunches of linkages, and we want to update the human's lastupdatedts and lastupdatedby_userid field when any of the linking tables are updated as well as the obvious contact phone and emails:

Trigger on delete

For completion's sake, your triggers should operate on an INSERT, UPDATE, or DELETE but note that the Java users never get to delete a record, they only deactivate them by writing a timestamp to the deactivatedts field and the deactivatedtby_userid FK fields.