Skip to content

Commit d9a9f56

Browse files
committed
1 parent 24e4c7c commit d9a9f56

2 files changed

Lines changed: 46 additions & 43 deletions

File tree

app/src/Bakery/MigrateResetHardCommand.php

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
7979
*/
8080
protected function performHardReset(bool $force): int
8181
{
82-
// Get doctrine schema Builder
82+
// Get schema Builder
8383
$connection = $this->db->getConnection();
84-
$schema = $connection->getDoctrineSchemaManager();
84+
$builder = $connection->getSchemaBuilder();
8585

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

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

107+
// Disable foreign key constraints
108+
$builder->disableForeignKeyConstraints();
109+
107110
// Drop all tables
108111
foreach ($tables as $table) {
109112
$this->io->writeln("Dropping table `$table`...");
110113

111114
// Perform drop
112-
$schema->dropTable($table);
115+
$builder->drop($table);
113116
}
114117

118+
// Re-enable foreign key constraints
119+
$builder->enableForeignKeyConstraints();
120+
115121
$this->io->success('Hard reset successful !');
116122

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

129-
// Get doctrine schema Builder
130-
// Doctrine schema is required to bypass sqlite not supported by normal schema
135+
// Get schema Builder
131136
$connection = $this->db->getConnection();
132-
$doctrineSchema = $connection->getDoctrineSchemaManager();
133-
$schema = $connection->getSchemaBuilder();
137+
$builder = $connection->getSchemaBuilder();
134138

135139
// Get a list of all tables
136-
$tables = $doctrineSchema->listTableNames();
140+
$tables = $builder->getTableListing();
137141

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

153157
// Perform drop
154-
$queries = $connection->pretend(function () use ($schema, $table) {
155-
$schema->drop($table);
158+
$queries = $connection->pretend(function () use ($builder, $table) {
159+
$builder->drop($table);
156160
});
157161

158162
// Display information

app/tests/Unit/Bakery/MigrateResetHardCommandTest.php

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

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

15-
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1615
use Illuminate\Database\Capsule\Manager as Capsule;
1716
use Illuminate\Database\Connection;
1817
use Illuminate\Database\DatabaseManager;
@@ -33,12 +32,12 @@ class MigrateResetHardCommandTest extends TestCase
3332

3433
public function testResetWithNoTables(): void
3534
{
36-
$schema = Mockery::mock(AbstractSchemaManager::class)
37-
->shouldReceive('listTableNames')->once()->andReturn([])
35+
$schema = Mockery::mock(Builder::class)
36+
->shouldReceive('getTableListing')->once()->andReturn([])
3837
->getMock();
3938

4039
$connection = Mockery::mock(Connection::class)
41-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
40+
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
4241
->getMock();
4342

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

5958
public function testReset(): void
6059
{
61-
$schema = Mockery::mock(AbstractSchemaManager::class)
62-
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
63-
->shouldReceive('dropTable')->with('foo')->once()
64-
->shouldReceive('dropTable')->with('bar')->once()
60+
$schema = Mockery::mock(Builder::class)
61+
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
62+
->shouldReceive('disableForeignKeyConstraints')->once()
63+
->shouldReceive('enableForeignKeyConstraints')->once()
64+
->shouldReceive('drop')->with('foo')->once()
65+
->shouldReceive('drop')->with('bar')->once()
6566
->getMock();
6667

6768
$connection = Mockery::mock(Connection::class)
68-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
69+
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
6970
->shouldReceive('getName')->once()->andReturn('foobar')
7071
->getMock();
7172

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

9192
public function testResetWithForce(): void
9293
{
93-
$schema = Mockery::mock(AbstractSchemaManager::class)
94-
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
95-
->shouldReceive('dropTable')->with('foo')->once()
96-
->shouldReceive('dropTable')->with('bar')->once()
94+
$schema = Mockery::mock(Builder::class)
95+
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
96+
->shouldReceive('disableForeignKeyConstraints')->once()
97+
->shouldReceive('enableForeignKeyConstraints')->once()
98+
->shouldReceive('drop')->with('foo')->once()
99+
->shouldReceive('drop')->with('bar')->once()
97100
->getMock();
98101

99102
$connection = Mockery::mock(Connection::class)
100-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
103+
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
101104
->shouldNotReceive('getName') // NOT
102105
->getMock();
103106

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

123126
public function testResetWithDeniedConfirmation(): void
124127
{
125-
$schema = Mockery::mock(AbstractSchemaManager::class)
126-
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
128+
$schema = Mockery::mock(Builder::class)
129+
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
127130
->getMock();
128131

129132
$connection = Mockery::mock(Connection::class)
130-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
133+
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
131134
->shouldReceive('getName')->once()->andReturn('foobar')
132135
->getMock();
133136

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

152155
public function testResetWithDatabase(): void
153156
{
154-
$schema = Mockery::mock(AbstractSchemaManager::class)
155-
->shouldReceive('listTableNames')->once()->andReturn(['foo', 'bar'])
156-
->shouldReceive('dropTable')->with('foo')->once()
157-
->shouldReceive('dropTable')->with('bar')->once()
157+
$schema = Mockery::mock(Builder::class)
158+
->shouldReceive('getTableListing')->once()->andReturn(['foo', 'bar'])
159+
->shouldReceive('disableForeignKeyConstraints')->once()
160+
->shouldReceive('enableForeignKeyConstraints')->once()
161+
->shouldReceive('drop')->with('foo')->once()
162+
->shouldReceive('drop')->with('bar')->once()
158163
->getMock();
159164

160165
$connection = Mockery::mock(Connection::class)
161-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($schema)
166+
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
162167
->shouldReceive('getName')->once()->andReturn('foobar')
163168
->getMock();
164169

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

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

199-
$schema = Mockery::mock(Builder::class);
200-
201204
$connection = Mockery::mock(Connection::class)
202-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($doctrineSchema)
203205
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
204206
->shouldReceive('pretend')->once()->andReturn($queries1)
205207
->shouldReceive('pretend')->once()->andReturn($queries2)
@@ -227,14 +229,11 @@ public function testPretendReset(): void
227229

228230
public function testPretendResetWithNoTables(): void
229231
{
230-
$doctrineSchema = Mockery::mock(AbstractSchemaManager::class)
231-
->shouldReceive('listTableNames')->once()->andReturn([])
232+
$schema = Mockery::mock(Builder::class)
233+
->shouldReceive('getTableListing')->once()->andReturn([])
232234
->getMock();
233235

234-
$schema = Mockery::mock(Builder::class);
235-
236236
$connection = Mockery::mock(Connection::class)
237-
->shouldReceive('getDoctrineSchemaManager')->once()->andReturn($doctrineSchema)
238237
->shouldReceive('getSchemaBuilder')->once()->andReturn($schema)
239238
->getMock();
240239

0 commit comments

Comments
 (0)