From 7778b8173b3e39d2d4936a409abe6328fbe1bd57 Mon Sep 17 00:00:00 2001 From: Erki Aas Date: Tue, 3 Feb 2026 19:16:59 +0200 Subject: [PATCH] Add limit option to Elasticdump --- src/Backup/Source/Elasticdump.php | 10 +++++++++ src/Cli/Executable/Elasticdump.php | 21 +++++++++++++++++++ tests/phpbu/Backup/Source/ElasticdumpTest.php | 15 +++++++++++++ .../phpbu/Cli/Executable/ElasticdumpTest.php | 12 +++++++++++ 4 files changed, 58 insertions(+) diff --git a/src/Backup/Source/Elasticdump.php b/src/Backup/Source/Elasticdump.php index 3712f274..73ff5444 100644 --- a/src/Backup/Source/Elasticdump.php +++ b/src/Backup/Source/Elasticdump.php @@ -64,6 +64,14 @@ class Elasticdump extends SimulatorExecutable implements Simulator */ private $type; + /** + * Number of documents to dump. + * --limit + * + * @var string + */ + private $limit; + /** * Setup * @@ -89,6 +97,7 @@ protected function setupSourceData(array $conf) { $this->index = Util\Arr::getValue($conf, 'index', ''); $this->type = Util\Arr::getValue($conf, 'type', ''); + $this->limit = Util\Arr::getValue($conf, 'limit', ''); } /** @@ -126,6 +135,7 @@ protected function createExecutable(Target $target) : Executable ->credentials($this->user, $this->password) ->dumpIndex($this->index) ->dumpType($this->type) + ->limit($this->limit) ->dumpTo($target->getPathnamePlain()); return $executable; } diff --git a/src/Cli/Executable/Elasticdump.php b/src/Cli/Executable/Elasticdump.php index c8180d27..1a67d1ab 100644 --- a/src/Cli/Executable/Elasticdump.php +++ b/src/Cli/Executable/Elasticdump.php @@ -65,6 +65,14 @@ class Elasticdump extends Abstraction */ private $dumpPathname; + /** + * Number of documents to dump. + * --limit + * + * @var string + */ + private $limit; + /** * Constructor. * @@ -124,6 +132,18 @@ public function dumpTo($pathname) return $this; } + /** + * Set limit of documents to dump. + * + * @param string $limit + * @return \phpbu\App\Cli\Executable\Elasticdump + */ + public function limit($limit) + { + $this->limit = $limit; + return $this; + } + /** * Set elastic credentials. * @@ -159,6 +179,7 @@ protected function createCommandLine() : CommandLine $cmd->addOption('--input', $this->generateNodeUrl($this->host, $this->user, $this->password, $this->index)); $cmd->addOptionIfNotEmpty('--type', $this->type); + $cmd->addOptionIfNotEmpty('--limit', $this->limit); $cmd->addOption('--output', $this->dumpPathname); return $process; diff --git a/tests/phpbu/Backup/Source/ElasticdumpTest.php b/tests/phpbu/Backup/Source/ElasticdumpTest.php index 4642d2eb..4ff33264 100644 --- a/tests/phpbu/Backup/Source/ElasticdumpTest.php +++ b/tests/phpbu/Backup/Source/ElasticdumpTest.php @@ -52,6 +52,21 @@ public function testUser() $this->assertEquals('"' . PHPBU_TEST_BIN . '/' . $expected, $executable->getCommand()); } + /** + * Tests Elasticdump::getExecutable + */ + public function testLimit() + { + $target = $this->createTargetMock('backup.json'); + $elasticdump = new Elasticdump(); + $elasticdump->setup(['pathToElasticdump' => PHPBU_TEST_BIN, 'limit' => '100']); + + $executable = $elasticdump->getExecutable($target); + $expected = 'elasticdump" --input=\'http://localhost:9200/\' --limit=\'100\' --output=\'backup.json\''; + + $this->assertEquals('"' . PHPBU_TEST_BIN . '/' . $expected, $executable->getCommand()); + } + /** * Tests Elasticdump::backup */ diff --git a/tests/phpbu/Cli/Executable/ElasticdumpTest.php b/tests/phpbu/Cli/Executable/ElasticdumpTest.php index 695395a7..dfca8385 100644 --- a/tests/phpbu/Cli/Executable/ElasticdumpTest.php +++ b/tests/phpbu/Cli/Executable/ElasticdumpTest.php @@ -77,6 +77,18 @@ public function testType() $this->assertEquals('"' . PHPBU_TEST_BIN . '/' . $expected, $elastic->getCommand()); } + /** + * Tests Elasticdump::createCommand + */ + public function testLimit() + { + $expected = 'elasticdump" --input=\'http://localhost:9200/\' --limit=\'500\' --output=\'./foo.json\''; + $elastic = new Elasticdump(PHPBU_TEST_BIN); + $elastic->useHost('localhost:9200')->limit('500')->dumpTo('./foo.json'); + + $this->assertEquals('"' . PHPBU_TEST_BIN . '/' . $expected, $elastic->getCommand()); + } + /** * Tests Elasticdump::createCommandLine */