Skip to content

Commit

Permalink
Add useColumnStatisticsOff for MySql (#133)
Browse files Browse the repository at this point in the history
* Add useColumnStatisticsOff for MySql

Add useColumnStatisticsOff() method in. order to enable --column-statistics=0 useful in a specific case. The scenario is: mysqldump 8 that queries by default column_statics table in information_schema database.
In some old version of MySql (service) like 5.7 , this table not exists.

Close #132

* renaming useColumnStatisticsOff into doNotUseColumnStatistics

* Update README for doNotUseColumnStatistics() method

* Add test for doNotUseColumnStatistics() use case.
  • Loading branch information
roberto-butti authored Sep 10, 2020
1 parent 7505ec3 commit 8425cbf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,22 @@ Spatie\DbDumper\Databases\MySql::create()
->dumpToFile('dump.sql');
```

### Don't use column_statics table with some old version of MySql service.

In order to use "_--column-statistics=0_" as option in mysqldump command you can use _doNotUseColumnStatistics()_ method.

If you have installed _mysqldump 8_, it queries by default _column_statics_ table in _information_schema_ database.
In some old version of MySql (service) like 5.7, this table it not exists. So you could have an exception during the execution of mysqldump. To avoid this, you could use _doNotUseColumnStatistics()_ method.

```php
Spatie\DbDumper\Databases\MySql::create()
->setDbName($databaseName)
->setUserName($userName)
->setPassword($password)
->doNotUseColumnStatistics()
->dumpToFile('dump.sql');
```

### Excluding tables from the dump

Using an array:
Expand Down
17 changes: 17 additions & 0 deletions src/Databases/MySql.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class MySql extends DbDumper
/** @var bool */
protected $skipLockTables = false;

/** @var bool */
protected $doNotUseColumnStatistics = false;

/** @var bool */
protected $useQuick = false;

Expand Down Expand Up @@ -113,6 +116,16 @@ public function skipLockTables()
return $this;
}

/**
* @return $this
*/
public function doNotUseColumnStatistics()
{
$this->doNotUseColumnStatistics = true;

return $this;
}

/**
* @return $this
*/
Expand Down Expand Up @@ -250,6 +263,10 @@ public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFil
$command[] = '--skip-lock-tables';
}

if ($this->doNotUseColumnStatistics) {
$command[] = '--column-statistics=0';
}

if ($this->useQuick) {
$command[] = '--quick';
}
Expand Down
15 changes: 15 additions & 0 deletions tests/MySqlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ public function it_can_generate_a_dump_command_with_compression_enabled()
$this->assertSame($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_columnstatistics()
{
$dumpCommand = MySql::create()
->setDbName('dbname')
->setUserName('username')
->setPassword('password')
->doNotUseColumnStatistics()
->getDumpCommand('dump.sql', 'credentials.txt');

$expected = '\'mysqldump\' --defaults-extra-file="credentials.txt" --skip-comments --extended-insert --column-statistics=0 dbname > "dump.sql"';

$this->assertSame($expected, $dumpCommand);
}

/** @test */
public function it_can_generate_a_dump_command_with_gzip_compressor_enabled()
{
Expand Down

0 comments on commit 8425cbf

Please sign in to comment.