PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.3.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.3.0
4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Staging / Service / Database / TableCreateService.php
wp-staging / Staging / Service / Database Last commit date
RowsExporter.php 1 year ago TableCreateService.php 1 year ago
TableCreateService.php
113 lines
1 <?php
2
3 namespace WPStaging\Staging\Service\Database;
4
5 use RuntimeException;
6 use WPStaging\Framework\Adapter\Database;
7 use WPStaging\Framework\Database\TableService;
8 use WPStaging\Vendor\Psr\Log\LoggerInterface;
9
10 /**
11 * This class is similar to the src\Framework\CloningProcess\Database\DatabaseCloningService class.
12 * It is adjusted to use Backuper Logic and it only focus on creating a table in the staging site.
13 * @see src\Framework\CloningProcess\Database\DatabaseCloningService for existing logic
14 * @see src\Staging\Service\Database\RowsCopier for logic related to copying tables rows
15 */
16 class TableCreateService
17 {
18 /** @var LoggerInterface */
19 protected $logger;
20
21 /** @var Database */
22 protected $productionDb;
23
24 /** @var Database */
25 protected $stagingDb;
26
27 /** @var TableService */
28 protected $tableService;
29
30 /** @var string */
31 protected $databaseName;
32
33 /** @var string */
34 protected $productionPrefix;
35
36 /** @var string */
37 protected $stagingPrefix;
38
39 /**
40 * @param Database $database
41 */
42 public function __construct(Database $productionDb, TableService $tableService)
43 {
44 $this->productionDb = $productionDb;
45 $this->tableService = $tableService;
46 }
47
48 public function setup(LoggerInterface $logger, Database $stagingDb)
49 {
50 $this->logger = $logger;
51 $this->stagingDb = $stagingDb;
52 $this->databaseName = $this->productionDb->getWpdba()->getClient()->__get('dbname');
53 $this->productionPrefix = $this->productionDb->getPrefix();
54 $this->stagingPrefix = $this->stagingDb->getPrefix();
55 }
56
57 public function getDestinationTable(string $srcTableName): string
58 {
59 if (strpos($srcTableName, $this->productionPrefix) !== 0) {
60 return $this->stagingPrefix . $srcTableName;
61 }
62
63 return $this->stagingPrefix . substr($srcTableName, strlen($this->productionPrefix));
64 }
65
66 /**
67 * @param string $destTableName
68 * @param string $srcTableName
69 * @return void
70 */
71 public function createStagingSiteTable(string $srcTableName, string $destTableName)
72 {
73 $this->logger->info(sprintf("Creating table %s", esc_html($destTableName)));
74 $this->dropDestinationTableIfExists($destTableName);
75
76 $createTableQuery = $this->tableService->getCreateTableQuery($srcTableName);
77 if (empty($createTableQuery)) {
78 throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: Unable to find create table query");
79 }
80
81 $createTableQuery = str_replace($srcTableName, $destTableName, $createTableQuery);
82 $createTableQuery = $this->tableService->replaceTableConstraints($createTableQuery);
83 $createTableQuery = $this->tableService->replaceTableOptions($createTableQuery);
84 if (empty($createTableQuery)) {
85 throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: Unable to replace contraints");
86 }
87
88 $result = $this->stagingDb->getClient()->query($createTableQuery);
89 if ($result === false) {
90 $error = $this->stagingDb->getWpdb()->last_error;
91 throw new RuntimeException("Create Table - Cannot clone table $srcTableName to $destTableName. Error: $error");
92 }
93 }
94
95 /**
96 * @param string $destTableName
97 * @return void
98 */
99 private function dropDestinationTableIfExists(string $destTableName)
100 {
101 if (!$this->tableService->tableExists($destTableName)) {
102 return;
103 }
104
105 $this->logger->warning(sprintf("Create Table - Table %s already exists, dropping it first", esc_html($destTableName)));
106 if ($this->tableService->dropTable($destTableName)) {
107 return;
108 }
109
110 throw new RuntimeException("Create Table - Cannot drop table $destTableName");
111 }
112 }
113