Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make compared tables order idempotent #954

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/Doctrine/Migrations/Provider/OrmSchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\Provider\Exception\NoMappingFound;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Tools\SchemaTool;
use function count;
use function usort;

/**
* The OrmSchemaProvider class is responsible for creating a Doctrine\DBAL\Schema\Schema instance from the mapping
Expand Down Expand Up @@ -38,6 +40,10 @@ public function createSchema() : Schema
throw NoMappingFound::new();
}

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

$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;
}
}
20 changes: 20 additions & 0 deletions tests/Doctrine/Migrations/Tests/Provider/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tests\Provider;

use Doctrine\Common\Persistence\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory as BaseMetadataFactoryAlias;
use function array_reverse;

class ClassMetadataFactory extends BaseMetadataFactoryAlias
{
/**
* @return ClassMetadata[]
*/
public function getAllMetadata() : array
{
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>