Add createdon/editedon timestamps to element and category tables#16930
Add createdon/editedon timestamps to element and category tables#16930pbowyer wants to merge 3 commits into
Conversation
…hemas Add nullable datetime columns for createdon and editedon to the XML schema and PHP map files for: modCategory, modChunk, modPlugin, modPluginEvent, modSnippet, modTemplate, modTemplateVar, modTemplateVarResource, modTemplateVarResourceGroup, and modTemplateVarTemplate. editedon uses ON UPDATE CURRENT_TIMESTAMP so MySQL automatically tracks modifications. Both fields default to NULL for existing records.
Set createdon to the current datetime in modElement::save() and modCategory::save() when creating new objects, matching the convention used by modUser. Add upgrade script for 3.2.1 that adds the createdon and editedon columns to all 10 affected tables during setup.
Verify that createdon is set on new chunks and categories, that editedon remains null after creation, and that createdon is preserved after updates.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 3.x #16930 +/- ##
============================================
+ Coverage 21.64% 21.68% +0.04%
- Complexity 10764 10788 +24
============================================
Files 566 566
Lines 33005 33150 +145
============================================
+ Hits 7144 7189 +45
- Misses 25861 25961 +100 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@pbowyer Hey Peter - Absolutely agree the timestamp additions should be done. However, for consistency with the current data structure, I'd much rather see them implemented as unix timestamps. For next-gen MODX (presumable 4.0) the optimal date/time storage type can be debated and, if changed, implemented across the whole codebase. Note that in the context of web apps, the 32-bit limitation is really a non-issue as, from my research, you're pretty much only going to find legacy non 64-bit hardware in industrial settings, especially 12 years from now. |
What does it do?
MODX tracks when resources, users, and settings were created and last modified, but not elements. There's no way to tell when a snippet was last changed or which templates haven't been touched in years.
As a developer, this information is useful. This PR adds
createdonandeditedoncolumns to chunks, snippets, plugins, templates, TVs, categories, and their association tables.How does it work?
createdonis set in PHP when an element or category is first saved, the same waymodUseralready handles it.editedonis handled by MySQL (ON UPDATE CURRENT_TIMESTAMP), so it requires no application code. Both use thedatetimetype rather thantimestampto avoid timezone conversion quirks and the 2038 year limit.Existing records get
NULLfor both columns.editedonstaysNULLuntil an actual edit is made, which matches how resources behave.For static elements,
editedonreflects when MODX last synced the file content to the database, which may differ from when the file was actually modified.editedonreflecting when the DB row was last updated is at least honest and consistent.How to test
I've extended the test suite to verify
createdonis set on creation,editedonisnullafter creation,createdonis preserved after update.Related issue(s)/PR(s)
#13857 was a previous PR to do this. It added
createdbyandeditedbyas well, and as it went the scope expanded. I have kept my PR focused because I am not using the other features. It ran into problems with static elements, and think I have taken a pragmatic approach to solving that.#13770 #13626 #13641 #2027