Skip to content

Commit

Permalink
Add a MariaDB driver to handle its mysqldump replacement (#5787)
Browse files Browse the repository at this point in the history
* Add a MariaDB driver to handle its mysqldump replacement

* Add driver file

* PHPCS

* Add return type hint
  • Loading branch information
weitzman committed Oct 19, 2023
1 parent 4ae35f3 commit b49cc6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Sql/SqlBase.php
Expand Up @@ -121,7 +121,7 @@ public static function getInstance($db_spec, $options): ?self
$driver = $db_spec['driver'];
$class_name = 'Drush\Sql\Sql' . ucfirst($driver);
if (class_exists($class_name)) {
$instance = new $class_name($db_spec, $options);
$instance = method_exists($class_name, 'make') ? $class_name::make($db_spec, $options) : new $class_name($db_spec, $options);
// Inject config
$instance->setConfig(Drush::config());
return $instance;
Expand Down
20 changes: 20 additions & 0 deletions src/Sql/SqlMariaDB.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Drush\Sql;

use Drush\Exec\ExecTrait;

class SqlMariaDB extends SqlMysql
{
use ExecTrait;

public function dumpProgram(): string
{
if (self::programExists('mariadb-dump')) {
return 'mariadb-dump';
}
return parent::dumpProgram();
}
}
40 changes: 39 additions & 1 deletion src/Sql/SqlMysql.php
Expand Up @@ -8,13 +8,51 @@

class SqlMysql extends SqlBase
{
protected string $version;

public string $queryExtra = '-A';

/**
* A factory method which creates a mysql or mariadb instance as needed.
*/
public static function make(array $dbSpec, array $options)
{
// First get a MySQL instance
$instance = new static($dbSpec, $options);
$sql = 'SELECT VERSION();"';
$instance->alwaysQuery($sql);
$out = trim($instance->getProcess()->getOutput());
if (str_contains($out, 'MariaDB')) {
// Replace with a MariaDB driver.
$instance = new SqlMariaDB($dbSpec, $options);
}
$instance->setVersion($out);
return $instance;
}

public function command(): string
{
return 'mysql';
}

public function dumpProgram(): string
{
return 'mysqldump';
}

/**
* The server version.
*/
public function getVersion(): string
{
return $this->version;
}

public function setVersion(string $version): void
{
$this->version = $version;
}

public function creds($hide_password = true): string
{
$dbSpec = $this->getDbSpec();
Expand Down Expand Up @@ -160,7 +198,7 @@ public function dumpCmd($table_selection): string
// The ordered-dump option is only supported by MySQL for now.
$ordered_dump = $this->getOption('ordered-dump');

$exec = 'mysqldump ';
$exec = $this->dumpProgram() . ' ';
// mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
$only_db_name = str_replace('--database=', ' ', $this->creds());
$exec .= $only_db_name;
Expand Down

0 comments on commit b49cc6b

Please sign in to comment.