Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

table limit range support #16

Merged
merged 7 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
83 changes: 50 additions & 33 deletions src/Mysqldump.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function __construct(
?string $pass = null,
array $settings = [],
array $pdoOptions = []
) {
)
{
$this->dsn = $this->parseDsn($dsn);
$this->user = $user;
$this->pass = $pass;
Expand Down Expand Up @@ -249,20 +250,20 @@ private function getDumpFileHeader(): string
{
// Some info about software, source and time
$header = sprintf(
"-- mysqldump-php https://github.com/druidfi/mysqldump-php". PHP_EOL.
"--". PHP_EOL.
"-- Host: %s\tDatabase: %s". PHP_EOL.
"-- ------------------------------------------------------". PHP_EOL,
"-- mysqldump-php https://github.com/druidfi/mysqldump-php" . PHP_EOL .
"--" . PHP_EOL .
"-- Host: %s\tDatabase: %s" . PHP_EOL .
"-- ------------------------------------------------------" . PHP_EOL,
$this->host,
$this->dbName
);

if (!empty($version = $this->db->getVersion())) {
$header .= "-- Server version \t". $version . PHP_EOL;
$header .= "-- Server version \t" . $version . PHP_EOL;
}

if (!$this->settings->skipDumpDate()) {
$header .= "-- Date: ".date('r'). PHP_EOL . PHP_EOL;
$header .= "-- Date: " . date('r') . PHP_EOL . PHP_EOL;
}

return $header;
Expand All @@ -276,7 +277,7 @@ private function getDumpFileFooter(): string
$footer = '-- Dump completed';

if (!$this->settings->skipDumpDate()) {
$footer .= ' on: '.date('r');
$footer .= ' on: ' . date('r');
}

$footer .= PHP_EOL;
Expand Down Expand Up @@ -510,9 +511,9 @@ private function getTableStructure(string $tableName)

if (!$this->settings->skipComments()) {
$ret = sprintf(
"--".PHP_EOL.
"-- Table structure for table `%s`".PHP_EOL.
"--".PHP_EOL.PHP_EOL,
"--" . PHP_EOL .
"-- Table structure for table `%s`" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL,
$tableName
);
}
Expand Down Expand Up @@ -550,7 +551,7 @@ private function getTableColumnTypes(string $tableName): array
foreach ($columns as $col) {
$types = $this->db->parseColumnType($col);
$columnTypes[$col['Field']] = [
'is_numeric'=> $types['is_numeric'],
'is_numeric' => $types['is_numeric'],
'is_blob' => $types['is_blob'],
'type' => $types['type'],
'type_sql' => $col['Type'],
Expand Down Expand Up @@ -610,7 +611,7 @@ private function createStandInTable(string $viewName): string
$ret = implode(PHP_EOL . ',', $ret);

return sprintf(
"CREATE TABLE IF NOT EXISTS `%s` (".PHP_EOL."%s".PHP_EOL.");".PHP_EOL,
"CREATE TABLE IF NOT EXISTS `%s` (" . PHP_EOL . "%s" . PHP_EOL . ");" . PHP_EOL,
$viewName,
$ret
);
Expand All @@ -623,9 +624,9 @@ private function getViewStructureView(string $viewName)
{
if (!$this->settings->skipComments()) {
$ret = sprintf(
"--". PHP_EOL.
"-- View structure for view `%s`". PHP_EOL.
"--". PHP_EOL . PHP_EOL,
"--" . PHP_EOL .
"-- View structure for view `%s`" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL,
$viewName
);

Expand Down Expand Up @@ -672,9 +673,9 @@ private function getTriggerStructure(string $triggerName)
private function getProcedureStructure(string $procedureName)
{
if (!$this->settings->skipComments()) {
$ret = "--".PHP_EOL.
"-- Dumping routines for database '".$this->dbName."'".PHP_EOL.
"--".PHP_EOL.PHP_EOL;
$ret = "--" . PHP_EOL .
"-- Dumping routines for database '" . $this->dbName . "'" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL;
$this->write($ret);
}

Expand All @@ -695,9 +696,9 @@ private function getProcedureStructure(string $procedureName)
private function getFunctionStructure(string $functionName)
{
if (!$this->settings->skipComments()) {
$ret = "--".PHP_EOL.
"-- Dumping routines for database '".$this->dbName."'".PHP_EOL.
"--".PHP_EOL.PHP_EOL;
$ret = "--" . PHP_EOL .
"-- Dumping routines for database '" . $this->dbName . "'" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL;
$this->write($ret);
}

Expand All @@ -719,9 +720,9 @@ private function getFunctionStructure(string $functionName)
private function getEventStructure(string $eventName)
{
if (!$this->settings->skipComments()) {
$ret = "--".PHP_EOL.
"-- Dumping events for database '".$this->dbName."'".PHP_EOL.
"--".PHP_EOL.PHP_EOL;
$ret = "--" . PHP_EOL .
"-- Dumping events for database '" . $this->dbName . "'" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL;
$this->write($ret);
}

Expand Down Expand Up @@ -801,7 +802,7 @@ private function listValues(string $tableName)
$colNames = $this->getColumnNames($tableName);
}

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

// Table specific conditions override the default 'where'
$condition = $this->getTableWhere($tableName);
Expand All @@ -811,7 +812,9 @@ private function listValues(string $tableName)
}

if ($limit = $this->getTableLimit($tableName)) {
$stmt .= sprintf(' LIMIT %d', $limit);
$stmt .= is_numeric($limit) ?
sprintf(' LIMIT %d', $limit) :
sprintf(' LIMIT %s', $limit);
}

$resultSet = $this->conn->query($stmt);
Expand Down Expand Up @@ -873,9 +876,9 @@ private function prepareListValues(string $tableName)
{
if (!$this->settings->skipComments()) {
$this->write(
"--".PHP_EOL.
"-- Dumping data for table `$tableName`".PHP_EOL.
"--".PHP_EOL.PHP_EOL
"--" . PHP_EOL .
"-- Dumping data for table `$tableName`" . PHP_EOL .
"--" . PHP_EOL . PHP_EOL
);
}

Expand Down Expand Up @@ -936,8 +939,8 @@ private function endListValues(string $tableName, int $count = 0)

if (!$this->settings->skipComments()) {
$this->write(
"-- Dumped table `".$tableName."` with $count row(s)".PHP_EOL.
'--'.PHP_EOL.PHP_EOL
"-- Dumped table `" . $tableName . "` with $count row(s)" . PHP_EOL .
'--' . PHP_EOL . PHP_EOL
);
}
}
Expand Down Expand Up @@ -1037,7 +1040,21 @@ public function getTableLimit(string $tableName)
return false;
}

return is_numeric($this->tableLimits[$tableName]) ? $this->tableLimits[$tableName] : false;
$limit = false;
back-2-95 marked this conversation as resolved.
Show resolved Hide resolved
if (is_numeric($this->tableLimits[$tableName])) {
$limit = $this->tableLimits[$tableName];
}

if (is_array($this->tableLimits[$tableName]) &&
count($this->tableLimits[$tableName]) === 2 &&
is_numeric(implode('', $this->tableLimits[$tableName])) &&
$this->tableLimits[$tableName][0] <= $this->tableLimits[$tableName][1]
) {
$limit = implode(',', $this->tableLimits[$tableName]);
}

return $limit;

back-2-95 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
12 changes: 11 additions & 1 deletion tests/MysqldumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,23 @@ public function testTableSpecificLimitsWork()
$dump->setTableLimits([
'users' => 200,
'logs' => 500,
'table_with_invalid_limit' => '41923, 42992'
'table_with_invalid_limit' => '41923, 42992',
'table_with_range_limit' => [41923, 42992],
'table_with_invalid_range_limit' => [41923],
'table_with_invalid_range_limit2' => [41923, 42992, 42999],
'table_with_invalid_range_limit3' => [2, 1],
'table_with_invalid_range_limit4' => [1, 1],
]);

$this->assertEquals(200, $dump->getTableLimit('users'));
$this->assertEquals(500, $dump->getTableLimit('logs'));
$this->assertFalse($dump->getTableLimit('table_with_invalid_limit'));
$this->assertFalse($dump->getTableLimit('table_name_with_no_limit'));
$this->assertEquals('41923,42992', $dump->getTableLimit('table_with_range_limit'));
$this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit'));
$this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit2'));
$this->assertFalse($dump->getTableLimit('table_with_invalid_range_limit3'));
$this->assertEquals('1,1', $dump->getTableLimit('table_with_invalid_range_limit4'));
}

private function getPrivate(Mysqldump $dump, $var)
Expand Down