PageRenderTime 28ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine-migrations/lib/Doctrine/DBAL/Migrations/Version.php

https://github.com/jordscream/symfony-sandbox
PHP | 333 lines | 175 code | 49 blank | 109 comment | 17 complexity | e863597c6e17f1750de38edfe57ea514 MD5 | raw file
  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\Migrations\Configuration\Configuration,
  21. Doctrine\DBAL\Schema\Schema;
  22. /**
  23. * Class which wraps a migration version and allows execution of the
  24. * individual migration version up or down method.
  25. *
  26. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  27. * @link www.doctrine-project.org
  28. * @since 2.0
  29. * @author Jonathan H. Wage <jonwage@gmail.com>
  30. */
  31. class Version
  32. {
  33. const STATE_NONE = 0;
  34. const STATE_PRE = 1;
  35. const STATE_EXEC = 2;
  36. const STATE_POST = 3;
  37. /**
  38. * The Migrations Configuration instance for this migration
  39. *
  40. * @var Configuration
  41. */
  42. private $configuration;
  43. /**
  44. * The OutputWriter object instance used for outputting information
  45. *
  46. * @var OutputWriter
  47. */
  48. private $outputWriter;
  49. /**
  50. * The version in timestamp format (YYYYMMDDHHMMSS)
  51. *
  52. * @param int
  53. */
  54. private $version;
  55. /**
  56. * @var AbstractSchemaManager
  57. */
  58. private $sm;
  59. /**
  60. * @var AbstractPlatform
  61. */
  62. private $platform;
  63. /**
  64. * The migration instance for this version
  65. *
  66. * @var AbstractMigration
  67. */
  68. private $migration;
  69. /**
  70. * @var Connection
  71. */
  72. private $connection;
  73. /**
  74. * @var string
  75. */
  76. private $class;
  77. /** The array of collected SQL statements for this version */
  78. private $sql = array();
  79. /** The time in seconds that this migration version took to execute */
  80. private $time;
  81. /**
  82. * @var int
  83. */
  84. private $state = self::STATE_NONE;
  85. public function __construct(Configuration $configuration, $version, $class)
  86. {
  87. $this->configuration = $configuration;
  88. $this->outputWriter = $configuration->getOutputWriter();
  89. $this->class = $class;
  90. $this->connection = $configuration->getConnection();
  91. $this->sm = $this->connection->getSchemaManager();
  92. $this->platform = $this->connection->getDatabasePlatform();
  93. $this->migration = new $class($this);
  94. $this->version = $this->migration->getName() ?: $version;
  95. }
  96. /**
  97. * Returns the string version in the format YYYYMMDDHHMMSS
  98. *
  99. * @return string $version
  100. */
  101. public function getVersion()
  102. {
  103. return $this->version;
  104. }
  105. /**
  106. * Returns the Migrations Configuration object instance
  107. *
  108. * @return Configuration $configuration
  109. */
  110. public function getConfiguration()
  111. {
  112. return $this->configuration;
  113. }
  114. /**
  115. * Check if this version has been migrated or not.
  116. *
  117. * @param bool $bool
  118. * @return mixed
  119. */
  120. public function isMigrated()
  121. {
  122. return $this->configuration->hasVersionMigrated($this);
  123. }
  124. public function markMigrated()
  125. {
  126. $this->configuration->createMigrationTable();
  127. $this->connection->executeQuery("INSERT INTO " . $this->configuration->getMigrationsTableName() . " (version) VALUES (?)", array($this->version));
  128. }
  129. public function markNotMigrated()
  130. {
  131. $this->configuration->createMigrationTable();
  132. $this->connection->executeQuery("DELETE FROM " . $this->configuration->getMigrationsTableName() . " WHERE version = ?", array($this->version));
  133. }
  134. /**
  135. * Add some SQL queries to this versions migration
  136. *
  137. * @param mixed $sql
  138. * @return void
  139. */
  140. public function addSql($sql)
  141. {
  142. if (is_array($sql)) {
  143. foreach ($sql as $query) {
  144. $this->sql[] = $query;
  145. }
  146. } else {
  147. $this->sql[] = $sql;
  148. }
  149. }
  150. /**
  151. * Write a migration SQL file to the given path
  152. *
  153. * @param string $path The path to write the migration SQL file.
  154. * @param string $direction The direction to execute.
  155. * @return bool $written
  156. */
  157. public function writeSqlFile($path, $direction = 'up')
  158. {
  159. $queries = $this->execute($direction, true);
  160. $string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
  161. $string .= "\n# Version " . $this->version . "\n";
  162. foreach ($queries as $query) {
  163. $string .= $query . ";\n";
  164. }
  165. if (is_dir($path)) {
  166. $path = realpath($path);
  167. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  168. }
  169. $this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
  170. return file_put_contents($path, $string);
  171. }
  172. /**
  173. * @return AbstractMigration
  174. */
  175. public function getMigration()
  176. {
  177. return $this->migration;
  178. }
  179. /**
  180. * Execute this migration version up or down and and return the SQL.
  181. *
  182. * @param string $direction The direction to execute the migration.
  183. * @param string $dryRun Whether to not actually execute the migration SQL and just do a dry run.
  184. * @return array $sql
  185. * @throws Exception when migration fails
  186. */
  187. public function execute($direction, $dryRun = false)
  188. {
  189. $this->sql = array();
  190. $this->connection->beginTransaction();
  191. try {
  192. $start = microtime(true);
  193. $this->state = self::STATE_PRE;
  194. $fromSchema = $this->sm->createSchema();
  195. $this->migration->{'pre' . ucfirst($direction)}($fromSchema);
  196. if ($direction === 'up') {
  197. $this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n");
  198. } else {
  199. $this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n");
  200. }
  201. $this->state = self::STATE_EXEC;
  202. $toSchema = clone $fromSchema;
  203. $this->migration->$direction($toSchema);
  204. $this->addSql($fromSchema->getMigrateToSql($toSchema, $this->platform));
  205. if ($dryRun === false) {
  206. if ($this->sql) {
  207. $count = count($this->sql);
  208. foreach ($this->sql as $query) {
  209. $this->outputWriter->write(' <comment>-></comment> ' . $query);
  210. $this->connection->executeQuery($query);
  211. }
  212. } else {
  213. $this->outputWriter->write(sprintf('<error>Migration %s was executed but did not result in any SQL statements.</error>', $this->version));
  214. }
  215. if ($direction === 'up') {
  216. $this->markMigrated();
  217. } else {
  218. $this->markNotMigrated();
  219. }
  220. } else {
  221. foreach ($this->sql as $query) {
  222. $this->outputWriter->write(' <comment>-></comment> ' . $query);
  223. }
  224. }
  225. $this->state = self::STATE_POST;
  226. $this->migration->{'post' . ucfirst($direction)}($toSchema);
  227. $end = microtime(true);
  228. $this->time = round($end - $start, 2);
  229. if ($direction === 'up') {
  230. $this->outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->time));
  231. } else {
  232. $this->outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->time));
  233. }
  234. $this->connection->commit();
  235. return $this->sql;
  236. } catch(SkipMigrationException $e) {
  237. $this->connection->rollback();
  238. if ($dryRun == false) {
  239. // now mark it as migrated
  240. if ($direction === 'up') {
  241. $this->markMigrated();
  242. } else {
  243. $this->markNotMigrated();
  244. }
  245. }
  246. $this->outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage()));
  247. } catch (\Exception $e) {
  248. $this->outputWriter->write(sprintf(
  249. '<error>Migration %s failed during %s. Error %s</error>',
  250. $this->version, $this->getExecutionState(), $e->getMessage()
  251. ));
  252. $this->connection->rollback();
  253. $this->state = self::STATE_NONE;
  254. throw $e;
  255. }
  256. $this->state = self::STATE_NONE;
  257. }
  258. public function getExecutionState()
  259. {
  260. switch($this->state) {
  261. case self::STATE_PRE:
  262. return 'Pre-Checks';
  263. case self::STATE_POST:
  264. return 'Post-Checks';
  265. case self::STATE_EXEC:
  266. return 'Execution';
  267. default:
  268. return 'No State';
  269. }
  270. }
  271. /**
  272. * Returns the time this migration version took to execute
  273. *
  274. * @return integer $time The time this migration version took to execute
  275. */
  276. public function getTime()
  277. {
  278. return $this->time;
  279. }
  280. public function __toString()
  281. {
  282. return $this->version;
  283. }
  284. }