diff --git a/src/Ifsnop/Mysqldump/Mysqldump.php b/src/Ifsnop/Mysqldump/Mysqldump.php index 8d1e4e37..17e7efa5 100644 --- a/src/Ifsnop/Mysqldump/Mysqldump.php +++ b/src/Ifsnop/Mysqldump/Mysqldump.php @@ -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 @@ -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 @@ -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); diff --git a/unit-tests/MysqldumpTest.php b/unit-tests/MysqldumpTest.php index fac8991c..0c520e3a 100644 --- a/unit-tests/MysqldumpTest.php +++ b/unit-tests/MysqldumpTest.php @@ -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')); + } }