Skip to content

Indices from sub classes always get re-created #2561

Open
@buffcode

Description

@buffcode

Bug Report

Q A
BC Break no
Version 2.5.3

Summary

Having an inheritance and defining indices on a subclass (eg. UniqueIndex) will always re-create the subclasses' indices.

Current behavior

Having an inheritance and defining indices on a subclass (eg. UniqueIndex) will result in the subclass indices to be dropped on every run of doctrine:mongodb:schema:update and re-created afterwards.

Notice that the index will exist after the command, so the collection looks okay, but the indices are effectively dropped and recreated.
This is usually not such of a big problem but it put some our our databases down after dropping an index on a 0,5 TB collection and starting to recreate it for multiple hours.

How to reproduce

<?php

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document()
 * @ODM\InheritanceType("SINGLE_COLLECTION")
 * @ODM\DiscriminatorField("metadataType")
 */
abstract class Metadata
{
    /**
     * @ODM\Id()
     */
    protected ?string $id = null;

    public function getId(): ?string
    {
        return $this->id;
    }
}
<?php

use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;

/**
 * @ODM\Document()
 */
class PreAggregationMetadata extends Metadata
{
    /**
     * @ODM\UniqueIndex()
     * @ODM\Field(type="string")
     */
    private string $cacheKey;
}

For debugging purposes add the following code to \Doctrine\ODM\MongoDB\SchemaManager::updateDocumentIndexes, right before $collection->dropIndex(...):

dump('Dropping index ' . $mongoIndex['name'] . ' on ' . $documentName);

Run:

$ bin/console doctrine:mongodb:schema:update
Dropping index cacheKey on Metadata
Updated indexes for all classes
Updated validation for all classes

Expected behavior

No index drop

$ bin/console doctrine:mongodb:schema:update
Updated indexes for all classes
Updated validation for all classes

Workaround

All indices have to be defined on the root class.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions