TableCreateService.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Staging\Service\Database; |
| 4 | |
| 5 | use RuntimeException; |
| 6 | use WPStaging\Backup\Service\Database\DatabaseImporter; |
| 7 | use WPStaging\Framework\Adapter\Database; |
| 8 | use WPStaging\Framework\Database\TableService; |
| 9 | use WPStaging\Vendor\Psr\Log\LoggerInterface; |
| 10 | |
| 11 | /** |
| 12 | * This class is similar to the src\Framework\CloningProcess\Database\DatabaseCloningService class. |
| 13 | * It is adjusted to use Backuper Logic and it only focus on creating a table in the staging site. |
| 14 | * @see src\Framework\CloningProcess\Database\DatabaseCloningService for existing logic |
| 15 | * @see src\Staging\Service\Database\RowsCopier for logic related to copying tables rows |
| 16 | */ |
| 17 | class TableCreateService |
| 18 | { |
| 19 | /** @var LoggerInterface */ |
| 20 | protected $logger; |
| 21 | |
| 22 | /** @var Database */ |
| 23 | protected $sourceDb; |
| 24 | |
| 25 | /** @var Database */ |
| 26 | protected $destinationDb; |
| 27 | |
| 28 | /** @var TableService */ |
| 29 | protected $tableService; |
| 30 | |
| 31 | /** @var string */ |
| 32 | protected $databaseName; |
| 33 | |
| 34 | /** @var string */ |
| 35 | protected $sourcePrefix; |
| 36 | |
| 37 | /** @var string */ |
| 38 | protected $destinationPrefix; |
| 39 | |
| 40 | /** @var bool */ |
| 41 | protected $isResetExistingTables = false; |
| 42 | |
| 43 | /** |
| 44 | * @param Database $database |
| 45 | */ |
| 46 | public function __construct(Database $sourceDb, TableService $tableService) |
| 47 | { |
| 48 | $this->sourceDb = $sourceDb; |
| 49 | $this->tableService = $tableService; |
| 50 | } |
| 51 | |
| 52 | public function setup(LoggerInterface $logger, Database $destinationDb) |
| 53 | { |
| 54 | $this->logger = $logger; |
| 55 | $this->destinationDb = $destinationDb; |
| 56 | $this->databaseName = $this->sourceDb->getWpdba()->getClient()->__get('dbname'); |
| 57 | $this->sourcePrefix = $this->sourceDb->getPrefix(); |
| 58 | $this->destinationPrefix = $this->destinationDb->getPrefix(); |
| 59 | } |
| 60 | |
| 61 | public function getTableWithoutPrefix(string $srcTableName): string |
| 62 | { |
| 63 | if (strpos($srcTableName, $this->sourcePrefix) !== 0) { |
| 64 | return $srcTableName; |
| 65 | } |
| 66 | |
| 67 | return substr($srcTableName, strlen($this->sourcePrefix)); |
| 68 | } |
| 69 | |
| 70 | public function getDestinationTable(string $srcTableName): string |
| 71 | { |
| 72 | if (strpos($srcTableName, $this->sourcePrefix) !== 0) { |
| 73 | return $this->destinationPrefix . $srcTableName; |
| 74 | } |
| 75 | |
| 76 | return $this->destinationPrefix . substr($srcTableName, strlen($this->sourcePrefix)); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param bool $isResetExistingTables |
| 81 | * @return void |
| 82 | */ |
| 83 | public function setIsResetExistingTables(bool $isResetExistingTables) |
| 84 | { |
| 85 | $this->isResetExistingTables = $isResetExistingTables; |
| 86 | } |
| 87 | |
| 88 | public function isTableExist(string $tableName): bool |
| 89 | { |
| 90 | return $this->tableService->tableExists($tableName); |
| 91 | } |
| 92 | |
| 93 | public function preserveExistingTable(string $tableName, string $tableWithoutPrefix): string |
| 94 | { |
| 95 | $newTableName = DatabaseImporter::TMP_DATABASE_PREFIX_TO_DROP . $tableWithoutPrefix; |
| 96 | $this->logger->info(sprintf("Preserving existing table %s by renaming it to %s", esc_html($tableName), esc_html($newTableName))); |
| 97 | if ($this->tableService->renameTable($tableName, $newTableName)) { |
| 98 | return $newTableName; |
| 99 | } |
| 100 | |
| 101 | throw new RuntimeException("Cleanup Table - Cannot preserve existing table. Error: Unable to rename table $tableName to $newTableName"); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @param string $destTableName |
| 106 | * @param string $srcTableName |
| 107 | * @return void |
| 108 | */ |
| 109 | public function createDestinationTable(string $srcTableName, string $destTableName) |
| 110 | { |
| 111 | $this->logger->info(sprintf("Creating table %s", esc_html($destTableName))); |
| 112 | $this->dropDestinationTableIfExists($destTableName); |
| 113 | |
| 114 | $createTableQuery = $this->tableService->getCreateTableQuery($srcTableName); |
| 115 | if (empty($createTableQuery)) { |
| 116 | throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: Unable to find create table query"); |
| 117 | } |
| 118 | |
| 119 | $createTableQuery = str_replace($srcTableName, $destTableName, $createTableQuery); |
| 120 | $createTableQuery = $this->tableService->replaceTableConstraints($createTableQuery); |
| 121 | $createTableQuery = $this->tableService->replaceTableOptions($createTableQuery); |
| 122 | if (empty($createTableQuery)) { |
| 123 | throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: Unable to replace contraints"); |
| 124 | } |
| 125 | |
| 126 | $result = $this->destinationDb->getClient()->query($createTableQuery); |
| 127 | if ($result === false) { |
| 128 | $error = $this->destinationDb->getWpdb()->last_error; |
| 129 | throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: $error"); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param string $destTableName |
| 135 | * @return void |
| 136 | */ |
| 137 | protected function dropDestinationTableIfExists(string $destTableName) |
| 138 | { |
| 139 | if (!$this->tableService->tableExists($destTableName)) { |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | if (!$this->isResetExistingTables) { |
| 144 | throw new RuntimeException("Create Table - Cannot clone table. Error: Destination table $destTableName already exists."); |
| 145 | } |
| 146 | |
| 147 | $this->logger->warning(sprintf("Create Table - Table %s already exists, dropping it first", esc_html($destTableName))); |
| 148 | if ($this->tableService->dropTable($destTableName)) { |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | throw new RuntimeException("Create Table - Cannot drop table $destTableName"); |
| 153 | } |
| 154 | } |
| 155 |