mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 04:49:41 -05:00
#5813 / #4743 made server-side card numbers atomic with a per-board counter, but a card added in the browser - the add-card form, a card from a template, a link, a checklist item turned into a card - still brings the number the browser computed: the highest among the cards it has, plus one. With lazy card loading the browser has only the cards in view, so it reissued numbers other cards already carried, and the counter never saw them, so the server reissued them as well. One production database had 842 numbers used by two or more open cards. Cards.before.insert on the server: a number another card on the board already has is replaced from the counter, after raising the counter to the board's highest number; a free number the counter did not issue raises the counter to it. A board with no counter yet is seeded from its highest number first, as getNextCardNumber's lazy seed does - the reporter's patch created it from the browser's number alone, which can be below numbers outside that browser's view, and the seed would then never run. 0 / missing stays no number. Existing duplicates are not renumbered: a number may be written down elsewhere. Thanks to Raymond-Voogt and xet7 ! Fixes #6815.
36 lines
1.7 KiB
JavaScript
36 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
// #6815: card numbers are board-wide and meant to be unique (#5813 / #4743), but a card
|
|
// added in the browser (add-card form, template, link, checklist item to card)
|
|
// brings the number the browser computed itself: the highest number among the
|
|
// cards it HAS, plus one. With lazy card loading the browser only has the cards
|
|
// in view, so it reissues numbers that cards outside its view already carry -
|
|
// and two people adding a card at the same moment get the same number. The
|
|
// server's atomic counter (Boards.getNextCardNumber) never saw those numbers, so
|
|
// it reissued them too. One production board set had 842 numbers used twice on
|
|
// 452 boards; 231 of the pairs were created more than a day apart.
|
|
//
|
|
// The server therefore checks every new card's number (server/models/cards.js,
|
|
// Cards.before.insert): a number another card on the board already has is
|
|
// replaced by a fresh one from the counter, and the counter is raised to any
|
|
// number it did not issue, so it never hands that one out again.
|
|
|
|
const counterName = boardId => `cardNumber-${boardId}`;
|
|
|
|
// Only a real number is checked: 0 / missing means "no number" and stays so.
|
|
function isCardNumber(n) {
|
|
return Number.isSafeInteger(n) && n > 0;
|
|
}
|
|
|
|
function needsNewCardNumber(cardNumber, takenByAnotherCard) {
|
|
return isCardNumber(cardNumber) && !!takenByAnotherCard;
|
|
}
|
|
|
|
// #6815: what the counter is raised to for a free number: that number, or the
|
|
// board's highest one when the counter is new and that is higher.
|
|
function counterFloor(cardNumber, boardTop) {
|
|
return isCardNumber(boardTop) ? Math.max(cardNumber, boardTop) : cardNumber;
|
|
}
|
|
|
|
module.exports = { counterName, isCardNumber, needsNewCardNumber, counterFloor };
|