SingleDatabaseSynchronizer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace Doctrine\DBAL\Schema\Synchronizer;
  3. use Doctrine\DBAL\Connection;
  4. use Doctrine\DBAL\Platforms\AbstractPlatform;
  5. use Doctrine\DBAL\Schema\Comparator;
  6. use Doctrine\DBAL\Schema\Schema;
  7. use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
  8. use function count;
  9. /**
  10. * Schema Synchronizer for Default DBAL Connection.
  11. *
  12. * @deprecated
  13. */
  14. class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
  15. {
  16. /** @var AbstractPlatform */
  17. private $platform;
  18. public function __construct(Connection $conn)
  19. {
  20. parent::__construct($conn);
  21. $this->platform = $conn->getDatabasePlatform();
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function getCreateSchema(Schema $createSchema)
  27. {
  28. return $createSchema->toSql($this->platform);
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function getUpdateSchema(Schema $toSchema, $noDrops = false)
  34. {
  35. $comparator = new Comparator();
  36. $sm = $this->conn->getSchemaManager();
  37. $fromSchema = $sm->createSchema();
  38. $schemaDiff = $comparator->compare($fromSchema, $toSchema);
  39. if ($noDrops) {
  40. return $schemaDiff->toSaveSql($this->platform);
  41. }
  42. return $schemaDiff->toSql($this->platform);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function getDropSchema(Schema $dropSchema)
  48. {
  49. $visitor = new DropSchemaSqlCollector($this->platform);
  50. $sm = $this->conn->getSchemaManager();
  51. $fullSchema = $sm->createSchema();
  52. foreach ($fullSchema->getTables() as $table) {
  53. if ($dropSchema->hasTable($table->getName())) {
  54. $visitor->acceptTable($table);
  55. }
  56. foreach ($table->getForeignKeys() as $foreignKey) {
  57. if (! $dropSchema->hasTable($table->getName())) {
  58. continue;
  59. }
  60. if (! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
  61. continue;
  62. }
  63. $visitor->acceptForeignKey($table, $foreignKey);
  64. }
  65. }
  66. if (! $this->platform->supportsSequences()) {
  67. return $visitor->getQueries();
  68. }
  69. foreach ($dropSchema->getSequences() as $sequence) {
  70. $visitor->acceptSequence($sequence);
  71. }
  72. foreach ($dropSchema->getTables() as $table) {
  73. $primaryKey = $table->getPrimaryKey();
  74. if ($primaryKey === null) {
  75. continue;
  76. }
  77. $columns = $primaryKey->getColumns();
  78. if (count($columns) > 1) {
  79. continue;
  80. }
  81. $checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
  82. if (! $fullSchema->hasSequence($checkSequence)) {
  83. continue;
  84. }
  85. $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
  86. }
  87. return $visitor->getQueries();
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getDropAllSchema()
  93. {
  94. $sm = $this->conn->getSchemaManager();
  95. $visitor = new DropSchemaSqlCollector($this->platform);
  96. $schema = $sm->createSchema();
  97. $schema->visit($visitor);
  98. return $visitor->getQueries();
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function createSchema(Schema $createSchema)
  104. {
  105. $this->processSql($this->getCreateSchema($createSchema));
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function updateSchema(Schema $toSchema, $noDrops = false)
  111. {
  112. $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function dropSchema(Schema $dropSchema)
  118. {
  119. $this->processSqlSafely($this->getDropSchema($dropSchema));
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function dropAllSchema()
  125. {
  126. $this->processSql($this->getDropAllSchema());
  127. }
  128. }