Configuration.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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\Configuration;
  20. use Doctrine\DBAL\Connection,
  21. Doctrine\DBAL\Migrations\MigrationException,
  22. Doctrine\DBAL\Migrations\Version,
  23. Doctrine\DBAL\Migrations\OutputWriter,
  24. Doctrine\DBAL\Schema\Table,
  25. Doctrine\DBAL\Schema\Column,
  26. Doctrine\DBAL\Types\Type;
  27. /**
  28. * Default Migration Configurtion object used for configuring an instance of
  29. * the Migration class. Set the connection, version table name, register migration
  30. * classes/versions, etc.
  31. *
  32. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  33. * @link www.doctrine-project.org
  34. * @since 2.0
  35. * @author Jonathan H. Wage <jonwage@gmail.com>
  36. */
  37. class Configuration
  38. {
  39. /**
  40. * Name of this set of migrations
  41. *
  42. * @var string
  43. */
  44. private $name;
  45. /**
  46. * Flag for whether or not the migration table has been created
  47. *
  48. * @var boolean
  49. */
  50. private $migrationTableCreated = false;
  51. /**
  52. * Connection instance to use for migrations
  53. *
  54. * @var Connection
  55. */
  56. private $connection;
  57. /**
  58. * OutputWriter instance for writing output during migrations
  59. *
  60. * @var OutputWriter
  61. */
  62. private $outputWriter;
  63. /**
  64. * The migration table name to track versions in
  65. *
  66. * @var string
  67. */
  68. private $migrationsTableName = 'doctrine_migration_versions';
  69. /**
  70. * The path to a directory where new migration classes will be written
  71. *
  72. * @var string
  73. */
  74. private $migrationsDirectory;
  75. /**
  76. * Namespace the migration classes live in
  77. *
  78. * @var string
  79. */
  80. private $migrationsNamespace;
  81. /**
  82. * Array of the registered migrations
  83. *
  84. * @var Version[]
  85. */
  86. private $migrations = array();
  87. /**
  88. * Construct a migration configuration object.
  89. *
  90. * @param Connection $connection A Connection instance
  91. * @param OutputWriter $outputWriter A OutputWriter instance
  92. */
  93. public function __construct(Connection $connection, OutputWriter $outputWriter = null)
  94. {
  95. $this->connection = $connection;
  96. if ($outputWriter === null) {
  97. $outputWriter = new OutputWriter();
  98. }
  99. $this->outputWriter = $outputWriter;
  100. }
  101. /**
  102. * Validation that this instance has all the required properties configured
  103. *
  104. * @return void
  105. * @throws MigrationException
  106. */
  107. public function validate()
  108. {
  109. if ( ! $this->migrationsNamespace) {
  110. throw MigrationException::migrationsNamespaceRequired();
  111. }
  112. if ( ! $this->migrationsDirectory) {
  113. throw MigrationException::migrationsDirectoryRequired();
  114. }
  115. }
  116. /**
  117. * Set the name of this set of migrations
  118. *
  119. * @param string $name The name of this set of migrations
  120. */
  121. public function setName($name)
  122. {
  123. $this->name = $name;
  124. }
  125. /**
  126. * Returns the name of this set of migrations
  127. *
  128. * @return string $name The name of this set of migrations
  129. */
  130. public function getName()
  131. {
  132. return $this->name;
  133. }
  134. /**
  135. * Returns the OutputWriter instance
  136. *
  137. * @return OutputWriter $outputWriter The OutputWriter instance
  138. */
  139. public function getOutputWriter()
  140. {
  141. return $this->outputWriter;
  142. }
  143. /**
  144. * Returns a timestamp version as a formatted date
  145. *
  146. * @param string $version
  147. *
  148. * @return string The formatted version
  149. */
  150. public function formatVersion($version)
  151. {
  152. return sprintf('%s-%s-%s %s:%s:%s',
  153. substr($version, 0, 4),
  154. substr($version, 4, 2),
  155. substr($version, 6, 2),
  156. substr($version, 8, 2),
  157. substr($version, 10, 2),
  158. substr($version, 12, 2)
  159. );
  160. }
  161. /**
  162. * Returns the Connection instance
  163. *
  164. * @return Connection $connection The Connection instance
  165. */
  166. public function getConnection()
  167. {
  168. return $this->connection;
  169. }
  170. /**
  171. * Set the migration table name
  172. *
  173. * @param string $tableName The migration table name
  174. */
  175. public function setMigrationsTableName($tableName)
  176. {
  177. $this->migrationsTableName = $tableName;
  178. }
  179. /**
  180. * Returns the migration table name
  181. *
  182. * @return string $migrationsTableName The migration table name
  183. */
  184. public function getMigrationsTableName()
  185. {
  186. return $this->migrationsTableName;
  187. }
  188. /**
  189. * Set the new migrations directory where new migration classes are generated
  190. *
  191. * @param string $migrationsDirectory The new migrations directory
  192. */
  193. public function setMigrationsDirectory($migrationsDirectory)
  194. {
  195. $this->migrationsDirectory = $migrationsDirectory;
  196. }
  197. /**
  198. * Returns the new migrations directory where new migration classes are generated
  199. *
  200. * @return string $migrationsDirectory The new migrations directory
  201. */
  202. public function getMigrationsDirectory()
  203. {
  204. return $this->migrationsDirectory;
  205. }
  206. /**
  207. * Set the migrations namespace
  208. *
  209. * @param string $migrationsNamespace The migrations namespace
  210. */
  211. public function setMigrationsNamespace($migrationsNamespace)
  212. {
  213. $this->migrationsNamespace = $migrationsNamespace;
  214. }
  215. /**
  216. * Returns the migrations namespace
  217. *
  218. * @return string $migrationsNamespace The migrations namespace
  219. */
  220. public function getMigrationsNamespace()
  221. {
  222. return $this->migrationsNamespace;
  223. }
  224. /**
  225. * Register migrations from a given directory. Recursively finds all files
  226. * with the pattern VersionYYYYMMDDHHMMSS.php as the filename and registers
  227. * them as migrations.
  228. *
  229. * @param string $path The root directory to where some migration classes live.
  230. *
  231. * @return Version[] The array of migrations registered.
  232. */
  233. public function registerMigrationsFromDirectory($path)
  234. {
  235. $path = realpath($path);
  236. $path = rtrim($path, '/');
  237. $files = glob($path . '/Version*.php');
  238. $versions = array();
  239. if ($files) {
  240. foreach ($files as $file) {
  241. require_once($file);
  242. $info = pathinfo($file);
  243. $version = substr($info['filename'], 7);
  244. $class = $this->migrationsNamespace . '\\' . $info['filename'];
  245. $versions[] = $this->registerMigration($version, $class);
  246. }
  247. }
  248. return $versions;
  249. }
  250. /**
  251. * Register a single migration version to be executed by a AbstractMigration
  252. * class.
  253. *
  254. * @param string $version The version of the migration in the format YYYYMMDDHHMMSS.
  255. * @param string $class The migration class to execute for the version.
  256. *
  257. * @return Version
  258. *
  259. * @throws MigrationException
  260. */
  261. public function registerMigration($version, $class)
  262. {
  263. $version = (string) $version;
  264. $class = (string) $class;
  265. if (isset($this->migrations[$version])) {
  266. throw MigrationException::duplicateMigrationVersion($version, get_class($this->migrations[$version]));
  267. }
  268. $version = new Version($this, $version, $class);
  269. $this->migrations[$version->getVersion()] = $version;
  270. ksort($this->migrations);
  271. return $version;
  272. }
  273. /**
  274. * Register an array of migrations. Each key of the array is the version and
  275. * the value is the migration class name.
  276. *
  277. *
  278. * @param array $migrations
  279. *
  280. * @return Version[]
  281. */
  282. public function registerMigrations(array $migrations)
  283. {
  284. $versions = array();
  285. foreach ($migrations as $version => $class) {
  286. $versions[] = $this->registerMigration($version, $class);
  287. }
  288. return $versions;
  289. }
  290. /**
  291. * Get the array of registered migration versions.
  292. *
  293. * @return Version[] $migrations
  294. */
  295. public function getMigrations()
  296. {
  297. return $this->migrations;
  298. }
  299. /**
  300. * Returns the Version instance for a given version in the format YYYYMMDDHHMMSS.
  301. *
  302. * @param string $version The version string in the format YYYYMMDDHHMMSS.
  303. *
  304. * @return Version
  305. *
  306. * @throws MigrationException Throws exception if migration version does not exist.
  307. */
  308. public function getVersion($version)
  309. {
  310. if ( ! isset($this->migrations[$version])) {
  311. throw MigrationException::unknownMigrationVersion($version);
  312. }
  313. return $this->migrations[$version];
  314. }
  315. /**
  316. * Check if a version exists.
  317. *
  318. * @param string $version
  319. *
  320. * @return boolean
  321. */
  322. public function hasVersion($version)
  323. {
  324. return isset($this->migrations[$version]);
  325. }
  326. /**
  327. * Check if a version has been migrated or not yet
  328. *
  329. * @param Version $version
  330. *
  331. * @return boolean
  332. */
  333. public function hasVersionMigrated(Version $version)
  334. {
  335. $this->createMigrationTable();
  336. $version = $this->connection->fetchColumn("SELECT version FROM " . $this->migrationsTableName . " WHERE version = ?", array($version->getVersion()));
  337. return $version !== false;
  338. }
  339. /**
  340. * Returns all migrated versions from the versions table, in an array.
  341. *
  342. * @return Version[]
  343. */
  344. public function getMigratedVersions()
  345. {
  346. $this->createMigrationTable();
  347. $ret = $this->connection->fetchAll("SELECT version FROM " . $this->migrationsTableName);
  348. $versions = array();
  349. foreach ($ret as $version) {
  350. $versions[] = current($version);
  351. }
  352. return $versions;
  353. }
  354. /**
  355. * Returns an array of available migration version numbers.
  356. *
  357. * @return array
  358. */
  359. public function getAvailableVersions()
  360. {
  361. $availableVersions = array();
  362. foreach ($this->migrations as $migration) {
  363. $availableVersions[] = $migration->getVersion();
  364. }
  365. return $availableVersions;
  366. }
  367. /**
  368. * Returns the current migrated version from the versions table.
  369. *
  370. * @return string
  371. */
  372. public function getCurrentVersion()
  373. {
  374. $this->createMigrationTable();
  375. $where = null;
  376. if ($this->migrations) {
  377. $migratedVersions = array();
  378. foreach ($this->migrations as $migration) {
  379. $migratedVersions[] = sprintf("'%s'", $migration->getVersion());
  380. }
  381. $where = " WHERE version IN (" . implode(', ', $migratedVersions) . ")";
  382. }
  383. $sql = sprintf("SELECT version FROM %s%s ORDER BY version DESC",
  384. $this->migrationsTableName, $where
  385. );
  386. $sql = $this->connection->getDatabasePlatform()->modifyLimitQuery($sql, 1);
  387. $result = $this->connection->fetchColumn($sql);
  388. return $result !== false ? (string) $result : '0';
  389. }
  390. /**
  391. * Returns the total number of executed migration versions
  392. *
  393. * @return integer
  394. */
  395. public function getNumberOfExecutedMigrations()
  396. {
  397. $this->createMigrationTable();
  398. $result = $this->connection->fetchColumn("SELECT COUNT(version) FROM " . $this->migrationsTableName);
  399. return $result !== false ? $result : 0;
  400. }
  401. /**
  402. * Returns the total number of available migration versions
  403. *
  404. * @return integer
  405. */
  406. public function getNumberOfAvailableMigrations()
  407. {
  408. return count($this->migrations);
  409. }
  410. /**
  411. * Returns the latest available migration version.
  412. *
  413. * @return string The version string in the format YYYYMMDDHHMMSS.
  414. */
  415. public function getLatestVersion()
  416. {
  417. $versions = array_keys($this->migrations);
  418. $latest = end($versions);
  419. return $latest !== false ? (string) $latest : '0';
  420. }
  421. /**
  422. * Create the migration table to track migrations with.
  423. *
  424. * @return boolean Whether or not the table was created.
  425. */
  426. public function createMigrationTable()
  427. {
  428. $this->validate();
  429. if ($this->migrationTableCreated) {
  430. return false;
  431. }
  432. if ( ! $this->connection->getSchemaManager()->tablesExist(array($this->migrationsTableName))) {
  433. $columns = array(
  434. 'version' => new Column('version', Type::getType('string'), array('length' => 255)),
  435. );
  436. $table = new Table($this->migrationsTableName, $columns);
  437. $table->setPrimaryKey(array('version'));
  438. $this->connection->getSchemaManager()->createTable($table);
  439. $this->migrationTableCreated = true;
  440. return true;
  441. }
  442. return false;
  443. }
  444. /**
  445. * Returns the array of migrations to executed based on the given direction
  446. * and target version number.
  447. *
  448. * @param string $direction The direction we are migrating.
  449. * @param string $to The version to migrate to.
  450. *
  451. * @return Version[] $migrations The array of migrations we can execute.
  452. */
  453. public function getMigrationsToExecute($direction, $to)
  454. {
  455. if ($direction === 'down') {
  456. if (count($this->migrations)) {
  457. $allVersions = array_reverse(array_keys($this->migrations));
  458. $classes = array_reverse(array_values($this->migrations));
  459. $allVersions = array_combine($allVersions, $classes);
  460. } else {
  461. $allVersions = array();
  462. }
  463. } else {
  464. $allVersions = $this->migrations;
  465. }
  466. $versions = array();
  467. $migrated = $this->getMigratedVersions();
  468. foreach ($allVersions as $version) {
  469. if ($this->shouldExecuteMigration($direction, $version, $to, $migrated)) {
  470. $versions[$version->getVersion()] = $version;
  471. }
  472. }
  473. return $versions;
  474. }
  475. /**
  476. * Check if we should execute a migration for a given direction and target
  477. * migration version.
  478. *
  479. * @param string $direction The direction we are migrating.
  480. * @param Version $version The Version instance to check.
  481. * @param string $to The version we are migrating to.
  482. * @param array $migrated Migrated versions array.
  483. *
  484. * @return boolean
  485. */
  486. private function shouldExecuteMigration($direction, Version $version, $to, $migrated)
  487. {
  488. if ($direction === 'down') {
  489. if ( ! in_array($version->getVersion(), $migrated)) {
  490. return false;
  491. }
  492. return $version->getVersion() > $to;
  493. }
  494. if ($direction === 'up') {
  495. if (in_array($version->getVersion(), $migrated)) {
  496. return false;
  497. }
  498. return $version->getVersion() <= $to;
  499. }
  500. }
  501. }