Skip to content

Commit

Permalink
Use Driver Name when creating DbImporter instead of Connection Name (#24
Browse files Browse the repository at this point in the history
)

* Add Additional Database Connections

* Update Test

* Add Connection with unsupported Driver

* Update Test and Exception

* Update Implementation

Fixes #22

* Use *-restore connections in RestoreCommandTest
  • Loading branch information
stefanzweifel authored Aug 22, 2023
1 parent 565578f commit a836fb3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/DbImporterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class DbImporterFactory
*/
public static function createFromConnection(string $dbConnectionName): DbImporter
{
if (config("database.connections.$dbConnectionName") === null) {
throw CannotCreateDbImporter::unsupportedDriver($dbConnectionName);
$config = config("database.connections.$dbConnectionName");

if ($config === null) {
throw CannotCreateDbImporter::configNotFound($dbConnectionName);
}

return static::forDriver($dbConnectionName);
return static::forDriver($config['driver']);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Exceptions/CannotCreateDbImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

class CannotCreateDbImporter extends Exception
{
public static function configNotFound(string $connectionName): self
{
return new static("Cannot find database connection `$connectionName` in config/database.php");
}

public static function unsupportedDriver(string $driver): self
{
return new static("Cannot create a importer for database driver `$driver`. Use `mysql`, `pgsql` or `sqlite`.");
Expand Down
12 changes: 6 additions & 6 deletions tests/Commands/RestoreCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'mysql',
'--connection' => 'mysql-restore',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"mysql\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"mysql-restore\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->expectsOutput('All health checks passed.')
->assertSuccessful();

Expand Down Expand Up @@ -46,11 +46,11 @@
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'sqlite',
'--connection' => 'sqlite-restore',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"sqlite\" database connection. (Database: database/database.sqlite)", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"sqlite-restore\" database connection. (Database: database/database.sqlite)", true)
->assertSuccessful();

$result = DB::connection('sqlite')->table('users')->count();
Expand Down Expand Up @@ -78,11 +78,11 @@
$this->artisan(RestoreCommand::class, [
'--disk' => 'remote',
'--backup' => $backup,
'--connection' => 'pgsql',
'--connection' => 'pgsql-restore',
'--password' => $password,
'--no-interaction' => true,
])
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"pgsql\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->expectsQuestion("Proceed to restore \"{$backup}\" using the \"pgsql-restore\" database connection. (Host: 127.0.0.1, Database: laravel_backup_restore, username: root)", true)
->assertSuccessful();

$result = DB::connection('pgsql')->table('users')->count();
Expand Down
26 changes: 21 additions & 5 deletions tests/DbImporterFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@
use Wnx\LaravelBackupRestore\DbImporterFactory;
use Wnx\LaravelBackupRestore\Exceptions\CannotCreateDbImporter;

it('returns db importer instances for given database driver', function ($driver, $expectedClass) {
expect(DbImporterFactory::createFromConnection($driver))->toBeInstanceOf($expectedClass);
it('returns db importer instances for given database driver', function ($connectionName, $expectedClass) {
expect(DbImporterFactory::createFromConnection($connectionName))->toBeInstanceOf($expectedClass);
})->with([
[
'driver' => 'mysql',
'connectionName' => 'mysql',
'expected' => \Wnx\LaravelBackupRestore\Databases\MySql::class,
],
[
'driver' => 'sqlite',
'connectionName' => 'mysql-restore',
'expected' => \Wnx\LaravelBackupRestore\Databases\MySql::class,
],
[
'connectionName' => 'sqlite',
'expected' => \Wnx\LaravelBackupRestore\Databases\Sqlite::class,
],
[
'connectionName' => 'sqlite-restore',
'expected' => \Wnx\LaravelBackupRestore\Databases\Sqlite::class,
],
[
'driver' => 'pgsql',
'connectionName' => 'pgsql',
'expected' => \Wnx\LaravelBackupRestore\Databases\PostgreSql::class,
],
[
'connectionName' => 'pgsql-restore',
'expected' => \Wnx\LaravelBackupRestore\Databases\PostgreSql::class,
],
]);

it('throws exception if no db importer instance can be created for connection')
->tap(fn () => DbImporterFactory::createFromConnection('unsupported'))
->throws(CannotCreateDbImporter::class);

it('throws exception if no db importer instance can be created for driver')
->tap(fn () => DbImporterFactory::createFromConnection('unsupported-driver'))
->throws(CannotCreateDbImporter::class);
3 changes: 2 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
declare(strict_types=1);

use Illuminate\Support\Facades\Storage;
use function Pest\Laravel\artisan;
use Wnx\LaravelBackupRestore\Tests\TestCase;

use function Pest\Laravel\artisan;

uses(TestCase::class)
->beforeEach(function () {
// Delete all files in the temp directory
Expand Down
26 changes: 26 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ protected function defineEnvironment($app)
'driver' => 'sqlite',
'database' => 'database/database.sqlite',
]);
$app['config']->set('database.connections.sqlite-restore', [
'driver' => 'sqlite',
'database' => 'database/database.sqlite',
]);

$app['config']->set('database.connections.mysql', [
'driver' => 'mysql',
Expand All @@ -34,6 +38,15 @@ protected function defineEnvironment($app)
'password' => env('MYSQL_PASSWORD', ''),
]);

$app['config']->set('database.connections.mysql-restore', [
'driver' => 'mysql',
'host' => env('MYSQL_HOST', '127.0.0.1'),
'port' => env('MYSQL_PORT', '3306'),
'database' => env('MYSQL_DATABASE', 'laravel_backup_restore'),
'username' => env('MYSQL_USERNAME', 'root'),
'password' => env('MYSQL_PASSWORD', ''),
]);

$app['config']->set('database.connections.pgsql', [
'driver' => 'pgsql',
'host' => env('PGSQL_HOST', '127.0.0.1'),
Expand All @@ -43,6 +56,19 @@ protected function defineEnvironment($app)
'password' => env('PGSQL_PASSWORD', ''),
'search_path' => 'public',
]);
$app['config']->set('database.connections.pgsql-restore', [
'driver' => 'pgsql',
'host' => env('PGSQL_HOST', '127.0.0.1'),
'port' => env('PGSQL_PORT', '5432'),
'database' => env('PGSQL_DATABASE', 'laravel_backup_restore'),
'username' => env('PGSQL_USERNAME', 'root'),
'password' => env('PGSQL_PASSWORD', ''),
'search_path' => 'public',
]);

$app['config']->set('database.connections.unsupported-driver', [
'driver' => 'sqlsrv',
]);

// Setup default filesystem disk where "remote" backups are stored
$app['config']->set('filesystems.disks.remote', [
Expand Down

0 comments on commit a836fb3

Please sign in to comment.