Mysql.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * MySQL platform database handler
  8. */
  9. namespace Magento\TestFramework\Db;
  10. class Mysql extends \Magento\TestFramework\Db\AbstractDb
  11. {
  12. /**
  13. * Default port
  14. */
  15. const DEFAULT_PORT = 3306;
  16. /**
  17. * Defaults extra file name
  18. */
  19. const DEFAULTS_EXTRA_FILE_NAME = 'defaults_extra.cnf';
  20. /**
  21. * MySQL DB dump file
  22. *
  23. * @var string
  24. */
  25. private $_dbDumpFile;
  26. /**
  27. * A file that contains credentials to database, to obscure them from logs
  28. *
  29. * @var string
  30. */
  31. private $_defaultsExtraFile;
  32. /**
  33. * Port number for connection
  34. *
  35. * @var integer
  36. */
  37. private $_port;
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function __construct($host, $user, $password, $schema, $varPath, \Magento\Framework\Shell $shell)
  42. {
  43. parent::__construct($host, $user, $password, $schema, $varPath, $shell);
  44. $this->_port = self::DEFAULT_PORT;
  45. if (strpos($this->_host, ':') !== false) {
  46. list($host, $port) = explode(':', $this->_host);
  47. $this->_host = $host;
  48. $this->_port = (int) $port;
  49. }
  50. $this->_dbDumpFile = $this->_varPath . '/setup_dump_' . $this->_schema . '.sql';
  51. $this->_defaultsExtraFile = rtrim($this->_varPath, '\\/') . '/' . self::DEFAULTS_EXTRA_FILE_NAME;
  52. }
  53. /**
  54. * Remove all DB objects
  55. */
  56. public function cleanup()
  57. {
  58. $this->ensureDefaultsExtraFile();
  59. $this->_shell->execute(
  60. 'mysql --defaults-file=%s --host=%s --port=%s %s -e %s',
  61. [
  62. $this->_defaultsExtraFile,
  63. $this->_host,
  64. $this->_port,
  65. $this->_schema,
  66. "DROP DATABASE `{$this->_schema}`; CREATE DATABASE `{$this->_schema}`"
  67. ]
  68. );
  69. }
  70. /**
  71. * Get filename for setup db dump
  72. *
  73. * @return string
  74. */
  75. protected function getSetupDbDumpFilename()
  76. {
  77. return $this->_dbDumpFile;
  78. }
  79. /**
  80. * Is dump exists
  81. *
  82. * @return bool
  83. */
  84. public function isDbDumpExists()
  85. {
  86. return file_exists($this->getSetupDbDumpFilename());
  87. }
  88. /**
  89. * Store setup db dump
  90. */
  91. public function storeDbDump()
  92. {
  93. $this->ensureDefaultsExtraFile();
  94. $this->_shell->execute(
  95. 'mysqldump --defaults-file=%s --host=%s --port=%s %s > %s',
  96. [$this->_defaultsExtraFile, $this->_host, $this->_port, $this->_schema, $this->getSetupDbDumpFilename()]
  97. );
  98. }
  99. /**
  100. * {@inheritdoc}
  101. * @throws \LogicException
  102. */
  103. public function restoreFromDbDump()
  104. {
  105. $this->ensureDefaultsExtraFile();
  106. if (!$this->isDbDumpExists()) {
  107. throw new \LogicException("DB dump file does not exist: " . $this->getSetupDbDumpFilename());
  108. }
  109. $this->_shell->execute(
  110. 'mysql --defaults-file=%s --host=%s --port=%s %s < %s',
  111. [$this->_defaultsExtraFile, $this->_host, $this->_port, $this->_schema, $this->getSetupDbDumpFilename()]
  112. );
  113. }
  114. /**
  115. * {@inheritdoc}
  116. */
  117. public function getVendorName()
  118. {
  119. return 'mysql';
  120. }
  121. /**
  122. * Create defaults extra file
  123. *
  124. * @return void
  125. */
  126. private function ensureDefaultsExtraFile()
  127. {
  128. if (!file_exists($this->_defaultsExtraFile)) {
  129. $this->assertVarPathWritable();
  130. $extraConfig = ['[client]', 'user=' . $this->_user, 'password="' . $this->_password . '"'];
  131. file_put_contents($this->_defaultsExtraFile, implode(PHP_EOL, $extraConfig));
  132. chmod($this->_defaultsExtraFile, 0640);
  133. }
  134. }
  135. }