Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add dropAllTables() to DB Schema Builder #18484

Merged
merged 8 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Illuminate/Database/SQLiteConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Database;

use Illuminate\Database\Schema\SQLiteBuilder;
use Illuminate\Database\Query\Processors\SQLiteProcessor;
use Doctrine\DBAL\Driver\PDOSqlite\Driver as DoctrineDriver;
use Illuminate\Database\Query\Grammars\SQLiteGrammar as QueryGrammar;
Expand All @@ -19,6 +20,20 @@ protected function getDefaultQueryGrammar()
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\SQLiteBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

return new SQLiteBuilder($this);
}

/**
* Get the default schema grammar instance.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Database\Schema;

use Closure;
use LogicException;
use Illuminate\Database\Connection;

class Builder
Expand Down Expand Up @@ -190,6 +191,18 @@ public function dropIfExists($table)
}));
}

/**
* Drop all tables from the database.
*
* @return void
*
* @throws \LogicException
*/
public function dropAllTables()
{
throw new LogicException('This database builder type does not support dropAllTables()');
}

/**
* Rename a table on the schema.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
return 'drop table if exists '.$this->wrapTable($blueprint);
}

/**
* Compile a drop all tables command.
*
* @param string $tables
* @return string
*/
public function compileDropAllTables($tables)
{
return 'drop table '.implode(',', $tables).' cascade';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't some sort of escaping be used here? It's rarely, but schema and tables may have really weird names

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bohdan-shulha - thanks, good catch. I've updated the PR

}

/**
* Compile a get all tables command.
*
* @param string $schema
* @return string
*/
public function compileGetAllTables($schema)
{
return "select tablename from pg_catalog.pg_tables where schemaname = '$schema'";
}

/**
* Compile a drop column command.
*
Expand Down
30 changes: 30 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,16 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
return 'drop table if exists '.$this->wrapTable($blueprint);
}

/**
* Compile a drop all tables command.
*
* @return string
*/
public function compileDropAllTables()
{
return "delete from sqlite_master where type in ('table', 'index', 'trigger')";
}

/**
* Compile a drop column command.
*
Expand Down Expand Up @@ -292,6 +302,26 @@ public function compileDisableForeignKeyConstraints()
return 'PRAGMA foreign_keys = OFF;';
}

/**
* Compile the command to enable a writable schema.
*
* @return string
*/
public function compileEnableWriteableSchema()
{
return 'PRAGMA writable_schema = 1;';
}

/**
* Compile the command to disable a writable schema.
*
* @return string
*/
public function compileDisableWriteableSchema()
{
return 'PRAGMA writable_schema = 0;';
}

/**
* Create the column definition for a char type.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ public function compileDropIfExists(Blueprint $blueprint, Fluent $command)
);
}

/**
* Compile a drop all tables command.
*
* @return string
*/
public function compileDropAllTables()
{
return "EXEC sp_msforeachtable 'DROP TABLE ?'";
}

/**
* Compile a drop column command.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Schema/MySqlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,22 @@

class MySqlBuilder extends Builder
{
/**
* Drop all tables from the database.
*
* @return void
*/
public function dropAllTables()
{
$this->disableForeignKeyConstraints();

foreach ($this->connection->select('SHOW TABLES') as $table) {
$this->drop(get_object_vars($table)[key($table)]);
}

$this->enableForeignKeyConstraints();
}

/**
* Determine if the given table exists.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,26 @@

class PostgresBuilder extends Builder
{
/**
* Drop all tables from the database.
*
* @return void
*/
public function dropAllTables()
{
$tables = [];

foreach ($this->connection->select($this->grammar->compileGetAllTables($this->connection->getConfig('schema'))) as $table) {
$tables[] = get_object_vars($table)[key($table)];
}

if (empty($tables)) {
return;
}

$this->connection->statement($this->grammar->compileDropAllTables($tables));
}

/**
* Determine if the given table exists.
*
Expand Down
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/SQLiteBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Database\Schema;

class SQLiteBuilder extends Builder
{
/**
* Drop all tables from the database.
*
* @return void
*/
public function dropAllTables()
{
$this->connection->select($this->grammar->compileEnableWriteableSchema());

$this->connection->select($this->grammar->compileDropAllTables());

$this->connection->select($this->grammar->compileDisableWriteableSchema());
}
}
20 changes: 20 additions & 0 deletions src/Illuminate/Database/Schema/SqlServerBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Illuminate\Database\Schema;

class SqlServerBuilder extends Builder
{
/**
* Drop all tables from the database.
*
* @return void
*/
public function dropAllTables()
{
$this->disableForeignKeyConstraints();

$this->connection->statement($this->grammar->compileDropAllTables());

$this->enableForeignKeyConstraints();
}
}
15 changes: 15 additions & 0 deletions src/Illuminate/Database/SqlServerConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Exception;
use Throwable;
use Illuminate\Database\Schema\SqlServerBuilder;
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as DoctrineDriver;
use Illuminate\Database\Query\Processors\SqlServerProcessor;
use Illuminate\Database\Query\Grammars\SqlServerGrammar as QueryGrammar;
Expand Down Expand Up @@ -66,6 +67,20 @@ protected function getDefaultQueryGrammar()
return $this->withTablePrefix(new QueryGrammar);
}

/**
* Get a schema builder instance for the connection.
*
* @return \Illuminate\Database\Schema\SqlServerBuilder
*/
public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

return new SqlServerBuilder($this);
}

/**
* Get the default schema grammar instance.
*
Expand Down
58 changes: 58 additions & 0 deletions tests/Database/DatabaseSchemaBuilderIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use PHPUnit\Framework\TestCase;
use Illuminate\Database\Capsule\Manager as DB;

class DatabaseSchemaBuilderIntegrationTest extends TestCase
{
protected $db;

/**
* Bootstrap database.
*
* @return void
*/
public function setUp()
{
$this->db = $db = new DB;

$db->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);

$db->setAsGlobal();

$container = new Illuminate\Container\Container;
$container->instance('db', $db->getDatabaseManager());
Illuminate\Support\Facades\Facade::setFacadeApplication($container);
}

public function tearDown()
{
Illuminate\Support\Facades\Facade::clearResolvedInstances();
Illuminate\Support\Facades\Facade::setFacadeApplication(null);
}

public function testDropAllTablesWorksWithForeignKeys()
{
$this->db->connection()->getSchemaBuilder()->create('table1', function ($table) {
$table->integer('id');
$table->string('name');
});

$this->db->connection()->getSchemaBuilder()->create('table2', function ($table) {
$table->integer('id');
$table->string('user_id');
$table->foreign('user_id')->references('id')->on('table1');
});

$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasTable('table1'));
$this->assertTrue($this->db->connection()->getSchemaBuilder()->hasTable('table2'));

$this->db->connection()->getSchemaBuilder()->dropAllTables();

$this->assertFalse($this->db->connection()->getSchemaBuilder()->hasTable('table1'));
$this->assertFalse($this->db->connection()->getSchemaBuilder()->hasTable('table2'));
}
}