Home > Software design >  Doctrine migration created by SymfonyMakerBundle tries to remove existing indexes
Doctrine migration created by SymfonyMakerBundle tries to remove existing indexes

Time:06-27

I am trying to create a doctrine migration using the SymfonyMakerBundle. I ran the symfony console make:migration command and fixed mismatches between entities and the database as far as possible. However, the migration tries to delete existing indexes in the database, and I can't seem to figure out as to why. If there were some mismatch in charset, or other funny mismatches, I would expect the migration to add the indexes later on (replace), but this is not the case. The desired outcome is to stop the make:migration command from deleting my indexes (most likely by fixing some misconfiguration in my entities).

Any tips are more than welcome :)!

Ouput of migration Version20220624123412.php

    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE RelationType (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
        $this->addSql('DROP TABLE migration_versions');
        $this->addSql('ALTER TABLE Administrator CHANGE roles roles LONGTEXT NOT NULL COMMENT \'(DC2Type:json)\', CHANGE internal_phone_extension internal_phone_extension LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\'');
        $this->addSql('ALTER TABLE AlpheraLeadDetails CHANGE lead_id lead_id INT NOT NULL');
        $this->addSql('DROP INDEX IX_BANK_ASSOCIATED_WITH_WORKFLOW_STEPNAME ON Bank');
        $this->addSql('DROP INDEX IDX_79CFCD8FBD62EB53 ON ContactLog');
        $this->addSql('DROP INDEX IX_COUNTRY_ISO_31661_2 ON Country');
        $this->addSql('DROP INDEX IX_COUNTRY_ISO_31661_3 ON Country');
        $this->addSql('DROP INDEX idx_created_at ON Lead');
        $this->addSql('ALTER TABLE Lead CHANGE risk_fee_warranty_addon risk_fee_warranty_addon TINYINT(1) NOT NULL');
        $this->addSql('ALTER TABLE LeadAnswer CHANGE lead_id lead_id INT NOT NULL');
        $this->addSql('ALTER TABLE LeadOrigin CHANGE trustpilotToken trustpilotToken LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:json)\'');
        $this->addSql('DROP INDEX LeadTradeIn_licensePlate_index ON LeadTradeIn');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype DROP FOREIGN KEY FK_4DE09F83EDEDABA0');
        $this->addSql('DROP INDEX IDX_4DE09F83EDEDABA0 ON vehiclesearchrequest_chassistype');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype DROP PRIMARY KEY');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype DROP FOREIGN KEY FK_4DE09F8336810269');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype DROP vehiclesearchrequest_id');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype ADD CONSTRAINT FK_4DE09F83F93C3BF5 FOREIGN KEY (chassisType_id) REFERENCES VehicleSearchRequest (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype ADD PRIMARY KEY (chassisType_id, chassistype_id)');
        $this->addSql('DROP INDEX idx_4de09f8336810269 ON vehiclesearchrequest_chassistype');
        $this->addSql('CREATE INDEX IDX_4DE09F83F93C3BF5 ON vehiclesearchrequest_chassistype (chassisType_id)');
        $this->addSql('ALTER TABLE vehiclesearchrequest_chassistype ADD CONSTRAINT FK_4DE09F8336810269 FOREIGN KEY (chassistype_id) REFERENCES ChassisType (id) ON DELETE CASCADE');
        $this->addSql('ALTER TABLE vehiclesearchrequest_vehiclecolor DROP FOREIGN KEY FK_314CF713EDEDABA0');
        $this->addSql('ALTER TABLE vehiclesearchrequest_vehiclecolor DROP FOREIGN KEY FK_314CF71311E1A3A7');
        $this->addSql('DROP INDEX IDX_314CF71311E1A3A7 ON vehiclesearchrequest_vehiclecolor');
        $this->addSql('DROP INDEX IDX_314CF713EDEDABA0 ON vehiclesearchrequest_vehiclecolor');
        $this->addSql('ALTER TABLE vehiclesearchrequest_vehiclecolor DROP PRIMARY KEY');
        $this->addSql('ALTER TABLE vehiclesearchrequest_vehiclecolor DROP vehiclesearchrequest_id');
        $this->addSql('ALTER TABLE vehiclesearchrequest_vehiclecolor ADD PRIMARY KEY (vehiclecolor_id)');
        $this->addSql('DROP INDEX next_step_name_idx ON WorkflowLog');
        $this->addSql('DROP INDEX current_step_name_idx ON WorkflowLog');
        $this->addSql('ALTER TABLE WorkflowLog DROP FOREIGN KEY FK_7BD34878E25D857E');
        $this->addSql('DROP INDEX context_idx ON WorkflowLog');
        $this->addSql('CREATE INDEX IDX_7BD34878E25D857E ON WorkflowLog (context)');
        $this->addSql('ALTER TABLE WorkflowLog ADD CONSTRAINT FK_7BD34878E25D857E FOREIGN KEY (context) REFERENCES Lead (id)');
    }

