welcome back to dyb-tech
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The SchemaDiffProvider class is responsible for providing a Doctrine\DBAL\Schema\Schema instance that
|
||||
* represents the current state of your database. A clone of this Schema instance is passed to each of your migrations
|
||||
* so that you can manipulate the Schema object. Your manipulated Schema object is then compared to the original Schema
|
||||
* object to produce the SQL statements that need to be executed.
|
||||
*
|
||||
* @internal
|
||||
*
|
||||
* @see Doctrine\Migrations\Version\DbalExecutor
|
||||
*/
|
||||
class DBALSchemaDiffProvider implements SchemaDiffProvider
|
||||
{
|
||||
/** @param AbstractSchemaManager<AbstractPlatform> $schemaManager- */
|
||||
public function __construct(
|
||||
private readonly AbstractSchemaManager $schemaManager,
|
||||
private readonly AbstractPlatform $platform,
|
||||
) {
|
||||
}
|
||||
|
||||
public function createFromSchema(): Schema
|
||||
{
|
||||
return $this->schemaManager->introspectSchema();
|
||||
}
|
||||
|
||||
public function createToSchema(Schema $fromSchema): Schema
|
||||
{
|
||||
return clone $fromSchema;
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array
|
||||
{
|
||||
return $this->platform->getAlterSchemaSQL(
|
||||
$this->schemaManager->createComparator()->compareSchemas($fromSchema, $toSchema),
|
||||
);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Platforms\AbstractPlatform;
|
||||
use Doctrine\DBAL\Schema\AbstractSchemaManager;
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The EmptySchemaProvider class is responsible for creating a Doctrine\DBAL\Schema\Schema instance that
|
||||
* represents the empty state of your database.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class EmptySchemaProvider implements SchemaProvider
|
||||
{
|
||||
/** @param AbstractSchemaManager<AbstractPlatform> $schemaManager */
|
||||
public function __construct(private readonly AbstractSchemaManager $schemaManager)
|
||||
{
|
||||
}
|
||||
|
||||
public function createSchema(): Schema
|
||||
{
|
||||
return new Schema([], [], $this->schemaManager->createSchemaConfig());
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider\Exception;
|
||||
|
||||
use UnexpectedValueException;
|
||||
|
||||
final class NoMappingFound extends UnexpectedValueException implements ProviderException
|
||||
{
|
||||
public static function new(): self
|
||||
{
|
||||
return new self('No mapping information to process');
|
||||
}
|
||||
}
|
||||
Vendored
+11
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider\Exception;
|
||||
|
||||
use Doctrine\Migrations\Exception\MigrationException;
|
||||
|
||||
interface ProviderException extends MigrationException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Symfony\Component\VarExporter\LazyProxyTrait;
|
||||
|
||||
/** @internal */
|
||||
class LazySchema extends Schema
|
||||
{
|
||||
use LazyProxyTrait;
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The LazySchemaDiffProvider is responsible for lazily generating the from schema when diffing two schemas
|
||||
* to produce a migration.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
class LazySchemaDiffProvider implements SchemaDiffProvider
|
||||
{
|
||||
public function __construct(
|
||||
private readonly SchemaDiffProvider $originalSchemaManipulator,
|
||||
) {
|
||||
}
|
||||
|
||||
public function createFromSchema(): Schema
|
||||
{
|
||||
$originalSchemaManipulator = $this->originalSchemaManipulator;
|
||||
|
||||
return LazySchema::createLazyProxy(static fn () => $originalSchemaManipulator->createFromSchema());
|
||||
}
|
||||
|
||||
public function createToSchema(Schema $fromSchema): Schema
|
||||
{
|
||||
$originalSchemaManipulator = $this->originalSchemaManipulator;
|
||||
|
||||
if ($fromSchema instanceof LazySchema && ! $fromSchema->isLazyObjectInitialized()) {
|
||||
return LazySchema::createLazyProxy(static fn () => $originalSchemaManipulator->createToSchema($fromSchema));
|
||||
}
|
||||
|
||||
return $this->originalSchemaManipulator->createToSchema($fromSchema);
|
||||
}
|
||||
|
||||
/** @return string[] */
|
||||
public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array
|
||||
{
|
||||
if (
|
||||
$toSchema instanceof LazySchema
|
||||
&& ! $toSchema->isLazyObjectInitialized()
|
||||
) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->originalSchemaManipulator->getSqlDiffToMigrate($fromSchema, $toSchema);
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
use Doctrine\ORM\Tools\SchemaTool;
|
||||
|
||||
use function usort;
|
||||
|
||||
/**
|
||||
* The OrmSchemaProvider class is responsible for creating a Doctrine\DBAL\Schema\Schema instance from the mapping
|
||||
* information provided by the Doctrine ORM. This is then used to diff against your current database schema to produce
|
||||
* a migration to bring your database in sync with the ORM mapping information.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class OrmSchemaProvider implements SchemaProvider
|
||||
{
|
||||
private EntityManagerInterface $entityManager;
|
||||
|
||||
public function __construct(EntityManagerInterface $em)
|
||||
{
|
||||
$this->entityManager = $em;
|
||||
}
|
||||
|
||||
public function createSchema(): Schema
|
||||
{
|
||||
/** @var array<int, ClassMetadata<object>> $metadata */
|
||||
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata();
|
||||
|
||||
usort($metadata, static fn (ClassMetadata $a, ClassMetadata $b): int => $a->getTableName() <=> $b->getTableName());
|
||||
|
||||
$tool = new SchemaTool($this->entityManager);
|
||||
|
||||
return $tool->getSchemaFromMetadata($metadata);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The SchemaDiffProvider defines the interface used to provide the from and to schemas and to produce
|
||||
* the SQL queries needed to migrate.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
interface SchemaDiffProvider
|
||||
{
|
||||
public function createFromSchema(): Schema;
|
||||
|
||||
public function createToSchema(Schema $fromSchema): Schema;
|
||||
|
||||
/** @return string[] */
|
||||
public function getSqlDiffToMigrate(Schema $fromSchema, Schema $toSchema): array;
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The SchemaProvider defines the interface used to create a Doctrine\DBAL\Schema\Schema instance that
|
||||
* represents the current state of your database.
|
||||
*/
|
||||
interface SchemaProvider
|
||||
{
|
||||
public function createSchema(): Schema;
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Doctrine\Migrations\Provider;
|
||||
|
||||
use Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* The StubSchemaProvider takes a Doctrine\DBAL\Schema\Schema instance through the constructor and returns it
|
||||
* from the createSchema() method.
|
||||
*/
|
||||
final class StubSchemaProvider implements SchemaProvider
|
||||
{
|
||||
private Schema $toSchema;
|
||||
|
||||
public function __construct(Schema $schema)
|
||||
{
|
||||
$this->toSchema = $schema;
|
||||
}
|
||||
|
||||
public function createSchema(): Schema
|
||||
{
|
||||
return $this->toSchema;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user