Skip to content

Commit

Permalink
fix: Do not drop/re-create 2dsphere indexes (#2575)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffcode authored Nov 6, 2023
1 parent fa10f36 commit d6f1686
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/SchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ final class SchemaManager
'language_override',
'textIndexVersion',
'name',
'2dsphereIndexVersion',
];

protected DocumentManager $dm;
Expand Down
39 changes: 39 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/Functional/IndexesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ public function testPartialIndexCreation(): void
self::assertSame(['counter' => ['$gt' => 5]], $indexes[0]['options']['partialFilterExpression']);
self::assertTrue($indexes[0]['options']['unique']);
}

public function testGeoIndexCreation(): void
{
$className = GeoIndexDocument::class;
$this->dm->getSchemaManager()->ensureDocumentIndexes(GeoIndexDocument::class);

$indexes = $this->dm->getSchemaManager()->getDocumentIndexes($className);
self::assertSame(['coordinatesWith2DIndex' => '2d'], $indexes[0]['keys']);
self::assertSame(['coordinatesWithSphereIndex' => '2dsphere'], $indexes[1]['keys']);
}
}

/** @ODM\Document */
Expand Down Expand Up @@ -657,3 +667,32 @@ class DocumentWithIndexInDiscriminatedEmbeds
*/
public $embedded;
}

/**
* @ODM\Document
* @ODM\Index(keys={"coordinatesWith2DIndex"="2d"})
* @ODM\Index(keys={"coordinatesWithSphereIndex"="2dsphere"})
*/
class GeoIndexDocument
{
/**
* @ODM\Id
*
* @var string|null
*/
public $id;

/**
* @ODM\Field(type="hash")
*
* @var array<float>
*/
public $coordinatesWith2DIndex;

/**
* @ODM\Field(type="hash")
*
* @var array<float>
*/
public $coordinatesWithSphereIndex;
}
6 changes: 6 additions & 0 deletions tests/Doctrine/ODM/MongoDB/Tests/SchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,12 @@ public static function dataIsMongoIndexEquivalentToDocumentIndex(): array
'mongoIndex' => ['background' => true],
'documentIndex' => ['options' => ['background' => true]],
],
// 2dsphereIndexVersion index options
'2dsphereIndexVersionOptionsDifferent' => [
'expected' => true,
'mongoIndex' => ['2dsphereIndexVersion' => 3],
'documentIndex' => [],
],
];
}

Expand Down

0 comments on commit d6f1686

Please sign in to comment.