- 
          
- 
        Couldn't load subscription status. 
- Fork 514
          Enable optimistic locking using an ObjectId as version field
          #2815
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 1 commit
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            83 changes: 83 additions & 0 deletions
          
          83 
        
  tests/Doctrine/ODM/MongoDB/Tests/Types/VersionableTest.php
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?php | ||
|  | ||
| declare(strict_types=1); | ||
|  | ||
| namespace Doctrine\ODM\MongoDB\Tests\Types; | ||
|  | ||
| use DateTime; | ||
| use DateTimeImmutable; | ||
| use Doctrine\ODM\MongoDB\Tests\BaseTestCase; | ||
| use Doctrine\ODM\MongoDB\Types\Type; | ||
| use Doctrine\ODM\MongoDB\Types\Versionable; | ||
| use MongoDB\BSON\ObjectId; | ||
|  | ||
| class VersionableTest extends BaseTestCase | ||
| { | ||
| public function testIntNextVersion(): void | ||
| { | ||
| $type = $this->getType(Type::INT); | ||
| self::assertSame(1, $type->getNextVersion(null)); | ||
| self::assertSame(2, $type->getNextVersion(1)); | ||
| } | ||
|  | ||
| public function testDecimal128NextVersion(): void | ||
| { | ||
| $type = $this->getType(Type::DECIMAL128); | ||
| self::assertSame('1', $type->getNextVersion(null)); | ||
| self::assertSame('2', $type->getNextVersion('1')); | ||
| } | ||
|  | ||
| public function testDateTimeNextVersion(): void | ||
| { | ||
| $type = $this->getType(Type::DATE); | ||
| $current = new DateTime(); | ||
| $next = $type->getNextVersion(null); | ||
| self::assertInstanceOf(DateTime::class, $next); | ||
| self::assertGreaterThanOrEqual($current, $next); | ||
| self::assertLessThanOrEqual(new DateTime(), $next); | ||
|  | ||
| $next = $type->getNextVersion(new DateTime('2000-01-01')); | ||
| self::assertInstanceOf(DateTime::class, $next); | ||
| self::assertGreaterThanOrEqual($current, $next); | ||
| self::assertLessThanOrEqual(new DateTime(), $next); | ||
| } | ||
|  | ||
| public function testDateTimeImmutableNextVersion(): void | ||
| { | ||
| $type = $this->getType(Type::DATE_IMMUTABLE); | ||
| $current = new DateTime(); | ||
| $next = $type->getNextVersion(null); | ||
| self::assertInstanceOf(DateTimeImmutable::class, $next); | ||
| self::assertGreaterThanOrEqual($current, $next); | ||
| self::assertLessThanOrEqual(new DateTimeImmutable(), $next); | ||
|  | ||
| $next = $type->getNextVersion(new DateTimeImmutable('2000-01-01')); | ||
| self::assertInstanceOf(DateTimeImmutable::class, $next); | ||
| self::assertGreaterThanOrEqual($current, $next); | ||
| self::assertLessThanOrEqual(new DateTimeImmutable(), $next); | ||
| } | ||
|  | ||
| public function testObjectIdNextVersion(): void | ||
| { | ||
| $type = $this->getType(Type::OBJECTID); | ||
| $current = new ObjectId(); | ||
| $next = $type->getNextVersion(null); | ||
| self::assertInstanceOf(ObjectId::class, $next); | ||
| self::assertGreaterThan($current, $next); | ||
| self::assertLessThan(new ObjectId(), $next); | ||
|  | ||
| $next = $type->getNextVersion($current); | ||
| self::assertInstanceOf(ObjectId::class, $next); | ||
| self::assertGreaterThan($current, $next); | ||
| self::assertLessThan(new ObjectId(), $next); | ||
| } | ||
|  | ||
| private function getType(string $name): Versionable | ||
| { | ||
| $type = Type::getType($name); | ||
|  | ||
| self::assertInstanceOf(Versionable::class, $type); | ||
|  | ||
| return $type; | ||
| } | ||
| } | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly think it would be worth explicitly mentioning somewhere the possibility of deriving the timestamp from the object ID. It's quite a good idea and will probably not occur to many users unless it's mentioned somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC, ODM converts ObjectId values to strings, so users won't be able to easily utilize
ObjectId::getTimestamp()for that purpose. Perhaps this would warrant a separate tutorial or cookbook entry, as there's likely a bit more code involved to parse the timestamp from the string (could be done with PHP code separate fromext-mongodb).On a separate note, ObjectIds might also encounter issues in high-concurrency environments (see: SERVER-6054). The 4-byte timestamp and 3-byte counter (see: ObjectId spec) likely provides more uniqueness than a UTCDateTime with millisecond precision, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exact, the
ObjectIdTypeconvert theObjectIdinto astring. You have to convert it back toObjectIdto get the timestamp.mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/ObjectIdType.php
Line 29 in 650b14d