Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexTableSwitcher.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
12 
17 {
24  private $replicaTableSuffix = '_replica';
25 
32  private $outdatedTableSuffix = '_outdated';
33 
37  private $resourceConnection;
38 
42  private $indexNameResolver;
43 
48  public function __construct(
49  ResourceConnection $resourceConnection,
50  IndexNameResolverInterface $indexNameResolver
51  ) {
52  $this->resourceConnection = $resourceConnection;
53  $this->indexNameResolver = $indexNameResolver;
54  }
55 
59  public function switch(IndexName $indexName, string $connectionName): void
60  {
61  $connection = $this->resourceConnection->getConnection($connectionName);
62  $tableName = $this->indexNameResolver->resolveName($indexName);
63 
64  $this->switchTable($connection, [$tableName]);
65  }
66 
74  private function switchTable(AdapterInterface $connection, array $tableNames)
75  {
76  $toRename = [];
77  foreach ($tableNames as $tableName) {
78  $outdatedTableName = $tableName . $this->outdatedTableSuffix;
79  $replicaTableName = $tableName . $this->replicaTableSuffix;
80 
81  $renameBatch = [
82  [
83  'oldName' => $tableName,
84  'newName' => $outdatedTableName,
85  ],
86  [
87  'oldName' => $replicaTableName,
88  'newName' => $tableName,
89  ],
90  [
91  'oldName' => $outdatedTableName,
92  'newName' => $replicaTableName,
93  ]
94  ];
95  $toRename = array_merge($toRename, $renameBatch);
96  }
97 
98  if (!empty($toRename)) {
99  $connection->renameTablesBatch($toRename);
100  }
101  }
102 }
$tableName
Definition: trigger.php:13
$connection
Definition: bulk.php:13
__construct(ResourceConnection $resourceConnection, IndexNameResolverInterface $indexNameResolver)