SqlFileWriter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Migrations;
  20. use Doctrine\DBAL\DBALException;
  21. use Doctrine\DBAL\Exception\InvalidArgumentException;
  22. class SqlFileWriter
  23. {
  24. private $migrationsColumnName;
  25. private $migrationsTableName;
  26. private $destPath;
  27. /** @var null|OutputWriter */
  28. private $outputWriter;
  29. /**
  30. * @param string $migrationsColumnName
  31. * @param string $migrationsTableName
  32. * @param string $destPath
  33. * @param \Doctrine\DBAL\Migrations\OutputWriter $outputWriter
  34. */
  35. public function __construct($migrationsColumnName, $migrationsTableName, $destPath, OutputWriter $outputWriter = null)
  36. {
  37. if (empty($migrationsColumnName)) {
  38. $this->throwInvalidArgumentException('Migrations column name cannot be empty.');
  39. }
  40. $this->migrationsColumnName = $migrationsColumnName;
  41. if (empty($migrationsTableName)) {
  42. $this->throwInvalidArgumentException('Migrations table name cannot be empty.');
  43. }
  44. $this->migrationsTableName = $migrationsTableName;
  45. if (empty($destPath)) {
  46. $this->throwInvalidArgumentException('Destination file must be specified.');
  47. }
  48. $this->destPath = $destPath;
  49. $this->outputWriter = $outputWriter;
  50. }
  51. /**
  52. * @param array $queriesByVersion array Keys are versions and values are arrays of SQL queries (they must be castable to string)
  53. * @param string $direction
  54. * @return int|bool
  55. */
  56. public function write(array $queriesByVersion, $direction)
  57. {
  58. $path = $this->buildMigrationFilePath();
  59. $string = $this->buildMigrationFile($queriesByVersion, $direction);
  60. if ($this->outputWriter) {
  61. $this->outputWriter->write("\n" . sprintf('Writing migration file to "<info>%s</info>"', $path));
  62. }
  63. return file_put_contents($path, $string);
  64. }
  65. private function buildMigrationFile(array $queriesByVersion, $direction)
  66. {
  67. $string = sprintf("-- Doctrine Migration File Generated on %s\n", date('Y-m-d H:i:s'));
  68. foreach ($queriesByVersion as $version => $queries) {
  69. $string .= "\n-- Version " . $version . "\n";
  70. foreach ($queries as $query) {
  71. $string .= $query . ";\n";
  72. }
  73. $string .= $this->getVersionUpdateQuery($version, $direction);
  74. }
  75. return $string;
  76. }
  77. private function getVersionUpdateQuery($version, $direction)
  78. {
  79. if ($direction == Version::DIRECTION_DOWN) {
  80. $query = "DELETE FROM %s WHERE %s = '%s';\n";
  81. } else {
  82. $query = "INSERT INTO %s (%s) VALUES ('%s');\n";
  83. }
  84. return sprintf($query, $this->migrationsTableName, $this->migrationsColumnName, $version);
  85. }
  86. private function buildMigrationFilePath()
  87. {
  88. $path = $this->destPath;
  89. if (is_dir($path)) {
  90. $path = realpath($path);
  91. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  92. }
  93. return $path;
  94. }
  95. /**
  96. * This only exists for backwards-compatibiliy with DBAL 2.4
  97. */
  98. protected function throwInvalidArgumentException($message)
  99. {
  100. if (class_exists('Doctrine\DBAL\Exception\InvalidArgumentException')) {
  101. throw new InvalidArgumentException($message);
  102. } else {
  103. throw new DBALException($message);
  104. }
  105. }
  106. }