Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit b27974c

Browse files
authored
Merge pull request #21 from spiral/feature/named-indexes
Feature/named indexes
2 parents 48181cc + c7ab13c commit b27974c

File tree

5 files changed

+47
-11
lines changed

5 files changed

+47
-11
lines changed

source/Spiral/ORM/RecordEntity.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ abstract class RecordEntity extends AbstractRecord implements RecordInterface
145145
*
146146
* @see Record::INDEXES
147147
*/
148-
const INDEX = 1000; //Default index type
149-
const UNIQUE = 2000; //Unique index definition
148+
const INDEX = 1000; //Default index type
149+
const UNIQUE = 2000; //Unique index definition
150150

151151
/*
152152
* ================================================

source/Spiral/ORM/Schemas/Definitions/IndexDefinition.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
final class IndexDefinition
1414
{
15+
/**
16+
* @var string|null
17+
*/
18+
private $name;
19+
1520
/**
1621
* @var array
1722
*/
@@ -23,13 +28,16 @@ final class IndexDefinition
2328
private $unique;
2429

2530
/**
26-
* @param array $columns
27-
* @param bool $unique
31+
* @param array $columns
32+
* @param bool $unique
33+
* @param string|null $name
2834
*/
29-
public function __construct(array $columns, bool $unique = false)
35+
public function __construct(array $columns, bool $unique = false, string $name = null)
3036
{
3137
$this->index = $columns;
3238
$this->unique = $unique;
39+
40+
$this->name = $name;
3341
}
3442

3543
/**
@@ -55,6 +63,10 @@ public function isUnique(): bool
5563
*/
5664
public function getName(): string
5765
{
66+
if (!empty($this->name)) {
67+
return $this->name;
68+
}
69+
5870
$name = ($this->isUnique() ? 'unique_' : 'index_') . join('_', $this->getColumns());
5971

6072
return strlen($name) > 64 ? md5($name) : $name;

source/Spiral/ORM/Schemas/RecordSchema.php

+21-5
Original file line numberDiff line numberDiff line change
@@ -330,16 +330,32 @@ protected function isRelation($type): bool
330330
*/
331331
protected function castIndex(array $definition)
332332
{
333+
$name = null;
333334
$unique = null;
334335
$columns = [];
335336

336-
foreach ($definition as $chunk) {
337-
if ($chunk == RecordEntity::INDEX || $chunk == RecordEntity::UNIQUE) {
338-
$unique = $chunk === RecordEntity::UNIQUE;
337+
338+
foreach ($definition as $key => $value) {
339+
340+
if ($key == RecordEntity::INDEX || $key == RecordEntity::UNIQUE) {
341+
$unique = ($key === RecordEntity::UNIQUE);
342+
343+
if (!is_string($value) || empty($value)){
344+
throw new DefinitionException(
345+
"Record '{$this}' has index definition with invalid index name"
346+
);
347+
}
348+
349+
$name = $value;
350+
continue;
351+
}
352+
353+
if ($value == RecordEntity::INDEX || $value == RecordEntity::UNIQUE) {
354+
$unique = ($value === RecordEntity::UNIQUE);
339355
continue;
340356
}
341357

342-
$columns[] = $chunk;
358+
$columns[] = $value;
343359
}
344360

345361
if (is_null($unique)) {
@@ -354,7 +370,7 @@ protected function castIndex(array $definition)
354370
);
355371
}
356372

357-
return new IndexDefinition($columns, $unique);
373+
return new IndexDefinition($columns, $unique, $name);
358374
}
359375

360376
/**

tests/ORM/ColumnRendererTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
namespace Spiral\Tests\ORM;
88

99
use Spiral\ORM\Helpers\ColumnRenderer;
10+
use Spiral\Tests\ORM\Fixtures\User;
1011

1112
abstract class ColumnRendererTest extends BaseTest
1213
{
14+
public function testNamedIndexes()
15+
{
16+
$table = $this->orm->table(User::class);
17+
$this->assertSame('status_index', $table->getSchema()->index(['status'])->getName());
18+
}
19+
20+
1321
public function testRenderString()
1422
{
1523
$table = $this->db->sample->getSchema();

tests/ORM/Fixtures/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ class User extends BaseRecord implements
5050
];
5151

5252
const INDEXES = [
53-
[self::INDEX, 'status']
53+
[self::INDEX => 'status_index', 'status']
5454
];
5555
}

0 commit comments

Comments
 (0)