From ae202e8a5fe5e55c4cd2ecb9d8fd3e523bdc5a5e Mon Sep 17 00:00:00 2001 From: theurtin Date: Wed, 8 Nov 2023 15:54:57 +0100 Subject: [PATCH 1/3] Handle backup --- src/Settings/SettingsManager.php | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Settings/SettingsManager.php b/src/Settings/SettingsManager.php index af241d8..cf6b506 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"; } /** From 7a0c8d86216807547e2f784775d19271dc4ae39e Mon Sep 17 00:00:00 2001 From: theurtin Date: Wed, 8 Nov 2023 16:03:04 +0100 Subject: [PATCH 2/3] Handle push --- src/Settings/SettingsManager.php | 34 ++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Settings/SettingsManager.php b/src/Settings/SettingsManager.php index cf6b506..670b3ea 100644 --- a/src/Settings/SettingsManager.php +++ b/src/Settings/SettingsManager.php @@ -88,18 +88,36 @@ public function push(array $params) $output = []; foreach ($indices as $indexName) { - $filename = $this->getFileName($indexName, 'settings'); + $this->pushIndice($indexName, $output); + } + + 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); - $index->setSettings($settings); + if (is_readable($filename)) { + $index = $this->algolia->initIndex($indexName); + $settings = json_decode(file_get_contents($filename), true); - $output[] = "Pushed settings for $indexName"; + if (array_key_exists('replicas', $settings)) { + foreach ( $settings['replicas'] as &$replica ) { + $replica = $this->config['prefix'] . $replica; + + $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"; + } } /** From 5705abf4d45bff4ab9c5a1559e13309ceafe4ddb Mon Sep 17 00:00:00 2001 From: theurtin Date: Wed, 8 Nov 2023 16:43:17 +0100 Subject: [PATCH 3/3] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc7793d..5da257e 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/6.0.1...master) +### Added +- Automatically backup replicas settings ## [v6.0.1](https://github.com/algolia/search-bundle/compare/6.0.0...6.0.1) ### Changed