Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jan 12, 2025
1 parent 24e4c7c commit d9a9f56
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
26 changes: 15 additions & 11 deletions app/src/Bakery/MigrateResetHardCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
protected function performHardReset(bool $force): int
{
// Get doctrine schema Builder
// Get schema Builder
$connection = $this->db->getConnection();
$schema = $connection->getDoctrineSchemaManager();
$builder = $connection->getSchemaBuilder();

// Get a list of all tables
$tables = $schema->listTableNames();
$tables = $builder->getTableListing();

// Stop if nothing to drop
if (count($tables) === 0) {
Expand All @@ -104,14 +104,20 @@ protected function performHardReset(bool $force): int
}
}

// Disable foreign key constraints
$builder->disableForeignKeyConstraints();

// Drop all tables
foreach ($tables as $table) {
$this->io->writeln("Dropping table `$table`...");

// Perform drop
$schema->dropTable($table);
$builder->drop($table);
}

// Re-enable foreign key constraints
$builder->enableForeignKeyConstraints();

$this->io->success('Hard reset successful !');

return self::SUCCESS;
Expand All @@ -126,14 +132,12 @@ protected function pretendHardReset(): int
{
$this->io->note("Running {$this->getName()} in pretend mode");

// Get doctrine schema Builder
// Doctrine schema is required to bypass sqlite not supported by normal schema
// Get schema Builder
$connection = $this->db->getConnection();
$doctrineSchema = $connection->getDoctrineSchemaManager();
$schema = $connection->getSchemaBuilder();
$builder = $connection->getSchemaBuilder();

// Get a list of all tables
$tables = $doctrineSchema->listTableNames();
$tables = $builder->getTableListing();

// Stop if nothing to drop
if (count($tables) === 0) {
Expand All @@ -151,8 +155,8 @@ protected function pretendHardReset(): int
$this->io->section("Dropping table `$table`...");

// Perform drop
$queries = $connection->pretend(function () use ($schema, $table) {
$schema->drop($table);
$queries = $connection->pretend(function () use ($builder, $table) {
$builder->drop($table);
});

// Display information
Expand Down
63 changes: 31 additions & 32 deletions app/tests/Unit/Bakery/MigrateResetHardCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

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

use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
Expand All @@ -33,12 +32,12 @@ class MigrateResetHardCommandTest extends TestCase

public function testResetWithNoTables(): void
{
$schema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn([])
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn([])
->getMock();

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->getMock();

$db = Mockery::mock(Capsule::class)
Expand All @@ -58,14 +57,16 @@ public function testResetWithNoTables(): void

public function testReset(): void
{
$schema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
->shouldReceive('dropTable')->with('foo')->once()
->shouldReceive('dropTable')->with('bar')->once()
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
->shouldReceive('disableForeignKeyConstraints')->once()
->shouldReceive('enableForeignKeyConstraints')->once()
->shouldReceive('drop')->with('foo')->once()
->shouldReceive('drop')->with('bar')->once()
->getMock();

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->shouldReceive('getName')->once()->andReturn('foobar')
->getMock();

Expand All @@ -90,14 +91,16 @@ public function testReset(): void

public function testResetWithForce(): void
{
$schema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
->shouldReceive('dropTable')->with('foo')->once()
->shouldReceive('dropTable')->with('bar')->once()
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
->shouldReceive('disableForeignKeyConstraints')->once()
->shouldReceive('enableForeignKeyConstraints')->once()
->shouldReceive('drop')->with('foo')->once()
->shouldReceive('drop')->with('bar')->once()
->getMock();

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->shouldNotReceive('getName') // NOT
->getMock();

Expand All @@ -122,12 +125,12 @@ public function testResetWithForce(): void

public function testResetWithDeniedConfirmation(): void
{
$schema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
->getMock();

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->shouldReceive('getName')->once()->andReturn('foobar')
->getMock();

Expand All @@ -151,14 +154,16 @@ public function testResetWithDeniedConfirmation(): void

public function testResetWithDatabase(): void
{
$schema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
->shouldReceive('dropTable')->with('foo')->once()
->shouldReceive('dropTable')->with('bar')->once()
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
->shouldReceive('disableForeignKeyConstraints')->once()
->shouldReceive('enableForeignKeyConstraints')->once()
->shouldReceive('drop')->with('foo')->once()
->shouldReceive('drop')->with('bar')->once()
->getMock();

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->shouldReceive('getName')->once()->andReturn('foobar')
->getMock();

Expand Down Expand Up @@ -192,14 +197,11 @@ public function testPretendReset(): void
$queries1 = [['query' => 'drop table "forbar"']];
$queries2 = [['query' => 'drop table "barfoo"']];

$doctrineSchema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn(['foo/bar', 'bar/foo'])
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn(['foo/bar', 'bar/foo'])
->getMock();

$schema = Mockery::mock(Builder::class);

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($doctrineSchema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->shouldReceive('pretend')->once()->andReturn($queries1)
->shouldReceive('pretend')->once()->andReturn($queries2)
Expand Down Expand Up @@ -227,14 +229,11 @@ public function testPretendReset(): void

public function testPretendResetWithNoTables(): void
{
$doctrineSchema = Mockery::mock(AbstractSchemaManager::class)
->shouldReceive('listTableNames')->once()->andReturn([])
$schema = Mockery::mock(Builder::class)
->shouldReceive('getTableListing')->once()->andReturn([])
->getMock();

$schema = Mockery::mock(Builder::class);

$connection = Mockery::mock(Connection::class)
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($doctrineSchema)
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
->getMock();

Expand Down

0 comments on commit d9a9f56

Please sign in to comment.