diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ffbd1b..6c76d3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p # Release Notes ## [Unreleased](https://github.com/algolia/search-bundle/compare/7.0.0...master) +### Added +- Automatically backup replicas settings ## [v7.0.0](https://github.com/algolia/search-bundle/compare/6.0.1...7.0.0) diff --git a/src/Settings/SettingsManager.php b/src/Settings/SettingsManager.php index af241d8..670b3ea 100644 --- a/src/Settings/SettingsManager.php +++ b/src/Settings/SettingsManager.php @@ -45,16 +45,36 @@ public function backup(array $params) } foreach ($indices as $indexName) { - $index = $this->algolia->initIndex($indexName); - $settings = $index->getSettings(); - $filename = $this->getFileName($indexName, 'settings'); + $this->backupIndice($indexName, $fs, $output); + } + + return $output; + } + + private function backupIndice($indexName, $fs, &$output): void + { + $index = $this->algolia->initIndex($indexName); + $settings = $index->getSettings(); - $fs->dumpFile($filename, json_encode($settings, JSON_PRETTY_PRINT)); + // Handle replicas + if (array_key_exists('replicas', $settings)) { + foreach($settings['replicas'] as &$replica) { + // Backup replica settings + $this->backupIndice($replica, $fs, $output); - $output[] = "Saved settings for $indexName in $filename"; + $replica = $this->removePrefixFromIndexName($replica); + } } - return $output; + if (array_key_exists('primary', $settings)) { + $settings['primary'] = $this->removePrefixFromIndexName($settings['primary']); + } + + $filename = $this->getFileName($indexName, 'settings'); + + $fs->dumpFile($filename, json_encode($settings, JSON_PRETTY_PRINT)); + + $output[] = "Saved settings for $indexName in $filename"; } /** @@ -68,18 +88,36 @@ public function push(array $params) $output = []; foreach ($indices as $indexName) { - $filename = $this->getFileName($indexName, 'settings'); + $this->pushIndice($indexName, $output); + } - if (is_readable($filename)) { - $index = $this->algolia->initIndex($indexName); - $settings = json_decode(file_get_contents($filename), true); - $index->setSettings($settings); + return $output; + } + + private function pushIndice($indexName, &$output): void + { + $filename = $this->getFileName($indexName, 'settings'); + + if (is_readable($filename)) { + $index = $this->algolia->initIndex($indexName); + $settings = json_decode(file_get_contents($filename), true); + + if (array_key_exists('replicas', $settings)) { + foreach ( $settings['replicas'] as &$replica ) { + $replica = $this->config['prefix'] . $replica; - $output[] = "Pushed settings for $indexName"; + $this->pushIndice($replica, $output); + } } - } - return $output; + if (array_key_exists('primary', $settings)) { + $settings['primary'] = $this->config['prefix'] . $settings['primary']; + } + + $index->setSettings($settings); + + $output[] = "Pushed settings for $indexName"; + } } /**