Contents of Country.php

#[
    ORM\Table(name: "Country",
    indexes: [
        new ORM\Index(columns: ['iso3166_1_alpha_2'], name: 'IX_COUNTRY_ISO_31661_2'),
        new ORM\Index(columns: ['iso3166_1_alpha_3'], name: 'IX_COUNTRY_ISO_31661_3')
    ]),
    ORM\Entity(repositoryClass: CountryRepository::class)
]
class Country implements Stringable
{
    /**
     * @var int
     *
     * @Serializer\Expose
     */
    #[ORM\Column(name: 'id', type: Types::INTEGER)]
    #[ORM\Id]
    #[ORM\GeneratedValue]
    protected ?int $id = null;
    
    /**
     * @Serializer\SerializedName("name")
     * @Serializer\Expose
     */
    #[ORM\Column(name: 'name', type: Types::STRING, length: 150)]
    private ?string $name = null;
    
    #[ORM\Column(name: 'official_name_in_english', type: Types::STRING, length: 150)]
    private ?string $officialNameInEnglish = null;
    
    #[ORM\Column(name: 'iso3166_1_alpha_2', type: Types::STRING, length: 2)]
    private ?string $iso3166_1_alpha_2 = null;
    
    #[ORM\Column(name: 'iso3166_1_alpha_3', type: Types::STRING, length: 3)]
    private ?string $iso3166_1_alpha_3 = null;
    
    #[ORM\Column(name: 'iso4217_currency_code', type: Types::STRING, length: 3)]
    private ?string $iso4217_currency_code = null;
    
    #[ORM\Column(name: 'phone_exts', type: Types::JSON)]
    private ?array $phoneExts = null;
    
    #[ORM\OneToOne(mappedBy: 'country', targetEntity: AlpheraCountry::class)]
    private readonly AlpheraCountry $alpheraCountry;

--- Shortend for relativity

Structure of the database table Country

create table Country
(
    id                       int auto_increment
        primary key,
    name                     varchar(150) not null,
    official_name_in_english varchar(150) not null,
    iso3166_1_alpha_2        varchar(2)   not null,
    iso3166_1_alpha_3        varchar(3)   not null,
    iso4217_currency_code    varchar(3)   not null,
    phone_exts               longtext     not null comment '(DC2Type:json)'
)
    collate = utf8mb3_unicode_ci
    auto_increment = 235;

create index IX_COUNTRY_ISO_31661_2
    on Country (iso3166_1_alpha_2);

create index IX_COUNTRY_ISO_31661_3
    on Country (iso3166_1_alpha_3);

Stack & packages

  • MariaDB: 10.7
  • doctrine/dbal: 3.3.7
  • doctrine/orm: 2.12.2
  • doctrine/doctrine-migrations-bundle: 3.2.2
  • symfony/maker-bundle: V1.43.0
  • doctrine/doctrine-bundle: 2.6.3

CodePudding user response:

Could you try this instead ?

#[
    ORM\Table(name: "Country"),
    ORM\Index(columns: ['iso3166_1_alpha_2'], name: 'IX_COUNTRY_ISO_31661_2'),
    ORM\Index(columns: ['iso3166_1_alpha_3'], name: 'IX_COUNTRY_ISO_31661_3'),
    ORM\Entity(repositoryClass: CountryRepository::class)
]
class Country implements Stringable
  • Related