Skip to content

Commit bd43055

Browse files
Drop tables one by one (#8)
* Drop tables one by one * Emulate prepares and add test for dropping all tables Co-authored-by: Aaron Francis <Aarondfrancis@gmail.com>
1 parent 9f9b2cf commit bd43055

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/Schema/Builder.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,34 @@ protected function createBlueprint($table, Closure $callback = null)
2727

2828
return parent::createBlueprint($table, $callback);
2929
}
30-
}
30+
31+
/**
32+
* Drop all tables from the database.
33+
*
34+
* @return void
35+
*/
36+
public function dropAllTables()
37+
{
38+
$tables = [];
39+
40+
foreach ($this->getAllTables() as $row) {
41+
$row = (array) $row;
42+
43+
$tables[] = reset($row);
44+
}
45+
46+
if (empty($tables)) {
47+
return;
48+
}
49+
50+
$this->disableForeignKeyConstraints();
51+
52+
foreach ($tables as $table) {
53+
$this->connection->statement(
54+
$this->grammar->compileDropAllTables([$table])
55+
);
56+
}
57+
58+
$this->enableForeignKeyConstraints();
59+
}
60+
}

tests/BaseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ public function getEnvironmentSetUp($app)
3939
// configuration possible, making for the ideal developer experience.
4040
$app['config']->set('database.default', 'mysql');
4141
$app['config']->set('database.connections.mysql.driver', 'singlestore');
42+
$app['config']->set('database.connections.mysql.options.' . PDO::ATTR_EMULATE_PREPARES, true);
4243
}
4344
}

tests/Hybrid/DropAllTablesTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* @author Aaron Francis <aarondfrancis@gmail.com|https://twitter.com/aarondfrancis>
4+
*/
5+
6+
namespace SingleStore\Laravel\Tests\Hybrid;
7+
8+
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Schema;
11+
use SingleStore\Laravel\Schema\Blueprint;
12+
use SingleStore\Laravel\Tests\BaseTest;
13+
14+
class DropAllTablesTest extends BaseTest
15+
{
16+
use HybridTestHelpers;
17+
18+
/** @test */
19+
public function it_drops_all_tables_sequentially()
20+
{
21+
if (!$this->runHybridIntegrations()) {
22+
return;
23+
}
24+
25+
$this->mockDatabaseConnection = false;
26+
27+
Schema::dropIfExists('test_drop_1');
28+
Schema::dropIfExists('test_drop_2');
29+
30+
$this->assertFalse(Schema::hasTable('test_drop_1'));
31+
$this->assertFalse(Schema::hasTable('test_drop_2'));
32+
33+
Schema::create('test_drop_1', function(Blueprint $table) {
34+
$table->id();
35+
});
36+
37+
Schema::create('test_drop_2', function (Blueprint $table) {
38+
$table->id();
39+
});
40+
41+
$this->assertTrue(Schema::hasTable('test_drop_1'));
42+
$this->assertTrue(Schema::hasTable('test_drop_2'));
43+
44+
$this->getConnection()->getSchemaBuilder()->dropAllTables();
45+
}
46+
47+
}

0 commit comments

Comments
 (0)