Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 9 additions & 11 deletions src/Illuminate/Database/Schema/PostgresBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,15 @@ public function dropAllTables()
{
$tables = [];

$excludedTables = $this->grammar->escapeNames(
$this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys']
);
$excludedTables = $this->connection->getConfig('dont_drop') ?? ['spatial_ref_sys'];

$schemas = $this->grammar->escapeNames($this->getSchemas());
$schemas = $this->getSchemas();

foreach ($this->getTables() as $table) {
$qualifiedName = $table['schema'].'.'.$table['name'];

if (empty(array_intersect($this->grammar->escapeNames([$table['name'], $qualifiedName]), $excludedTables))
&& in_array($this->grammar->escapeNames([$table['schema']])[0], $schemas)) {
if (in_array($table['schema'], $schemas) &&
empty(array_intersect([$table['name'], $qualifiedName], $excludedTables))) {
$tables[] = $qualifiedName;
}
}
Expand All @@ -130,10 +128,10 @@ public function dropAllViews()
{
$views = [];

$schemas = $this->grammar->escapeNames($this->getSchemas());
$schemas = $this->getSchemas();

foreach ($this->getViews() as $view) {
if (in_array($this->grammar->escapeNames([$view['schema']])[0], $schemas)) {
if (in_array($view['schema'], $schemas)) {
$views[] = $view['schema'].'.'.$view['name'];
}
}
Expand All @@ -157,10 +155,10 @@ public function dropAllTypes()
$types = [];
$domains = [];

$schemas = $this->grammar->escapeNames($this->getSchemas());
$schemas = $this->getSchemas();

foreach ($this->getTypes() as $type) {
if (! $type['implicit'] && in_array($this->grammar->escapeNames([$type['schema']])[0], $schemas)) {
if (! $type['implicit'] && in_array($type['schema'], $schemas)) {
if ($type['type'] === 'domain') {
$domains[] = $type['schema'].'.'.$type['name'];
} else {
Expand Down Expand Up @@ -236,7 +234,7 @@ public function getForeignKeys($table)
*
* @return array
*/
protected function getSchemas()
public function getSchemas()
{
return $this->parseSearchPath(
$this->connection->getConfig('search_path') ?: $this->connection->getConfig('schema') ?: 'public'
Expand Down
97 changes: 62 additions & 35 deletions src/Illuminate/Foundation/Testing/DatabaseTruncation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\PostgresBuilder;
use Illuminate\Foundation\Testing\Traits\CanConfigureMigrationCommands;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -84,32 +86,58 @@ protected function truncateTablesForConnection(ConnectionInterface $connection,

$connection->unsetEventDispatcher();

(new Collection(static::$allTables[$name] ??= $connection->getSchemaBuilder()->getTableListing()))
(new Collection($this->getAllTablesForConnection($connection, $name)))
->when(
property_exists($this, 'tablesToTruncate'),
fn ($tables) => $tables->intersect($this->tablesToTruncate),
fn ($tables) => $tables->diff($this->exceptTables($name))
$this->tablesToTruncate($connection, $name),
function (Collection $tables, array $tablesToTruncate) {
return $tables->filter(fn (array $table) => $this->tableExistsIn($table, $tablesToTruncate));
},
function (Collection $tables) use ($connection, $name) {
$exceptTables = $this->exceptTables($connection, $name);

return $tables->filter(fn (array $table) => ! $this->tableExistsIn($table, $exceptTables));
}
)
->filter(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->exists())
->each(fn ($table) => $connection->table($this->withoutTablePrefix($connection, $table))->truncate());
->each(function (array $table) use ($connection) {
$table = $connection->table(
new Expression($table['schema'] ? $table['schema'].'.'.$table['name'] : $table['name'])
);

if ($table->exists()) {
$table->truncate();
}
});

$connection->setEventDispatcher($dispatcher);
}

/**
* Remove the table prefix from a table name, if it exists.
*
* @param \Illuminate\Database\ConnectionInterface $connection
* @param string $table
* @return string
* Get all the tables that belong to the connection.
*/
protected function withoutTablePrefix(ConnectionInterface $connection, string $table)
protected function getAllTablesForConnection(ConnectionInterface $connection, ?string $name): array
{
$prefix = $connection->getTablePrefix();
if (isset(static::$allTables[$name])) {
return static::$allTables[$name];
}

$schema = $connection->getSchemaBuilder();

return str_starts_with($table, $prefix)
? substr($table, strlen($prefix))
: $table;
return static::$allTables[$name] = (new Collection($schema->getTables()))->when(
$schema instanceof PostgresBuilder ? $schema->getSchemas() : null,
fn (Collection $tables, array $schemas) => $tables->filter(
fn (array $table) => in_array($table['schema'], $schemas)
)
)->all();
}

/**
* Determine if a table exists in the given list, with or without its schema.
*/
protected function tableExistsIn(array $table, array $tables): bool
{
return $table['schema']
? ! empty(array_intersect([$table['name'], $table['schema'].'.'.$table['name']], $tables))
: in_array($table['name'], $tables);
}

/**
Expand All @@ -123,33 +151,32 @@ protected function connectionsToTruncate(): array
? $this->connectionsToTruncate : [null];
}

/**
* Get the tables that should be truncated.
*/
protected function tablesToTruncate(ConnectionInterface $connection, ?string $connectionName): ?array
{
return property_exists($this, 'tablesToTruncate') && is_array($this->tablesToTruncate)
? $this->tablesToTruncate[$connectionName] ?? $this->tablesToTruncate
: null;
}

/**
* Get the tables that should not be truncated.
*
* @param string|null $connectionName
* @return array
*/
protected function exceptTables(?string $connectionName): array
protected function exceptTables(ConnectionInterface $connection, ?string $connectionName): array
{
$migrations = $this->app['config']->get('database.migrations');

$migrationsTable = is_array($migrations) ? ($migrations['table'] ?? null) : $migrations;

if (property_exists($this, 'exceptTables')) {
if (array_is_list($this->exceptTables ?? [])) {
return array_merge(
$this->exceptTables ?? [],
[$migrationsTable],
);
}
$migrationsTable = is_array($migrations) ? ($migrations['table'] ?? 'migrations') : $migrations;
$migrationsTable = $connection->getTablePrefix().$migrationsTable;

return array_merge(
$this->exceptTables[$connectionName] ?? [],
return property_exists($this, 'exceptTables') && is_array($this->exceptTables)
? array_merge(
$this->exceptTables[$connectionName] ?? $this->exceptTables,
[$migrationsTable],
);
}

return [$migrationsTable];
)
: [$migrationsTable];
}

/**
Expand Down
11 changes: 0 additions & 11 deletions tests/Database/DatabasePostgresBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,6 @@ public function testDropAllTablesWhenSearchPathIsString()
$grammar->shouldReceive('compileTables')->andReturn('sql');
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'public']]);
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'public']]);
$grammar->shouldReceive('escapeNames')->with(['public'])->andReturn(['"public"']);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['users', 'public.users'])->andReturn(['"users"', '"public"."users"']);
$grammar->shouldReceive('compileDropAllTables')->with(['public.users'])->andReturn('drop table "public"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "public"."users" cascade');
$builder = $this->getBuilder($connection);
Expand All @@ -253,10 +250,6 @@ public function testDropAllTablesWhenSearchPathIsStringOfMany()
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'foouser']]);
$grammar->shouldReceive('compileTables')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'foouser']]);
$grammar->shouldReceive('escapeNames')->with(['foouser', 'public', 'foo_bar-Baz.Áüõß'])->andReturn(['"foouser"', '"public"', '"foo_bar-Baz"."Áüõß"']);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['foouser'])->andReturn(['"foouser"']);
$grammar->shouldReceive('escapeNames')->with(['users', 'foouser.users'])->andReturn(['"users"', '"foouser"."users"']);
$grammar->shouldReceive('compileDropAllTables')->with(['foouser.users'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);
Expand All @@ -282,10 +275,6 @@ public function testDropAllTablesWhenSearchPathIsArrayOfMany()
$processor->shouldReceive('processTables')->once()->andReturn([['name' => 'users', 'schema' => 'foouser']]);
$grammar->shouldReceive('compileTables')->andReturn('sql');
$connection->shouldReceive('selectFromWriteConnection')->with('sql')->andReturn([['name' => 'users', 'schema' => 'foouser']]);
$grammar->shouldReceive('escapeNames')->with(['foouser', 'dev', 'test', 'spaced schema'])->andReturn(['"foouser"', '"dev"', '"test"', '"spaced schema"']);
$grammar->shouldReceive('escapeNames')->with(['foo'])->andReturn(['"foo"']);
$grammar->shouldReceive('escapeNames')->with(['users', 'foouser.users'])->andReturn(['"users"', '"foouser"."users"']);
$grammar->shouldReceive('escapeNames')->with(['foouser'])->andReturn(['"foouser"']);
$grammar->shouldReceive('compileDropAllTables')->with(['foouser.users'])->andReturn('drop table "foouser"."users" cascade');
$connection->shouldReceive('statement')->with('drop table "foouser"."users" cascade');
$builder = $this->getBuilder($connection);
Expand Down
Loading