SingleDatabaseSynchronizer.php 3.8 KB

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