Skip to content

Commit

Permalink
replace dbal
Browse files Browse the repository at this point in the history
  • Loading branch information
inmanturbo committed Feb 3, 2024
1 parent fd52ac9 commit ad144ea
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 64 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ phpstan.neon
testbench.yaml
vendor
node_modules
.phpunit.cache
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
],
"require": {
"php": "^8.1",
"doctrine/dbal": "^3.7.2",
"illuminate/contracts": "^10.0",
"illuminate/support": "^10.0",
"spatie/laravel-package-tools": "^1.16.1"
Expand Down
55 changes: 21 additions & 34 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
backupGlobals="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false"
>
<testsuites>
<testsuite name="Envor Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="Envor Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
4 changes: 2 additions & 2 deletions src/Contracts/DatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Envor\DatabaseManager\Contracts;

use Carbon\Carbon;
use Illuminate\Support\Carbon;
use Stringable;

interface DatabaseManager extends Stringable
Expand All @@ -15,7 +15,7 @@ public function createDatabase(string $databaseName): bool;
/**
* Delete a database.
*/
public function deleteDatabase(string $databaseName, null|Carbon $deletedAt = null): bool;
public function deleteDatabase(string $databaseName, ?Carbon $deletedAt = null): bool;

/**
* Does a database exist.
Expand Down
2 changes: 1 addition & 1 deletion src/FakeDatabaseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function createDatabase($databaseName): bool
return true;
}

public function deleteDatabase(string $databaseName, null|Carbon $deletedAt = null): bool
public function deleteDatabase(string $databaseName, ?Carbon $deletedAt = null): bool
{
return true;
}
Expand Down
31 changes: 20 additions & 11 deletions src/MySQLDatabaseManager.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php

declare(strict_types=1);

namespace Envor\DatabaseManager;

use Carbon\Carbon;
use Envor\DatabaseManager\Contracts\DatabaseManager;
use Envor\DatabaseManager\Exceptions\NoConnectionSetException;
use Illuminate\Database\Connection;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Stringable;

class MySQLDatabaseManager implements DatabaseManager
{
Expand All @@ -34,19 +33,23 @@ public function setConnection(string $connection): self

public function listTableNames(): array
{
return $this->database()->getDoctrineSchemaManager()->listTableNames();
return $this->database()->getSchemaBuilder()->getTableListing();
}

public function createDatabase($databaseName): bool
public function createDatabase(string|Stringable $databaseName): bool
{
$databaseName = (string) $databaseName;

$charset = $this->database()->getConfig('charset');
$collation = $this->database()->getConfig('collation');

return $this->database()->statement("CREATE DATABASE IF NOT EXISTS `{$databaseName}` CHARACTER SET `$charset` COLLATE `$collation`");
}

public function deleteDatabase($databaseName, $deletedAt = null): bool
public function deleteDatabase(string|Stringable $databaseName, ?Carbon $deletedAt = null): bool
{
$databaseName = (string) $databaseName;

try {
$deletedAt = $deletedAt ?? now();
$deletedDatabaseName = 'deleted_'.$deletedAt->format('Y_m_d_H_i_s_').$databaseName;
Expand Down Expand Up @@ -82,8 +85,10 @@ public function deleteDatabase($databaseName, $deletedAt = null): bool
}
}

public function eraseDatabase($databaseName): bool
public function eraseDatabase(string|Stringable $databaseName): bool
{
$databaseName = (string) $databaseName;

try {
$this->database()->statement("DROP DATABASE IF EXISTS `{$databaseName}`");

Expand All @@ -103,8 +108,10 @@ public function cleanupOldDatabases(int $daysOld = 1): int
return $count;
}

protected function deleteIfOld(string $databaseName, int $daysOld = 1): int
protected function deleteIfOld(string|Stringable $databaseName, int $daysOld = 1): int
{
$databaseName = (string) $databaseName;

if ($this->databaseIsOld($databaseName, $daysOld)) {
$this->eraseDatabase($databaseName);

Expand All @@ -129,8 +136,10 @@ protected function databaseIsOld(string $deletedDatabaseName, int $daysOld = 1):
return $date < now()->subDays($daysOld)->getTimestamp();
}

public function databaseExists(string $databaseName): bool
public function databaseExists(string|Stringable $databaseName): bool
{
$databaseName = (string) $databaseName;

return (bool) $this->database()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$databaseName}'");
}

Expand All @@ -141,9 +150,9 @@ public function makeConnectionConfig(array $baseConfig, string $databaseName): a
return $baseConfig;
}

public function getDatabaseName(string $databaseName): string
public function getDatabaseName(string|Stringable $databaseName): string
{
return $databaseName;
return (string) $databaseName;
}

public function __toString()
Expand Down
37 changes: 22 additions & 15 deletions src/SQLiteDatabaseManager.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
<?php

declare(strict_types=1);

namespace Envor\DatabaseManager;

use Envor\DatabaseManager\Contracts\DatabaseManager;
use Envor\DatabaseManager\Exceptions\NoConnectionSetException;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Connection;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Stringable;

class SQLiteDatabaseManager implements DatabaseManager
{
Expand All @@ -24,31 +25,37 @@ protected function database(): Connection
return DB::connection($this->connection);
}

public function createDatabase($databaseName): bool
public function createDatabase(string|Stringable $databaseName): bool
{
$databaseName = (string) $databaseName;

try {
return $this->databaseResolver()->put($databaseName.'.sqlite', '');
return $this->storageDisk()->put($databaseName.'.sqlite', '');
} catch (\Throwable $th) {
return false;
}
}

public function deleteDatabase($databaseName, $deletedAt = null): bool
public function deleteDatabase(string|Stringable $databaseName, ?Carbon $deletedAt = null): bool
{
$databaseName = (string) $databaseName;

$deletedAt = $deletedAt ?? now();
$deletedDatabaseFilePath = '.trash/'.$deletedAt->format('Y/m/d/H_i_s_').$databaseName.'.sqlite';

$file = $this->databaseResolver()->move($databaseName.'.sqlite', $deletedDatabaseFilePath);
$file = $this->storageDisk()->move($databaseName.'.sqlite', $deletedDatabaseFilePath);

touch($this->databaseResolver()->path($deletedDatabaseFilePath), $deletedAt->getTimestamp());
touch($this->storageDisk()->path($deletedDatabaseFilePath), $deletedAt->getTimestamp());

return $file;
}

public function eraseDatabase($databaseName): bool
public function eraseDatabase(string|Stringable $databaseName): bool
{
$databaseName = (string) $databaseName;

try {
return $this->databaseResolver()->delete($databaseName.'.sqlite');
return $this->storageDisk()->delete($databaseName.'.sqlite');
} catch (\Throwable $th) {
return false;
}
Expand All @@ -58,10 +65,10 @@ public function cleanupOldDatabases(int $daysOld = 1): int
{
$count = 0;
// keep only files new than $daysOld days
collect($this->databaseResolver()->listContents('.trash', true))
collect($this->storageDisk()->listContents('.trash', true))
->each(function ($file) use ($daysOld, &$count) {
if ($file['type'] == 'file' && $file['lastModified'] < now()->subDays($daysOld)->getTimestamp()) {
$this->databaseResolver()->delete($file['path']);
$this->storageDisk()->delete($file['path']);
$count++;
}
});
Expand All @@ -71,12 +78,12 @@ public function cleanupOldDatabases(int $daysOld = 1): int

public function listTableNames(): array
{
return $this->database()->getDoctrineSchemaManager()->listTableNames();
return $this->database()->getSchemaBuilder()->getTableListing();
}

public function databaseExists(string $databaseName): bool
{
return $this->databaseResolver()->exists($databaseName.'.sqlite');
return $this->storageDisk()->exists($databaseName.'.sqlite');
}

public function makeConnectionConfig(array $baseConfig, string $databaseName): array
Expand All @@ -95,10 +102,10 @@ public function setConnection(string $connection): self

public function getDatabaseName(string $databaseName): string
{
return $this->databaseResolver()->path($databaseName.'.sqlite');
return $this->storageDisk()->path($databaseName.'.sqlite');
}

protected function databaseResolver()
protected function storageDisk(): Filesystem
{
return Storage::disk(config('database_manager.sqlite_disk'));
}
Expand Down

0 comments on commit ad144ea

Please sign in to comment.