Skip to content

Commit 47e8beb

Browse files
author
Fureev Eugene
committed
feat: Add Partial index
1 parent 4b9bd10 commit 47e8beb

File tree

9 files changed

+345
-19
lines changed

9 files changed

+345
-19
lines changed

.meta.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ class Schema
1919
use Illuminate\Support\Fluent;
2020
use Php\Support\Laravel\Database\Schema\Definitions\LikeDefinition;
2121
use Php\Support\Laravel\Database\Schema\Definitions\UniqueDefinition;
22+
use Php\Support\Laravel\Database\Schema\Definitions\PartialDefinition;
2223
use Php\Support\Laravel\Database\Schema\Definitions\ViewDefinition;
2324

2425
/**
2526
* @method LikeDefinition like(string $table)
2627
* @method Fluent ifNotExists()
28+
* @method PartialDefinition partial($columns, ?string $index = null, ?string $algorithm = null)
2729
* @method UniqueDefinition uniquePartial($columns, ?string $index = null, ?string $algorithm = null)
2830
* @method ViewDefinition createView(string $view, string $select, bool $materialize = false)
2931
* @method ViewDefinition createViewOrReplace(string $view, string $select, bool $materialize = false)

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ Check MD [online][check-online].
99

1010
## [unreleased]
1111

12+
## [1.10.0] - 2023-03-27
13+
14+
### Added
15+
16+
- Add `Partial index`
17+
1218
## [1.9.0] - 2023-02-24
1319

1420
### Added

readme.md

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,29 @@ composer require efureev/laravel-support-db "^1.9"
2020
## Contents
2121

2222
- [Ext Column Types](#ext-column-types)
23-
- [Bit](#bit)
24-
- [GeoPoint](#geo-point)
25-
- [GeoPath](#geo-path)
26-
- [IP Network](#ip-network)
27-
- [Ranges](#ranges)
28-
- [UUID](#uuid)
29-
- [XML](#xml)
30-
- [Array of UUID](#array-of-uuid)
31-
- [Array of Integer](#array-of-integer)
23+
- [Bit](#bit)
24+
- [GeoPoint](#geo-point)
25+
- [GeoPath](#geo-path)
26+
- [IP Network](#ip-network)
27+
- [Ranges](#ranges)
28+
- [UUID](#uuid)
29+
- [XML](#xml)
30+
- [Array of UUID](#array-of-uuid)
31+
- [Array of Integer](#array-of-integer)
3232
- [Column Options](#column-options)
33-
- [Compression](#compression)
33+
- [Compression](#compression)
3434
- [Views](#views)
3535
- [Indexes](#indexes)
36-
- [Unique Partial indexes](#unique-partial-indexes)
36+
- [Partial indexes](#partial-indexes)
37+
- [Unique Partial indexes](#unique-partial-indexes)
3738
- [Extended Schema](#extended-schema)
38-
- [Create like another table](#create-like-another-table)
39-
- [Create as another table with full data](#create-as-another-table-with-full-data)
40-
- [Create as another table with data from select query](#create-as-another-table-with-data-from-select-query)
41-
- [Drop Cascade If Exists](#drop-cascade-if-exists)
39+
- [Create like another table](#create-like-another-table)
40+
- [Create as another table with full data](#create-as-another-table-with-full-data)
41+
- [Create as another table with data from select query](#create-as-another-table-with-data-from-select-query)
42+
- [Drop Cascade If Exists](#drop-cascade-if-exists)
4243
- [Extended Query Builder](#extended-query-builder)
43-
- [Update records and return deleted records` columns](#update-records-and-return-updated-records-columns)
44-
- [Delete records and return deleted records` columns](#delete-records-and-return-deleted-records-columns)
44+
- [Update records and return deleted records` columns](#update-records-and-return-updated-records-columns)
45+
- [Delete records and return deleted records` columns](#delete-records-and-return-deleted-records-columns)
4546
- [Extensions](#extensions)
4647

4748
### Ext Column Types
@@ -201,6 +202,33 @@ Schema::dropViewIfExists('active_users');
201202

202203
### Indexes
203204

205+
#### Partial indexes
206+
207+
See: https://www.postgresql.org/docs/current/indexes-partial.html
208+
209+
Example:
210+
211+
```php
212+
use \Php\Support\Laravel\Database\Schema\Postgres\Blueprint;
213+
Schema::create('table', static function (Blueprint $table) {
214+
$table->string('code');
215+
$table->softDeletes();
216+
$table
217+
->partial('code')
218+
->whereNull('deleted_at');
219+
});
220+
```
221+
222+
If you want to delete partial index, use this method:
223+
224+
```php
225+
use \Php\Support\Laravel\Database\Schema\Postgres\Blueprint;
226+
227+
Schema::create('table', static function (Blueprint $table) {
228+
$table->dropPartial(['code']);
229+
});
230+
```
231+
204232
#### Unique Partial indexes
205233

206234
Example:
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Laravel\Database\Schema\Definitions;
6+
7+
use Illuminate\Support\Fluent;
8+
9+
/**
10+
* @method self where(string $column, $operator, $value, $boolean = 'and')
11+
* @method self whereRaw($sql, $bindings = [], $boolean = 'and')
12+
* @method self whereColumn($first, $operator, $second, $boolean = 'and')
13+
* @method self whereIn(string $column, $values = [], $boolean = 'and', $not = false)
14+
* @method self whereNotIn(string $column, $values = [], $boolean = 'and')
15+
* @method self whereBetween(string $column, $values, $boolean = 'and', $not = false)
16+
* @method self whereNotBetween(string $column, $values, $boolean = 'and')
17+
* @method self whereNull(string $column, $boolean = 'and', $not = false)
18+
* @method self whereNotNull(string $column, $boolean = 'and')
19+
* @method self whereBool(string $column, bool $value, $boolean = 'and')
20+
* @method self whereTrue(string $column, $boolean = 'and')
21+
* @method self whereFalse(string $column, $boolean = 'and')
22+
*/
23+
class PartialDefinition extends Fluent
24+
{
25+
}

