PageRenderTime 65ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/nguyennamtien/TaskBoxx
PHP | 347 lines | 186 code | 50 blank | 111 comment | 21 complexity | 6844532c64d8aa10463994e7627516a0 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 array of collected parameters for SQL statements for this version */
  80. private $params = array();
  81. /** The time in seconds that this migration version took to execute */
  82. private $time;
  83. /**
  84. * @var int
  85. */
  86. private $state = self::STATE_NONE;
  87. public function __construct(Configuration $configuration, $version, $class)
  88. {
  89. $this->configuration = $configuration;
  90. $this->outputWriter = $configuration->getOutputWriter();
  91. $this->class = $class;
  92. $this->connection = $configuration->getConnection();
  93. $this->sm = $this->connection->getSchemaManager();
  94. $this->platform = $this->connection->getDatabasePlatform();
  95. $this->migration = new $class($this);
  96. $this->version = $this->migration->getName() ?: $version;
  97. }
  98. /**
  99. * Returns the string version in the format YYYYMMDDHHMMSS
  100. *
  101. * @return string $version
  102. */
  103. public function getVersion()
  104. {
  105. return $this->version;
  106. }
  107. /**
  108. * Returns the Migrations Configuration object instance
  109. *
  110. * @return Configuration $configuration
  111. */
  112. public function getConfiguration()
  113. {
  114. return $this->configuration;
  115. }
  116. /**
  117. * Check if this version has been migrated or not.
  118. *
  119. * @param bool $bool
  120. * @return mixed
  121. */
  122. public function isMigrated()
  123. {
  124. return $this->configuration->hasVersionMigrated($this);
  125. }
  126. public function markMigrated()
  127. {
  128. $this->configuration->createMigrationTable();
  129. $this->connection->executeQuery("INSERT INTO " . $this->configuration->getMigrationsTableName() . " (version) VALUES (?)", array($this->version));
  130. }
  131. public function markNotMigrated()
  132. {
  133. $this->configuration->createMigrationTable();
  134. $this->connection->executeQuery("DELETE FROM " . $this->configuration->getMigrationsTableName() . " WHERE version = ?", array($this->version));
  135. }
  136. /**
  137. * Add some SQL queries to this versions migration
  138. *
  139. * @param mixed $sql
  140. * @param array $params
  141. * @return void
  142. */
  143. public function addSql($sql, array $params = array())
  144. {
  145. if (is_array($sql)) {
  146. foreach ($sql as $key => $query) {
  147. $this->sql[] = $query;
  148. if (isset($params[$key])) {
  149. $this->params[count($this->sql) - 1] = $params[$key];
  150. }
  151. }
  152. } else {
  153. $this->sql[] = $sql;
  154. if ($params) {
  155. $this->params[count($this->sql) - 1] = $params;
  156. }
  157. }
  158. }
  159. /**
  160. * Write a migration SQL file to the given path
  161. *
  162. * @param string $path The path to write the migration SQL file.
  163. * @param string $direction The direction to execute.
  164. * @return bool $written
  165. */
  166. public function writeSqlFile($path, $direction = 'up')
  167. {
  168. $queries = $this->execute($direction, true);
  169. $string = sprintf("# Doctrine Migration File Generated on %s\n", date('Y-m-d H:m:s'));
  170. $string .= "\n# Version " . $this->version . "\n";
  171. foreach ($queries as $query) {
  172. $string .= $query . ";\n";
  173. }
  174. if (is_dir($path)) {
  175. $path = realpath($path);
  176. $path = $path . '/doctrine_migration_' . date('YmdHis') . '.sql';
  177. }
  178. $this->outputWriter->write("\n".sprintf('Writing migration file to "<info>%s</info>"', $path));
  179. return file_put_contents($path, $string);
  180. }
  181. /**
  182. * @return AbstractMigration
  183. */
  184. public function getMigration()
  185. {
  186. return $this->migration;
  187. }
  188. /**
  189. * Execute this migration version up or down and and return the SQL.
  190. *
  191. * @param string $direction The direction to execute the migration.
  192. * @param string $dryRun Whether to not actually execute the migration SQL and just do a dry run.
  193. * @return array $sql
  194. * @throws Exception when migration fails
  195. */
  196. public function execute($direction, $dryRun = false)
  197. {
  198. $this->sql = array();
  199. $this->connection->beginTransaction();
  200. try {
  201. $start = microtime(true);
  202. $this->state = self::STATE_PRE;
  203. $fromSchema = $this->sm->createSchema();
  204. $this->migration->{'pre' . ucfirst($direction)}($fromSchema);
  205. if ($direction === 'up') {
  206. $this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n");
  207. } else {
  208. $this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n");
  209. }
  210. $this->state = self::STATE_EXEC;
  211. $toSchema = clone $fromSchema;
  212. $this->migration->$direction($toSchema);
  213. $this->addSql($fromSchema->getMigrateToSql($toSchema, $this->platform));
  214. if ($dryRun === false) {
  215. if ($this->sql) {
  216. foreach ($this->sql as $key => $query) {
  217. if ( ! isset($this->params[$key])) {
  218. $this->outputWriter->write(' <comment>-></comment> ' . $query);
  219. $this->connection->executeQuery($query);
  220. } else {
  221. $this->outputWriter->write(sprintf(' <comment>-</comment> %s (with parameters)', $query));
  222. $this->connection->executeQuery($query, $this->params[$key]);
  223. }
  224. }
  225. } else {
  226. $this->outputWriter->write(sprintf('<error>Migration %s was executed but did not result in any SQL statements.</error>', $this->version));
  227. }
  228. if ($direction === 'up') {
  229. $this->markMigrated();
  230. } else {
  231. $this->markNotMigrated();
  232. }
  233. } else {
  234. foreach ($this->sql as $query) {
  235. $this->outputWriter->write(' <comment>-></comment> ' . $query);
  236. }
  237. }
  238. $this->state = self::STATE_POST;
  239. $this->migration->{'post' . ucfirst($direction)}($toSchema);
  240. $end = microtime(true);
  241. $this->time = round($end - $start, 2);
  242. if ($direction === 'up') {
  243. $this->outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->time));
  244. } else {
  245. $this->outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->time));
  246. }
  247. $this->connection->commit();
  248. return $this->sql;
  249. } catch(SkipMigrationException $e) {
  250. $this->connection->rollback();
  251. if ($dryRun == false) {
  252. // now mark it as migrated
  253. if ($direction === 'up') {
  254. $this->markMigrated();
  255. } else {
  256. $this->markNotMigrated();
  257. }
  258. }
  259. $this->outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage()));
  260. } catch (\Exception $e) {
  261. $this->outputWriter->write(sprintf(
  262. '<error>Migration %s failed during %s. Error %s</error>',
  263. $this->version, $this->getExecutionState(), $e->getMessage()
  264. ));
  265. $this->connection->rollback();
  266. $this->state = self::STATE_NONE;
  267. throw $e;
  268. }
  269. $this->state = self::STATE_NONE;
  270. }
  271. public function getExecutionState()
  272. {
  273. switch($this->state) {
  274. case self::STATE_PRE:
  275. return 'Pre-Checks';
  276. case self::STATE_POST:
  277. return 'Post-Checks';
  278. case self::STATE_EXEC:
  279. return 'Execution';
  280. default:
  281. return 'No State';
  282. }
  283. }
  284. /**
  285. * Returns the time this migration version took to execute
  286. *
  287. * @return integer $time The time this migration version took to execute
  288. */
  289. public function getTime()
  290. {
  291. return $this->time;
  292. }
  293. public function __toString()
  294. {
  295. return $this->version;
  296. }
  297. }