Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ abstract public function create(string $name): bool;
* Optionally check if collection exists in database
*
* @param string $database database name
* @param string $collection (optional) collection name
* @param string|null $collection (optional) collection name
*
* @return bool
*/
abstract public function exists(string $database, ?string $collection): bool;
abstract public function exists(string $database, ?string $collection = null): bool;

/**
* List Databases
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public function create(string $name): bool
{
$name = $this->filter($name);

if ($this->exists($name)) {
return true;
}

return $this->getPDO()
->prepare("CREATE DATABASE IF NOT EXISTS `{$name}` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;")
->execute();
Expand Down
34 changes: 10 additions & 24 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,27 @@ public function ping(): bool
* @return bool
* @throws Exception
*/
public function exists(string $database, ?string $collection): bool
public function exists(string $database, ?string $collection = null): bool
{
$database = $this->filter($database);

if (!\is_null($collection)) {
$collection = $this->filter($collection);

$select = 'TABLE_NAME';
$from = 'INFORMATION_SCHEMA.TABLES';
$where = 'TABLE_SCHEMA = :schema AND TABLE_NAME = :table';
$match = "{$this->getNamespace()}_{$collection}";
} else {
$select = 'SCHEMA_NAME';
$from = 'INFORMATION_SCHEMA.SCHEMATA';
$where = 'SCHEMA_NAME = :schema';
$match = $database;
}

$stmt = $this->getPDO()
->prepare("SELECT {$select}
FROM {$from}
WHERE {$where};");

$stmt->bindValue(':schema', $database, PDO::PARAM_STR);

if (!\is_null($collection)) {
$stmt = $this->getPDO()->prepare("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = :schema AND TABLE_NAME = :table");
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
$stmt->bindValue(':table', "{$this->getNamespace()}_{$collection}", PDO::PARAM_STR);
} else {
$stmt = $this->getPDO()->prepare("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = :schema");
$stmt->bindValue(':schema', $database, PDO::PARAM_STR);
}

$stmt->execute();

$document = $stmt->fetch();
if (empty($document)) {
return false;
}

return (($document[$select] ?? '') === $match) || // case insensitive check
(($document[strtolower($select)] ?? '') === $match);
return true;
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ class SQLite extends MariaDB
* Optionally check if collection exists in Database
*
* @param string $database
* @param string $collection
* @param string|null $collection
* @return bool
* @throws Exception
* @throws PDOException
* @throws DatabaseException
*/
public function exists(string $database, ?string $collection): bool
public function exists(string $database, ?string $collection = null): bool
{
$database = $this->filter($database);

Expand Down
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MariaDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public static function getDatabase(): Database
$database = new Database(new MariaDB($pdo), $cache);
$database->setDefaultDatabase('utopiaTests');
$database->setNamespace('myapp_'.uniqid());

if ($database->exists('utopiaTests')) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove this, since it is Done in Base.php Tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link it? I don't see a call to delete in Base.php

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->assertEquals(true, static::getDatabase()->delete($this->testDatabase));

I guess I can revert this to be more consistent for a fresh start, not to depend on this exist method 👍

$database->delete('utopiaTests');
}

$database->create();

return self::$database = $database;
Expand Down
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MongoDBTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ public static function getDatabase(): Database
$database = new Database(new Mongo($client), $cache);
$database->setDefaultDatabase($schema);
$database->setNamespace('myapp_' . uniqid());

if ($database->exists('utopiaTests')) {
$database->delete('utopiaTests');
}

$database->create();

return self::$database = $database;
Expand Down
5 changes: 0 additions & 5 deletions tests/Database/Adapter/MySQLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ public static function getDatabase(): Database
$database = new Database(new MySQL($pdo), $cache);
$database->setDefaultDatabase('utopiaTests');
$database->setNamespace('myapp_'.uniqid());

if ($database->exists('utopiaTests')) {
$database->delete('utopiaTests');
}

$database->create();

return self::$database = $database;
Expand Down
5 changes: 0 additions & 5 deletions tests/Database/Adapter/PostgresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ public static function getDatabase(): Database
$database = new Database(new Postgres($pdo), $cache);
$database->setDefaultDatabase('utopiaTests');
$database->setNamespace('myapp_'.uniqid());

if ($database->exists('utopiaTests')) {
$database->delete('utopiaTests');
}

$database->create();

return self::$database = $database;
Expand Down