src/Schema/Postgres/Blueprint.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
use Illuminate\Support\Fluent;
1313
use Php\Support\Laravel\Database\Schema\Definitions\ColumnDefinition;
1414
use Php\Support\Laravel\Database\Schema\Definitions\LikeDefinition;
15+
use Php\Support\Laravel\Database\Schema\Definitions\PartialDefinition;
1516
use Php\Support\Laravel\Database\Schema\Definitions\UniqueDefinition;
1617
use Php\Support\Laravel\Database\Schema\Definitions\ViewDefinition;
18+
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\PartialBuilder;
1719
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\Unique\UniqueBuilder;
1820

1921
class Blueprint extends BaseBlueprint
@@ -180,7 +182,7 @@ public function intArray(string $column): ColumnDefinition
180182
*
181183
* @return ColumnDefinition
182184
*/
183-
public function addColumn($type, $name, array $parameters = [])
185+
public function addColumn($type, $name, array $parameters = []): ColumnDefinition
184186
{
185187
return $this->addColumnDefinition(
186188
new ColumnDefinition(
@@ -282,6 +284,26 @@ public function uniquePartial($columns, ?string $index = null, ?string $algorith
282284
);
283285
}
284286

287+
/**
288+
* @param array|string $columns
289+
* @param string|null $index
290+
* @param string|null $algorithm
291+
*
292+
* @return PartialDefinition|PartialBuilder
293+
*/
294+
public function partial($columns, ?string $index = null, ?string $algorithm = null): Fluent
295+
{
296+
$columns = (array)$columns;
297+
298+
$index = $index ?: $this->createIndexName('partial', $columns);
299+
300+
return $this->addExtendedCommand(
301+
PartialBuilder::class,
302+
'partial',
303+
compact('columns', 'index', 'algorithm')
304+
);
305+
}
306+
285307
public function ginIndex($columns, ?string $name = null): Fluent
286308
{
287309
return $this->indexCommand('index', $columns, $name, 'gin');
@@ -292,6 +314,11 @@ public function dropUniquePartial($index): Fluent
292314
return $this->dropIndexCommand('dropIndex', 'unique', $index);
293315
}
294316

317+
public function dropPartial($index): Fluent
318+
{
319+
return $this->dropIndexCommand('dropIndex', 'partial', $index);
320+
}
321+
295322
protected function getSchemaManager()
296323
{
297324
return Schema::getConnection()->getDoctrineSchemaManager();
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes;
6+
7+
use Illuminate\Support\Fluent;
8+
use Php\Support\Laravel\Database\Schema\Postgres\Builders\WhereBuilderTrait;
9+
10+
class PartialBuilder extends Fluent
11+
{
12+
use WhereBuilderTrait;
13+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Php\Support\Laravel\Database\Schema\Postgres\Compilers;
6+
7+
use Php\Support\Laravel\Database\Schema\Postgres\Blueprint;
8+
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\PartialBuilder;
9+
use Php\Support\Laravel\Database\Schema\Postgres\Grammar;
10+
11+
class PartialCompiler
12+
{
13+
use WheresBuilder;
14+
15+
public static function compile(
16+
Grammar $grammar,
17+
Blueprint $blueprint,
18+
PartialBuilder $fluent
19+
): string {
20+
$wheres = static::build($grammar, $blueprint, $fluent);
21+
22+
$where = count($wheres) === 0 ? '' : ' WHERE %s';
23+
return sprintf(
24+
"CREATE INDEX %s ON %s (%s)$where",
25+
$fluent->get('index'),
26+
$blueprint->getTable(),
27+
implode(',', (array)$fluent->get('columns')),
28+
static::removeLeadingBoolean(implode(' ', $wheres))
29+
);
30+
}
31+
}

src/Schema/Postgres/Grammar/GrammarIndexes.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace Php\Support\Laravel\Database\Schema\Postgres\Grammar;
66

7-
use Illuminate\Support\Fluent;
87
use Php\Support\Laravel\Database\Schema\Postgres\Blueprint;
8+
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\PartialBuilder;
99
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\Unique\UniqueBuilder;
1010
use Php\Support\Laravel\Database\Schema\Postgres\Builders\Indexes\Unique\UniquePartialBuilder;
11+
use Php\Support\Laravel\Database\Schema\Postgres\Compilers\PartialCompiler;
1112
use Php\Support\Laravel\Database\Schema\Postgres\Compilers\UniqueCompiler;
1213

1314
trait GrammarIndexes
@@ -20,4 +21,9 @@ public function compileUniquePartial(Blueprint $blueprint, UniqueBuilder $comman
2021
}
2122
return $this->compileUnique($blueprint, $command);
2223
}
24+
25+
public function compilePartial(Blueprint $blueprint, PartialBuilder $command): string
26+
{
27+
return PartialCompiler::compile($this, $blueprint, $command);
28+
}
2329
}

0 commit comments

Comments
 (0)