Skip to content

Commit a14fe57

Browse files
Merge pull request #279 from bemanuel
Features/pgdump njobs
2 parents f244a0f + f4d316d commit a14fe57

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

doc/config/source/pgdump.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"options": {
44
"host": "localhost",
55
"port": "1234",
6+
"jobs": "4",
67
"user": "myUserName",
78
"password": "topSecret",
89
"database": "myDatabaseName",

doc/config/source/pgdump.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
<!-- optional, default none -->
77
<option name="port" value="1234" />
88

9+
<!-- optional, default none -->
10+
<option name="jobs" value="4" />
11+
912
<!-- optional, default none -->
1013
<option name="user" value="myUserName" />
1114

src/Backup/Source/Pgdump.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ class Pgdump extends SimulatorExecutable implements Simulator
5050
*/
5151
private $port;
5252

53+
/**
54+
* Run the dump in parallel by dumping njobs tables simultaneously.
55+
* Reduces the time of the dump but it also increases the load on the database server.
56+
* --jobs=<NJobs>
57+
*
58+
* @var int
59+
*/
60+
private $jobs;
61+
5362
/**
5463
* User to connect with
5564
* --user=<username>
@@ -236,6 +245,7 @@ private function setupDumpOptions(array $conf)
236245
$this->noOwner = Util\Str::toBoolean(Util\Arr::getValue($conf, 'noOwner', ''), false);
237246
$this->encoding = Util\Arr::getValue($conf, 'encoding', '');
238247
$this->format = Util\Arr::getValue($conf, 'format', self::DEFAULT_FORMAT);
248+
$this->jobs = Util\Arr::getValue($conf, 'jobs', 0);
239249
}
240250

241251

@@ -285,6 +295,7 @@ protected function createExecutable(Target $target) : Executable
285295
->dumpNoPrivileges($this->noPrivileges)
286296
->dumpNoOwner($this->noOwner)
287297
->dumpFormat($this->format)
298+
->dumpJobs($this->jobs)
288299
->dumpTo($target->getPathnamePlain());
289300
return $executable;
290301
}

src/Cli/Executable/Pgdump.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class Pgdump extends Abstraction implements Executable
3636
* @var int
3737
*/
3838
private $port;
39+
40+
/**
41+
* Run the dump in parallel by dumping njobs tables simultaneously.
42+
* --jobs=njobs
43+
* @var int
44+
*/
45+
private $jobs;
3946

4047
/**
4148
* Set SSL mode
@@ -180,7 +187,7 @@ class Pgdump extends Abstraction implements Executable
180187
* @var string
181188
*/
182189
private $file;
183-
190+
184191
/**
185192
* List of available output formats
186193
*
@@ -260,6 +267,21 @@ public function usePort(int $port) : Pgdump
260267
return $this;
261268
}
262269

270+
/**
271+
* Define njobs tables simultaneously..
272+
*
273+
* @param int $jobs
274+
* @return \phpbu\App\Cli\Executable\Pgdump
275+
*/
276+
public function dumpJobs(int $jobs): Pgdump
277+
{
278+
if ($jobs < 0) {
279+
throw new Exception('invalid jobs value');
280+
}
281+
$this->jobs = $jobs;
282+
return $this;
283+
}
284+
263285
/**
264286
* Set the sslmode
265287
*
@@ -501,6 +523,7 @@ protected function createCommandLine() : CommandLine
501523
$cmd->addOptionIfNotEmpty('--username', $this->user);
502524
$cmd->addOptionIfNotEmpty('--host', $this->host);
503525
$cmd->addOptionIfNotEmpty('--port', $this->port);
526+
$cmd->addOptionIfNotEmpty('--jobs', $this->jobs);
504527
$cmd->addOptionIfNotEmpty('--dbname', $this->databaseToDump);
505528
$cmd->addOptionIfNotEmpty('--schema-only', $this->schemaOnly, false);
506529
$cmd->addOptionIfNotEmpty('--data-only', $this->dataOnly, false);
@@ -509,6 +532,7 @@ protected function createCommandLine() : CommandLine
509532
$cmd->addOptionIfNotEmpty('--encoding', $this->encoding);
510533
$cmd->addOptionIfNotEmpty('--no-tablespaces', $this->noTablespaces, false);
511534
$cmd->addOptionIfNotEmpty('--no-acl', $this->noPrivileges, false);
535+
512536

513537
$this->handleSchemas($cmd);
514538
$this->handleTables($cmd);

tests/phpbu/Cli/Executable/PgdumpTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,33 @@ public function testEncoding()
251251
);
252252
}
253253

254+
/**
255+
* Tests Pgdump::getCommand
256+
*/
257+
public function testJobs()
258+
{
259+
$file = '/tmp/foo';
260+
$pgdump = new Pgdump(PHPBU_TEST_BIN);
261+
$pgdump->dumpDatabase('phpbu')->dumpJobs(4)->dumpTo($file);
262+
263+
$this->assertEquals(
264+
PHPBU_TEST_BIN . '/pg_dump -w --jobs=\'4\' --dbname=\'phpbu\' --file=\'/tmp/foo\'',
265+
$pgdump->getCommand()
266+
);
267+
}
268+
269+
/**
270+
* Tests Pgdump::getCommand
271+
*/
272+
public function testInvalidJobs()
273+
{
274+
$this->expectException('phpbu\App\Exception');
275+
$file = '/tmp/foo';
276+
$pgdump = new Pgdump(PHPBU_TEST_BIN);
277+
$pgdump->dumpDatabase('phpbu')->dumpJobs(-1)->dumpTo($file);
278+
}
279+
280+
254281
/**
255282
* Tests Pgdump::getCommand
256283
*/

0 commit comments

Comments
 (0)