RemoveNamespacedAssets.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the LGPL. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\DBAL\Schema\Visitor;
  20. use Doctrine\DBAL\Platforms\AbstractPlatform,
  21. Doctrine\DBAL\Schema\Table,
  22. Doctrine\DBAL\Schema\Schema,
  23. Doctrine\DBAL\Schema\Column,
  24. Doctrine\DBAL\Schema\ForeignKeyConstraint,
  25. Doctrine\DBAL\Schema\Constraint,
  26. Doctrine\DBAL\Schema\Sequence,
  27. Doctrine\DBAL\Schema\Index;
  28. /**
  29. * Remove assets from a schema that are not in the default namespace.
  30. *
  31. * Some databases such as MySQL support cross databases joins, but don't
  32. * allow to call DDLs to a database from another connected database.
  33. * Before a schema is serialized into SQL this visitor can cleanup schemas with
  34. * non default namespaces.
  35. *
  36. * This visitor filters all these non-default namespaced tables and sequences
  37. * and removes them from the SChema instance.
  38. *
  39. * @author Benjamin Eberlei <kontakt@beberlei.de>
  40. * @since 2.2
  41. */
  42. class RemoveNamespacedAssets implements Visitor
  43. {
  44. /**
  45. * @var Schema
  46. */
  47. private $schema;
  48. /**
  49. * @param Schema $schema
  50. */
  51. public function acceptSchema(Schema $schema)
  52. {
  53. $this->schema = $schema;
  54. }
  55. /**
  56. * @param Table $table
  57. */
  58. public function acceptTable(Table $table)
  59. {
  60. if ( ! $table->isInDefaultNamespace($this->schema->getName()) ) {
  61. $this->schema->dropTable($table->getName());
  62. }
  63. }
  64. /**
  65. * @param Sequence $sequence
  66. */
  67. public function acceptSequence(Sequence $sequence)
  68. {
  69. if ( ! $sequence->isInDefaultNamespace($this->schema->getName()) ) {
  70. $this->schema->dropSequence($sequence->getName());
  71. }
  72. }
  73. /**
  74. * @param Column $column
  75. */
  76. public function acceptColumn(Table $table, Column $column)
  77. {
  78. }
  79. /**
  80. * @param Table $localTable
  81. * @param ForeignKeyConstraint $fkConstraint
  82. */
  83. public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
  84. {
  85. // The table may already be deleted in a previous
  86. // RemoveNamespacedAssets#acceptTable call. Removing Foreign keys that
  87. // point to nowhere.
  88. if ( ! $this->schema->hasTable($fkConstraint->getForeignTableName())) {
  89. $localTable->removeForeignKey($fkConstraint->getName());
  90. return;
  91. }
  92. $foreignTable = $this->schema->getTable($fkConstraint->getForeignTableName());
  93. if ( ! $foreignTable->isInDefaultNamespace($this->schema->getName()) ) {
  94. $localTable->removeForeignKey($fkConstraint->getName());
  95. }
  96. }
  97. /**
  98. * @param Table $table
  99. * @param Index $index
  100. */
  101. public function acceptIndex(Table $table, Index $index)
  102. {
  103. }
  104. }