Filebase icon indicating copy to clipboard operation
Filebase copied to clipboard

Generate AutoId for new Records

Open chrissander opened this issue 5 years ago • 3 comments

Added the option to leave the id value empty when generating a new item via $db->get(). As suggested in feature request https://github.com/filebase/Filebase/issues/3 there are two config options for auto_id_mode, these are autoincrement and hash.

The option 'hash' will generate a random id with 8 characters. The option 'autoincrement' will result the highest current id +1.

chrissander avatar May 16 '19 04:05 chrissander

Pull Request Test Coverage Report for Build 208

  • 1 of 17 (5.88%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-2.6%) to 92.308%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Database.php 1 17 5.88%
<!-- Total: 1 17
Totals Coverage Status
Change from base Build 148: -2.6%
Covered Lines: 528
Relevant Lines: 572

💛 - Coveralls

coveralls avatar May 16 '19 04:05 coveralls

Only problem with autoincrement is that there is a possibility, that a new item can get the same id as a previously deleted item. This will occur if the last inserted item with the current max id gets deleted, than its old id will get the maxid and will be used for the next inserted item.

In some scenarios like UserIds this could cause trouble, then the hash method should be used. As an alternative one could use a deleted flag instead of deleting the whole item.

chrissander avatar May 16 '19 04:05 chrissander

@chrissander why not instead just keep autoinc in a single file with locks etc?

You could also create an AutoInc class which could be instantiated, this would then allow for multiple autoinc sequences if needed. ie.

$inc_evt = new AutoInc('events');
$inc_users = new AutoInc('users'); // specific counters
or even
AutoInc::init('assets'); //default

// new document based on Event counter
$doc = $db->get($inc_evt);
$doc_id = $doc->getId();

// new document based on default counter, if not defined then AutoIncException is thrown etc
$doc = $db->get(false);
$doc_id = $doc->getId();

tsmgeek avatar Apr 13 '20 17:04 tsmgeek