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

added feature to create automatic indexing on deleted_at field #52796

Draft
wants to merge 2 commits into
base: 11.x
Choose a base branch
from
Draft
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
15 changes: 9 additions & 6 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -1284,35 +1284,38 @@ public function datetimes($precision = 0)
*
* @param string $column
* @param int|null $precision
* @param bool $index
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletes($column = 'deleted_at', $precision = 0)
public function softDeletes($column = 'deleted_at', $precision = 0, $index = true)
{
return $this->timestamp($column, $precision)->nullable();
return $index ? $this->timestamp($column, $precision)->nullable()->index() : $this->timestamp($column, $precision)->nullable();
}

/**
* Add a "deleted at" timestampTz for the table.
*
* @param string $column
* @param int|null $precision
* @param bool $index
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletesTz($column = 'deleted_at', $precision = 0)
public function softDeletesTz($column = 'deleted_at', $precision = 0, $index = true)
{
return $this->timestampTz($column, $precision)->nullable();
return $index ? $this->timestampTz($column, $precision)->nullable()->index() : $this->timestampTz($column, $precision)->nullable();
}

/**
* Add a "deleted at" datetime column to the table.
*
* @param string $column
* @param int|null $precision
* @param bool $index
* @return \Illuminate\Database\Schema\ColumnDefinition
*/
public function softDeletesDatetime($column = 'deleted_at', $precision = 0)
public function softDeletesDatetime($column = 'deleted_at', $precision = 0, $index = true)
{
return $this->datetime($column, $precision)->nullable();
return $index ? $this->datetime($column, $precision)->nullable()->index() : $this->datetime($column, $precision)->nullable();
}

/**
Expand Down
95 changes: 95 additions & 0 deletions tests/Database/DatabaseSoftDeleteWithIndexTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Illuminate\Tests\Database;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Illuminate\Database\Schema\Blueprint;

class DatabaseSoftDeleteWithIndexTest extends TestCase
{
protected function tearDown(): void
{
m::close();
}

public function testSoftDeletesWithIndex()
{
$blueprint = new Blueprint('test');

$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletes('deleted_at', 0, true);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);
$this->assertStringContainsString('alter table `test` add `deleted_at` timestamp null', $statements[0]);
$this->assertStringContainsString('alter table `test` add index `deleted_at_idx`(`deleted_at`)', $statements[1]);
}

public function testSoftDeletesWithoutIndex()
{
$blueprint = new Blueprint('test');
$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletes('deleted_at', 0, false);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);

$this->assertStringContainsString('`deleted_at` timestamp null', $statements[0]);
$this->assertCount(2, $statements);
}

public function testSoftDeletesTzWithIndex()
{
$blueprint = new Blueprint('test');
$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletesTz('deleted_at', 0, true);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);

$this->assertStringContainsString('`deleted_at` timestamp null', $statements[0]);
$this->assertStringContainsString('alter table `test` add index `deleted_at_idx`(`deleted_at`)', $statements[1]);
}

public function testSoftDeletesTzWithoutIndex()
{
$blueprint = new Blueprint('test');
$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletesTz('deleted_at', 0, false);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);

$this->assertStringContainsString('`deleted_at` timestamp null', $statements[0]);
$this->assertCount(2, $statements);
}

public function testSoftDeletesDatetimeWithIndex()
{
$blueprint = new Blueprint('test');
$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletesDatetime('deleted_at', 0, true);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);

$this->assertStringContainsString('`deleted_at` datetime null', $statements[0]);
$this->assertStringContainsString('alter table `test` add index `deleted_at_idx`(`deleted_at`)', $statements[1]);
}

public function testSoftDeletesDatetimeWithoutIndex()
{
$blueprint = new Blueprint('test');
$connection = m::mock('Illuminate\Database\Connection');

$blueprint->softDeletesDatetime('deleted_at', 0, false);
$blueprint->index(['deleted_at'], 'deleted_at_idx');
$statements = $blueprint->toSql($connection, new \Illuminate\Database\Schema\Grammars\MySqlGrammar);

$this->assertStringContainsString('`deleted_at` datetime null', $statements[0]);
$this->assertCount(2, $statements);
}
}


Loading