IndexTableSwitcher.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryMultiDimensionalIndexerApi\Model;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. /**
  11. * @inheritdoc
  12. */
  13. class IndexTableSwitcher implements IndexTableSwitcherInterface
  14. {
  15. /**
  16. * TODO: move to separate configurable interface (https://github.com/magento-engcom/msi/issues/213)
  17. * Suffix for replica index table
  18. *
  19. * @var string
  20. */
  21. private $replicaTableSuffix = '_replica';
  22. /**
  23. * TODO: move to separate configurable interface (https://github.com/magento-engcom/msi/issues/213)
  24. * Suffix for outdated index table
  25. *
  26. * @var string
  27. */
  28. private $outdatedTableSuffix = '_outdated';
  29. /**
  30. * @var ResourceConnection
  31. */
  32. private $resourceConnection;
  33. /**
  34. * @var IndexNameResolverInterface
  35. */
  36. private $indexNameResolver;
  37. /**
  38. * @param ResourceConnection $resourceConnection
  39. * @param IndexNameResolverInterface $indexNameResolver
  40. */
  41. public function __construct(
  42. ResourceConnection $resourceConnection,
  43. IndexNameResolverInterface $indexNameResolver
  44. ) {
  45. $this->resourceConnection = $resourceConnection;
  46. $this->indexNameResolver = $indexNameResolver;
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function switch(IndexName $indexName, string $connectionName): void
  52. {
  53. $connection = $this->resourceConnection->getConnection($connectionName);
  54. $tableName = $this->indexNameResolver->resolveName($indexName);
  55. $this->switchTable($connection, [$tableName]);
  56. }
  57. /**
  58. * Switch index tables from replica to active
  59. *
  60. * @param AdapterInterface $connection
  61. * @param array $tableNames
  62. * @return void
  63. */
  64. private function switchTable(AdapterInterface $connection, array $tableNames)
  65. {
  66. $toRename = [];
  67. foreach ($tableNames as $tableName) {
  68. $outdatedTableName = $tableName . $this->outdatedTableSuffix;
  69. $replicaTableName = $tableName . $this->replicaTableSuffix;
  70. $renameBatch = [
  71. [
  72. 'oldName' => $tableName,
  73. 'newName' => $outdatedTableName,
  74. ],
  75. [
  76. 'oldName' => $replicaTableName,
  77. 'newName' => $tableName,
  78. ],
  79. [
  80. 'oldName' => $outdatedTableName,
  81. 'newName' => $replicaTableName,
  82. ]
  83. ];
  84. $toRename = array_merge($toRename, $renameBatch);
  85. }
  86. if (!empty($toRename)) {
  87. $connection->renameTablesBatch($toRename);
  88. }
  89. }
  90. }