migrations icon indicating copy to clipboard operation
migrations copied to clipboard

Model dependent PHP migrations cannot be executed after modification

Open Mikulas opened this issue 9 years ago • 1 comments

Initial state:

class Foo
{
	 /**
	 * @ORM\Column(type="text")
	 */
	private $content;
}

php migration (2016-01-01.php)

assert($entityManager instanceof Doctrine\ORM\EntityManager);

foreach ($entityManager->getRepository(Foo::class)->findAll() as $foo) {
    // ... queries db
}

If we ever change the original model, for example by adding additional property

class Foo
{
	 /**
	 * @ORM\Column(type="text")
	 */
	private $content;

+	 /**
+	 * @ORM\Column(type="datetime_immutable")
+	 */
+	private $createdAt;
}

we will break the php migration, because Doctrine will query the database as

SELECT t0.content AS content, t0.created_at AS created_at -- ...   

but the newly added column was not yet added to database when the php migration executes.

In other words, php migrations use latest meta data with Doctrine, because the model itself is not versioned (or at least the versions are not used for migration purposes).

I don't have a solution to propose, other than not using default Doctrine queries in php migrations. Even writing custom DQL/SQL queries cannot handle renamed columns etc.

Mikulas avatar Dec 28 '16 08:12 Mikulas

Yes, this is a well known limitation. The only solution I know was proposed by @PetrP and that is to essentially checkout old PHP files from Git repository.

JanTvrdik avatar Dec 31 '16 11:12 JanTvrdik