Skip to content

Commit

Permalink
Add database interface
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Jan 7, 2025
1 parent b5f3d0f commit 39d6da5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
21 changes: 5 additions & 16 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,8 @@
use Smile\GdprDump\Database\Metadata\MysqlMetadata;
use UnexpectedValueException;

/**
* Wrapper that stores the following objects:
* - connection: the Doctrine connection.
* - driver: allows to retrieve the DSN that was used to connect to the database.
* - metadata: allows to fetch the database metadata (table names, foreign key constraints).
*
* We use a custom abstraction layer for database metadata, because the Doctrine schema manager
* crashes when used with databases that use custom Doctrine types (e.g. OroCommerce).
*/
final class Database
final class Database implements DatabaseInterface
{
public const DRIVER_MYSQL = 'pdo_mysql';

private Connection $connection;
private DriverInterface $driver;
private MetadataInterface $metadata;
Expand Down Expand Up @@ -53,31 +42,31 @@ public function __construct(array $connectionParams)
}

/**
* Get the doctrine connection.
* @inheritdoc
*/
public function getConnection(): Connection
{
return $this->connection;
}

/**
* Get the database driver.
* @inheritdoc
*/
public function getDriver(): DriverInterface
{
return $this->driver;
}

/**
* Get the database metadata.
* @inheritdoc
*/
public function getMetadata(): MetadataInterface
{
return $this->metadata;
}

/**
* Get the connection parameters (host, port, user...).
* @inheritdoc
*/
public function getConnectionParams(): ParameterBag
{
Expand Down
43 changes: 43 additions & 0 deletions src/Database/DatabaseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Smile\GdprDump\Database;

use Doctrine\DBAL\Connection;
use Smile\GdprDump\Database\Driver\DriverInterface;
use Smile\GdprDump\Database\Metadata\MetadataInterface;

/**
* Wrapper that stores the following objects:
* - connection: the Doctrine connection.
* - driver: allows to retrieve the DSN that was used to connect to the database.
* - metadata: allows to fetch the database metadata (table names, foreign key constraints).
*
* We use a custom abstraction layer for database metadata, because the Doctrine schema manager
* crashes when used with databases that use custom Doctrine types (e.g. OroCommerce).
*/
interface DatabaseInterface
{
public const DRIVER_MYSQL = 'pdo_mysql';

/**
* Get the doctrine connection.
*/
public function getConnection(): Connection;

/**
* Get the database driver.
*/
public function getDriver(): DriverInterface;

/**
* Get the database metadata.
*/
public function getMetadata(): MetadataInterface;

/**
* Get the connection parameters (host, port, user...).
*/
public function getConnectionParams(): ParameterBag;
}
4 changes: 2 additions & 2 deletions src/Database/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ final class ParameterBag
{
private array $params;
private array $defaults = [
Database::DRIVER_MYSQL => ['host' => 'localhost', 'user' => 'root'],
DatabaseInterface::DRIVER_MYSQL => ['host' => 'localhost', 'user' => 'root'],
];

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ private function prepareParams(array $params): array

// Set the driver
if (!isset($params['driver'])) {
$params['driver'] = Database::DRIVER_MYSQL;
$params['driver'] = DatabaseInterface::DRIVER_MYSQL;
}

if (isset($this->defaults[$params['driver']])) {
Expand Down
6 changes: 3 additions & 3 deletions src/Dumper/Event/DumpEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Smile\GdprDump\Dumper\Event;

use Druidfi\Mysqldump\Mysqldump;
use Smile\GdprDump\Database\Database;
use Smile\GdprDump\Database\DatabaseInterface;
use Smile\GdprDump\Dumper\Config\DumperConfigInterface;
use Symfony\Contracts\EventDispatcher\Event;

Expand All @@ -16,7 +16,7 @@ final class DumpEvent extends Event
{
public function __construct(
private Mysqldump $dumper,
private Database $database,
private DatabaseInterface $database,
private DumperConfigInterface $config,
private array $context
) {
Expand All @@ -33,7 +33,7 @@ public function getConfig(): DumperConfigInterface
/**
* Get the database wrapper.
*/
public function getDatabase(): Database
public function getDatabase(): DatabaseInterface
{
return $this->database;
}
Expand Down

0 comments on commit 39d6da5

Please sign in to comment.