Skip to content

Commit

Permalink
refactor(src) update following #702 mysqldump work
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Mar 5, 2024
1 parent 28afecd commit a2760ea
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 21 deletions.
38 changes: 29 additions & 9 deletions src/WordPress/Database/MysqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace lucatume\WPBrowser\WordPress\Database;

use Druidfi\Mysqldump\Mysqldump;
use Ifsnop\Mysqldump\Mysqldump as LegacyMysqldump;
use Exception;
use Ifsnop\Mysqldump\Mysqldump;
use lucatume\WPBrowser\Utils\Db as DbUtil;
use lucatume\WPBrowser\Utils\Serializer;
use lucatume\WPBrowser\WordPress\DbException;
Expand Down Expand Up @@ -368,20 +369,39 @@ public function import(string $dumpFilePath): int
return $modifiedByQuery;
}

private function buildIfsnopMysqlDump(): LegacyMysqldump
{
return new class( $this->dsn, $this->dbUser, $this->dbPassword ) extends LegacyMysqldump {
public function start($filename = ''): void
{
// @phpstan-ignore-next-line property defined in ifsnop/mysqldump dependency.
$this->dumpSettings['add-drop-table'] = true;
$this->dumpSettings['add-drop-database'] = true;

parent::start($filename);
}
};
}

private function buildDruidfiMysqldump(): Mysqldump
{
$dumpSettings = [ 'add-drop-table' => true, 'add-drop-database' => true ];

return new Mysqldump($this->dsn, $this->dbUser, $this->dbPassword, $dumpSettings);
}

/**
* @throws DbException
*/
public function dump(string $dumpFile): void
{
try {
$dump = new class($this->dsn, $this->dbUser, $this->dbPassword) extends Mysqldump {
public function start($filename = '')
{
$this->dumpSettings['add-drop-table'] = true;
$this->dumpSettings['add-drop-database'] = true;
return parent::start($filename);
}
};
if (class_exists(LegacyMysqldump::class)) {
$dump = $this->buildIfsnopMysqlDump();
} else {
$dump = $this->buildDruidfiMysqldump();
}
/** @var Mysqldump $dump */
$dump->start($dumpFile);
} catch (\Exception $e) {
throw new DbException("Failed to dump database: " . $e->getMessage(), DbException::FAILED_DUMP);
Expand Down
Loading

0 comments on commit a2760ea

Please sign in to comment.