Skip to content

Commit

Permalink
Make compared tables order idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque committed Mar 30, 2020
1 parent cff9542 commit c110daa
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Migrations\Provider;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Provider\Exception\NoMappingFound;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -38,6 +39,14 @@ public function createSchema() : Schema
throw NoMappingFound::new();
}

usort($metadata, static function (ClassMetadata $a, ClassMetadata $b) {
if (method_exists($a, 'getTableName') && method_exists($b, 'getTableName')) {
return $a->getTableName() <=> $b->getTableName();
}

return $a->getName() <=> $b->getName();
});

$tool = new SchemaTool($this->entityManager);

return $tool->getSchemaFromMetadata($metadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Doctrine\Migrations\Tests\Provider;

class SampleEntity
class A
{
/** @var int|null */
private $id;
Expand Down
16 changes: 16 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

class B
{
/** @var int|null */
private $id;

public function getId() : ?int
{
return $this->id;
}
}
16 changes: 16 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/C.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

class C
{
/** @var int|null */
private $id;

public function getId() : ?int
{
return $this->id;
}
}
13 changes: 13 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Doctrine\Migrations\Tests\Provider;

use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseMetadataFactoryAlias;

class ClassMetadataFactory extends BaseMetadataFactoryAlias
{
public function getAllMetadata()
{
return array_reverse(parent::getAllMetadata());
}
}
21 changes: 17 additions & 4 deletions tests/Doctrine/Migrations/Tests/Provider/OrmSchemaProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,20 @@ class OrmSchemaProviderTest extends MigrationTestCase
public function testCreateSchemaFetchesMetadataFromEntityManager() : void
{
$schema = $this->ormProvider->createSchema();
self::assertTrue($schema->hasTable('sample_entity'));
$table = $schema->getTable('sample_entity');
self::assertTrue($table->hasColumn('id'));

self::assertSame(
[
'public.a',
'public.b',
'public.c'
],
$schema->getTableNames()
);

foreach (['a', 'b', 'c'] as $expectedTable) {
$table = $schema->getTable($expectedTable);
self::assertTrue($table->hasColumn('id'));
}
}

public function testEntityManagerWithoutMetadataCausesError() : void
Expand All @@ -50,8 +61,10 @@ public function testEntityManagerWithoutMetadataCausesError() : void

protected function setUp() : void
{
$this->config = Setup::createXMLMetadataConfiguration([__DIR__ . '/_files'], true);
$this->config->setClassMetadataFactoryName(ClassMetadataFactory::class);

$this->conn = $this->getSqliteConnection();
$this->config = Setup::createXMLMetadataConfiguration([__DIR__ . '/_files'], true);
$this->entityManager = EntityManager::create($this->conn, $this->config);
$this->ormProvider = new OrmSchemaProvider($this->entityManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\SampleEntity" table="sample_entity">
<entity name="Doctrine\Migrations\Tests\Provider\A" table="a">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\B" table="b">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
</entity>

</doctrine-mapping>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

<entity name="Doctrine\Migrations\Tests\Provider\C" table="c">
<id name="id" type="integer" column="id">
<generator strategy="AUTO" />
</id>
</entity>

</doctrine-mapping>

0 comments on commit c110daa

Please sign in to comment.