[TikiWiki-commits] [Git][tikiwiki/tiki][29.x] [FIX] AutoIncrement race condition: try to resolve duplicate values when...

"Victor Emanouilov \(@kroky\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <699c2ba1f29b7_3b217b6948777@gitlab-sidekiq-low-urgency-cpu-bound-v2-ccd878489-cbbtv.mail>

Victor Emanouilov pushed to branch 29.x at Tiki Wiki CMS Groupware / Tiki


Commits:
b36e84c8 by Victor Emanouilov at 2026-02-23T12:27:34+02:00
[FIX] AutoIncrement race condition: try to resolve duplicate values when records are created at the same time

- - - - -


1 changed file:

- lib/core/Tracker/Field/AutoIncrement.php


Changes:

=====================================
lib/core/Tracker/Field/AutoIncrement.php
=====================================
@@ -149,6 +149,51 @@ class Tracker_Field_AutoIncrement extends \Tracker\Field\AbstractItemField imple
         return false;
     }
 
+    /**
+     * After the item is saved, check for duplicate auto-increment values that
+     * can occur when concurrent processes both read the same maximum value
+     * before either has persisted its result. If a duplicate is found,
+     * re-read the current maximum and assign a new unique value.
+     */
+    public function postSaveHook($value)
+    {
+        // itemId mode uses the actual DB item ID — duplicates are impossible
+        if ($this->getOption('itemId') == 'itemId') {
+            return;
+        }
+
+        $itemId = $this->getItemId();
+        $fieldId = $this->getConfiguration('fieldId');
+
+        if (! $itemId || $value === false || $value === null || $value === '') {
+            return;
+        }
+
+        global $prefs;
+        $trklib = TikiLib::lib('trk');
+        $table = TikiDb::get()->table('tiki_tracker_item_fields');
+
+        $maxRetries = 3;
+        for ($i = 0; $i < $maxRetries; $i++) {
+            $duplicateCount = $table->fetchCount([
+                'fieldId' => (int)$fieldId,
+                'value' => (string)$value,
+            ]);
+
+            if ($duplicateCount <= 1) {
+                break;
+            }
+
+            $maxValue = $trklib->get_maximum_value($fieldId);
+            if ($prefs['tracker_autoincrement_resettable'] == 'y') {
+                $value = max($maxValue + 1, $this->getOption('start', 1));
+            } else {
+                $value = $maxValue + 1;
+            }
+            $trklib->modify_field($itemId, $fieldId, $value);
+        }
+    }
+
     public function getTabularSchema()
     {
         $schema = new Tracker\Tabular\Schema($this->getTrackerDefinition());



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/b36e84c83386d44de17be5a8ed0116094ba29cab

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/b36e84c83386d44de17be5a8ed0116094ba29cab
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.