Skip to content

Commit

Permalink
Inject Connection instead of Database Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Nov 10, 2023
1 parent f50cd73 commit 8ad2050
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 170 deletions.
8 changes: 3 additions & 5 deletions app/src/Bakery/Helper/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
namespace UserFrosting\Sprinkle\Core\Bakery\Helper;

use DI\Attribute\Inject;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use PDOException;

/**
Expand All @@ -26,7 +26,7 @@
trait DatabaseTest
{
#[Inject]
protected Capsule $capsule;
protected Connection $connection;

/**
* Test database connection directly using PDO.
Expand All @@ -37,9 +37,7 @@ trait DatabaseTest
protected function testDB(): bool
{
try {
$connectionName = $this->capsule->getDatabaseManager()->getDefaultConnection();
$connection = $this->capsule->getConnection($connectionName);
$connection->getPdo();
$this->connection->getPdo();
} catch (PDOException $e) {
$message = 'Could not connect to the database connection' . PHP_EOL;
$message .= 'Exception: ' . $e->getMessage() . PHP_EOL . PHP_EOL;
Expand Down
57 changes: 14 additions & 43 deletions app/src/Database/Migrator/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@

namespace UserFrosting\Sprinkle\Core\Database\Migrator;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use Illuminate\Database\Schema\Grammars\Grammar;
use UserFrosting\Sprinkle\Core\Exceptions\MigrationDependencyNotMetException;
use UserFrosting\Sprinkle\Core\Exceptions\MigrationRollbackException;

Expand All @@ -24,21 +22,14 @@
class Migrator
{
/**
* @var string|null The connection name (default: null)
*/
protected ?string $connection = null;

/**
* Constructor.
*
* @param MigrationRepositoryInterface $repository The migration repository
* @param MigrationLocatorInterface $locator The Migration locator
* @param Capsule $db The database
* @param MigrationRepositoryInterface $repository The migration repository
* @param MigrationLocatorInterface $locator The Migration locator
* @param Connection $dbConnection The database connection
*/
public function __construct(
protected MigrationRepositoryInterface $repository,
protected MigrationLocatorInterface $locator,
protected Capsule $db,
protected Connection $dbConnection,
) {
}

Expand Down Expand Up @@ -336,7 +327,7 @@ public function migrate(bool $step = false): array
};

