Christopher Francisco
Christopher Francisco

Reputation: 16258

Using doctrine migrations when a column is to be deleted

We had a column type for a enum called enumFooType which we had added on \Doctrine\DBal\Types\Type::addType().

When running vendor/bin/doctrine-module migrations:diff to generate the migration that would delete said column, an error was thrown:

[Doctrine\DBAL\DBALException]
  Unknown column type "enumFooType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). 
  You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). 
  If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. 
  Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). 
  If the type name is empty you might have a problem with the cache or forgot some mapping information.

I'm guessing the error was thrown because the database has a foo_type marked with (DC2Type:enumFooType).

What is the correct way of handling these types of deletions? My first thought would be to generate a blank migration using vendor/bin/doctrine-module migrations:generate and manually write the query, but I'd like a more automated way, if possible not writing anything manually.

Upvotes: 1

Views: 796

Answers (1)

Christopher Francisco
Christopher Francisco

Reputation: 16258

TL;DR:

The class definition for the DBAL type enumFooType should exist before running the doctrine commands (now that I have written this line, it feels kind of obvious, like "duh!").

Long answer:

After a couple of rollbacks and trial and errors, I devised the following procedure for this kind of operations:

  1. Delete the property of enumFooType from the entity class.
  2. Create the migration (up to this point, the EnumFooType file still exists).
  3. Delete the EnumFooType class that contains the definition of this dbal type.

The reason it has to be done in this order is because if you delete the type first, doctrine won't be load because this file is missing, resulting in the exception posted in the original question.

Moreover, after you have created the migration, and then deleted the type; If you ever need to rollback that change, you have to:

  1. Restore to the previous commit, so that the EnumFooType exist and the property of type enumFooType is defined in the entity class.
  2. Run the migration command to roll back.

Upvotes: 1

Related Questions