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 4 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
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
22 changes: 22 additions & 0 deletions src/Illuminate/Database/Schema/SQLiteBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Illuminate\Database\Schema;

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

if (file_exists($dbPath)) {
unlink($dbPath);
Copy link
Contributor

@casperboone casperboone Mar 26, 2017

Choose a reason for hiding this comment

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

I think this will give problems when someone tries to drop all tables in an in-memory SQLite database (which is of course not a very common use case, but still). An approach similar to the Postgres solution might be better.

You can get a list of tables with:

SELECT name FROM sqlite_master WHERE type='table'

Copy link
Contributor Author

@laurencei laurencei Mar 26, 2017

Choose a reason for hiding this comment

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

@casperboone - thanks. I'm not worried about in-memory calls - I dont think that will ever happen.

But I didnt like that the command was doing a file manipulation - that didnt feel very right.

Using your code as a starting point - I've come up with a pure sql solution that should clear a sqlite database without actually deleting the file.

Should work for both in-memory and file sqlite databases.

Thanks.

}

touch($dbPath);
}
}
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