// TODO : See if Transaction still needs to be used
if ($this->getSchemaGrammar()->supportsSchemaTransactions()) {
if ($this->transactionsSupported()) {
$this->getConnection()->transaction($callback);
} else {
$callback();
Expand Down Expand Up @@ -544,7 +535,7 @@ protected function runDownMigrations(array $migrations): void
};

// TODO : See if Transaction still needs to be used
if ($this->getSchemaGrammar()->supportsSchemaTransactions()) {
if ($this->transactionsSupported()) {
$this->getConnection()->transaction($callback);
} else {
$callback();
Expand Down Expand Up @@ -644,40 +635,20 @@ public function setLocator(MigrationLocatorInterface $locator): static
*
* @return Connection
*/
public function getConnection(): Connection
{
return $this->db->getConnection($this->getConnectionName());
}

/**
* Set database connection name.
*
* @param string|null $connection
*/
public function setConnectionName(?string $connection): static
{
$this->connection = $connection;

return $this;
}

/**
* Resolve the database connection instance.
*
* @return string|null The connection name (default: null, aka the default connection)
*/
public function getConnectionName(): ?string
protected function getConnection(): Connection
{
return $this->connection;
return $this->dbConnection;
}

/**
* Get instance of Schema Grammar.
* Return if database transaction are supported by current db connection.
*
* @return Grammar
* @return bool
*/
protected function getSchemaGrammar(): Grammar
protected function transactionsSupported(): bool
{
return $this->getConnection()->getSchemaGrammar();
return $this->getConnection()
->getSchemaGrammar()
->supportsSchemaTransactions();
}
}
6 changes: 3 additions & 3 deletions app/src/ServicesProvider/DatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class DatabaseService implements ServicesProviderInterface
public function register(): array
{
return [
Capsule::class => function (Config $config, LogExecutedQuery $logger) {
Capsule::class => function (Config $config, LogExecutedQuery $logger) {
$capsule = new Capsule();

// Add each defined connection in the config
Expand Down Expand Up @@ -63,11 +63,11 @@ public function register(): array
return $capsule;
},

Builder::class => function (Connection $connection) {
Builder::class => function (Connection $connection) {
return $connection->getSchemaBuilder();
},

Connection::class => function (Capsule $db) {
Connection::class => function (Capsule $db) {
$connection = $db->getDatabaseManager()->getDefaultConnection();

return $db->getConnection($connection);
Expand Down
8 changes: 4 additions & 4 deletions app/tests/Integration/Bakery/DebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace UserFrosting\Sprinkle\Core\Tests\Integration\Bakery;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PDOException;
Expand Down Expand Up @@ -43,10 +43,10 @@ public function testCommand(): void
public function testCommandForFailDatabase(): void
{
// Setup mocks
$class = Mockery::mock(Capsule::class)
->shouldReceive('getDatabaseManager')->andThrow(PDOException::class)
$class = Mockery::mock(Connection::class)
->shouldReceive('getPdo')->andThrow(PDOException::class)
->getMock();
$this->ci->set(Capsule::class, $class);
$this->ci->set(Connection::class, $class);

$result = $this->getCommandTester();

Expand Down
13 changes: 7 additions & 6 deletions app/tests/Integration/Database/Migrator/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace UserFrosting\Sprinkle\Core\Tests\Integration\Database\Migrator;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
use UserFrosting\Sprinkle\Core\Core;
use UserFrosting\Sprinkle\Core\Database\Migration;
use UserFrosting\Sprinkle\Core\Database\Migrator\MigrationLocatorInterface;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function testPretendToMigrate(): void
// Initial state, table doesn't exist.
// N.B.: Requires to get schema from connection, as otherwise it might
// not work (different :memory: instance)
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertFalse($schema->hasTable('test'));

// Pretend to migrate
Expand All @@ -67,7 +68,7 @@ public function testMigrate(): void
$migrator = $this->ci->get(Migrator::class);

// Initial state, table doesn't exist.
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertFalse($schema->hasTable('test'));

// Migrate
Expand All @@ -93,7 +94,7 @@ public function testPretendToRollback(): void

// Initial state, table exist.
$migrator->migrate();
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertTrue($schema->hasTable('test'));

// Pretend to rollback
Expand All @@ -116,7 +117,7 @@ public function testRollback(): void

// Initial state, table exist.
$migrator->migrate();
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertTrue($schema->hasTable('test'));

// Rollback
Expand Down Expand Up @@ -148,7 +149,7 @@ public function testReset(): void
// Install migration
$result = $migrator->migrate();
$this->assertSame([StubMigrationA::class], $result);
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertTrue($schema->hasTable('test'));

// Test pretend to reset
Expand All @@ -160,7 +161,7 @@ public function testReset(): void
// Test reset
$result = $migrator->reset();
$this->assertSame([StubMigrationA::class], $result);
$schema = $migrator->getConnection()->getSchemaBuilder();
$schema = $this->ci->get(Builder::class);
$this->assertFalse($schema->hasTable('test'));
}

Expand Down
19 changes: 3 additions & 16 deletions app/tests/Unit/Database/Migrator/MigratorDependencyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace UserFrosting\Sprinkle\Core\Tests\Unit\Database\Migrator;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
Expand Down Expand Up @@ -59,11 +58,7 @@ public function testConstruct(): Migrator
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

$this->assertInstanceOf(Migrator::class, $analyser);

Expand Down Expand Up @@ -161,11 +156,7 @@ public function testGetPendingThirdStageDependency(): void
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

$this->assertSame([
StubAnalyserMigrationC::class, // C is before B because B depend on C
Expand Down Expand Up @@ -193,11 +184,7 @@ public function testGetPendingWithNonAvailable(): void
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

$this->expectException(MigrationDependencyNotMetException::class);
$this->expectExceptionMessage(StubAnalyserMigrationG::class . ' depends on ' . StubAnalyserMigrationF::class . ", but it's not available.");
Expand Down
44 changes: 8 additions & 36 deletions app/tests/Unit/Database/Migrator/MigratorRollbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

namespace UserFrosting\Sprinkle\Core\Tests\Unit\Database\Migrator;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
Expand Down Expand Up @@ -50,11 +49,7 @@ public function testGetMigrationsForRollback(): void
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

$result = $analyser->getMigrationsForRollback(1);
$this->assertSame([StubAnalyserRollbackMigrationD::class], $result);
Expand All @@ -79,11 +74,7 @@ public function testGetMigrationsForReset(): void
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

$result = $analyser->getMigrationsForReset();
$this->assertSame([StubAnalyserRollbackMigrationD::class], $result);
Expand Down Expand Up @@ -117,11 +108,7 @@ public function testValidateRollbackMigration(): void
->getMock();

$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();

$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Run command
$this->assertNull($analyser->validateRollbackMigration(StubAnalyserRollbackMigrationD::class));
Expand All @@ -140,10 +127,7 @@ public function testValidateRollbackMigrationForNotInstalledException(): void
->getMock();
$available = Mockery::mock(MigrationLocatorInterface::class);
$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();
$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Start by testing false
$this->assertFalse($analyser->canRollbackMigration(StubAnalyserRollbackMigrationD::class));
Expand Down Expand Up @@ -179,10 +163,7 @@ public function testValidateRollbackMigrationForStaleException(): void
])
->getMock();
$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();
$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Start by testing false
$this->assertFalse($analyser->canRollbackMigration(StubAnalyserRollbackMigrationD::class));
Expand Down Expand Up @@ -220,10 +201,7 @@ public function testValidateRollbackMigrationForDependenciesNotMet(): void
->shouldReceive('has')->with(StubAnalyserRollbackMigrationC::class)->twice()->andReturn(true)
->getMock();
$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();
$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Run command
$this->assertFalse($analyser->canRollbackMigration(StubAnalyserRollbackMigrationC::class));
Expand Down Expand Up @@ -261,10 +239,7 @@ public function testValidateRollbackMigrationForDependenciesDoesntExist(): void
->shouldReceive('has')->with(StubAnalyserRollbackMigrationC::class)->twice()->andReturn(false)
->getMock();
$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();
$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Run command
$this->assertFalse($analyser->canRollbackMigration(StubAnalyserRollbackMigrationA::class));
Expand Down Expand Up @@ -299,10 +274,7 @@ public function testValidateRollbackMigrationForDependenciesDoesntExistWithDirec
->shouldReceive('has')->with(StubAnalyserRollbackMigrationC::class)->twice()->andReturn(false)
->getMock();
$connection = Mockery::mock(Connection::class);
$database = Mockery::mock(Capsule::class)
->shouldReceive('getConnection')->with(null)->andReturn($connection)
->getMock();
$analyser = new Migrator($installed, $available, $database);
$analyser = new Migrator($installed, $available, $connection);

// Run command
$this->assertFalse($analyser->canRollbackMigration(StubAnalyserRollbackMigrationB::class));
Expand Down
Loading

0 comments on commit 8ad2050

Please sign in to comment.