Skip to content

[7.x] Add the ability to create indexes as expressions #32411

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

Merged
merged 1 commit into from
Apr 17, 2020
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
13 changes: 13 additions & 0 deletions src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BadMethodCallException;
use Closure;
use Illuminate\Database\Connection;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Grammars\Grammar;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Support\Fluent;
Expand Down Expand Up @@ -521,6 +522,18 @@ public function spatialIndex($columns, $name = null)
return $this->indexCommand('spatialIndex', $columns, $name);
}

/**
* Specify a raw index for the table.
*
* @param string $expression
* @param string $name
* @return \Illuminate\Support\Fluent
*/
public function rawIndex($expression, $name)
{
return $this->index([new Expression($expression)], $name);
}

/**
* Specify a foreign key for the table.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseMySqlSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ public function testAddingFluentSpatialIndex()
$this->assertSame('alter table `geo` add spatial index `geo_coordinates_spatialindex`(`coordinates`)', $statements[1]);
}

public function testAddingRawIndex()
{
$blueprint = new Blueprint('users');
$blueprint->rawIndex('(function(column))', 'raw_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('alter table `users` add index `raw_index`((function(column)))', $statements[0]);
}

public function testAddingForeignKey()
{
$blueprint = new Blueprint('users');
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,16 @@ public function testAddingFluentSpatialIndex()
$this->assertSame('create index "geo_coordinates_spatialindex" on "geo" using gist ("coordinates")', $statements[1]);
}

public function testAddingRawIndex()
{
$blueprint = new Blueprint('users');
$blueprint->rawIndex('(function(column))', 'raw_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
}

public function testAddingIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseSQLiteSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,16 @@ public function testAddingFluentSpatialIndex()
$blueprint->toSql($this->getConnection(), $this->getGrammar());
}

public function testAddingRawIndex()
{
$blueprint = new Blueprint('users');
$blueprint->rawIndex('(function(column))', 'raw_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
}

public function testAddingIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down
10 changes: 10 additions & 0 deletions tests/Database/DatabaseSqlServerSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,16 @@ public function testAddingFluentSpatialIndex()
$this->assertSame('create spatial index "geo_coordinates_spatialindex" on "geo" ("coordinates")', $statements[1]);
}

public function testAddingRawIndex()
{
$blueprint = new Blueprint('users');
$blueprint->rawIndex('(function(column))', 'raw_index');
$statements = $blueprint->toSql($this->getConnection(), $this->getGrammar());

$this->assertCount(1, $statements);
$this->assertSame('create index "raw_index" on "users" ((function(column)))', $statements[0]);
}

public function testAddingIncrementingID()
{
$blueprint = new Blueprint('users');
Expand Down