ActiveTableSwitcher.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Model\ResourceModel\Indexer;
  7. /**
  8. * Logic for switching active and replica index tables.
  9. */
  10. class ActiveTableSwitcher
  11. {
  12. /**
  13. * Suffix for replica index table.
  14. *
  15. * @var string
  16. */
  17. private $additionalTableSuffix = '_replica';
  18. /**
  19. * Suffix for outdated index table.
  20. *
  21. * @var string
  22. */
  23. private $outdatedTableSuffix = '_outdated';
  24. /**
  25. * Switch index tables from replica to active.
  26. *
  27. * @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
  28. * @param array $tableNames
  29. * @return void
  30. */
  31. public function switchTable(\Magento\Framework\DB\Adapter\AdapterInterface $connection, array $tableNames)
  32. {
  33. $toRename = [];
  34. foreach ($tableNames as $tableName) {
  35. $outdatedTableName = $tableName . $this->outdatedTableSuffix;
  36. $replicaTableName = $tableName . $this->additionalTableSuffix;
  37. $renameBatch = [
  38. [
  39. 'oldName' => $tableName,
  40. 'newName' => $outdatedTableName
  41. ],
  42. [
  43. 'oldName' => $replicaTableName,
  44. 'newName' => $tableName
  45. ],
  46. [
  47. 'oldName' => $outdatedTableName,
  48. 'newName' => $replicaTableName
  49. ]
  50. ];
  51. $toRename = array_merge($toRename, $renameBatch);
  52. }
  53. if (!empty($toRename)) {
  54. $connection->renameTablesBatch($toRename);
  55. }
  56. }
  57. /**
  58. * @param string $tableName
  59. * @return string
  60. */
  61. public function getAdditionalTableName($tableName)
  62. {
  63. return $tableName . $this->additionalTableSuffix;
  64. }
  65. }