Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/Ifsnop/Mysqldump/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ class Mysqldump
private $tableWheres = array();
private $tableLimits = array();

/**
* Keyed on table name, with the value as the array of partition names.
* e.g. - 'users' => [ 'p20220101', 'p20220102' ]
*
* @var array
*/
private $tableParitions = array();


/**
* Constructor of Mysqldump. Note that in the case of an SQLite database
Expand Down Expand Up @@ -246,6 +254,31 @@ public function getTableWhere($tableName)
return false;
}

/**
* Keyed on table name, with the value as the array of partition names:
* e.g. - 'users' => [ 'p20220101', 'p20220102' ]
*
* @param array $tableWheres
*/
public function setTablePartitions(array $tableWheres)
{
$this->tableParitions = $tableWheres;
}

/**
* @param $tableName
*
* @return false|mixed
*/
public function getTablePartitions($tableName)
{
if (!empty($this->tableParitions[$tableName])) {
return $this->tableParitions[$tableName];
}

return false;
}

/**
* Keyed by table name, with the value as the numeric limit:
* e.g. 'users' => 3000
Expand Down Expand Up @@ -1101,6 +1134,13 @@ private function listValues($tableName)

$stmt = "SELECT ".implode(",", $colStmt)." FROM `$tableName`";

// Table specific partitions
$partitions = $this->getTablePartitions($tableName);

if ($partitions) {
$stmt .= " PARTITION (".implode(",", $partitions).")";
}

// Table specific conditions override the default 'where'
$condition = $this->getTableWhere($tableName);

Expand Down
14 changes: 14 additions & 0 deletions unit-tests/MysqldumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,18 @@ public function tableSpecificLimitsWork()
$this->assertFalse($dump->getTableLimit('table_with_invalid_limit'));
$this->assertFalse($dump->getTableLimit('table_name_with_no_limit'));
}

/** @test */
public function tableSpecificPartitionsConditionsWork()
{
$dump = new Mysqldump('mysql:host=localhost;dbname=test', 'testing', 'testing');

$dump->setTablePartitions(array(
'users' => array('p200220101', 'p20220102'),
'logs' => array('p200220101')
));

$this->assertEquals(array('p200220101', 'p20220102'), $dump->getTablePartitions('users'));
$this->assertEquals(array('p200220101'), $dump->getTablePartitions('logs'));
}
}