-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unique constraints support (#289)
- Loading branch information
Showing
20 changed files
with
433 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
## Unique Constraints | ||
|
||
You can create unique constraints in migration where new table is created. | ||
```php | ||
// create table with a unique constraint | ||
$this->table('users') | ||
->addColumn('username', 'string') | ||
->addColumn('password', 'string') | ||
->addColumn('created_at', 'datetime') | ||
->addColumn('updated_at', 'datetime') | ||
->addColumn('another_column', 'integer') | ||
->addUniqueConstraint('username', 'u_username')); | ||
->create(); | ||
``` | ||
|
||
Or add a unique constraint to an existing table same way: | ||
```php | ||
// create table | ||
$this->table('users') | ||
->addColumn('username', 'string') | ||
->addColumn('password', 'string') | ||
->addColumn('created_at', 'datetime') | ||
->addColumn('updated_at', 'datetime') | ||
->addColumn('another_column', 'integer') | ||
->create(); | ||
|
||
// add index | ||
$this->table('users') | ||
->addUniqueConstraint('username', 'u_username')); | ||
->save(); | ||
``` | ||
|
||
You can also specify a few columns to one unique constraint: | ||
```php | ||
// create table | ||
$this->table('users') | ||
->addColumn('username', 'string') | ||
->addColumn('sku', 'string') | ||
->addColumn('password', 'string') | ||
->addColumn('created_at', 'datetime') | ||
->addColumn('updated_at', 'datetime') | ||
->addColumn('another_column', 'integer') | ||
->addUniqueConstraint(['username', 'sku'], 'u_username_sku')); | ||
->create(); | ||
``` | ||
|
||
Keep in mind that this is the preferred way to add a unique constraint (at least for [PostgreSQL](https://www.postgresql.org/docs/9.4/indexes-unique.html)) and NOT a unique index. | ||
The use of indexes to enforce unique constraints could be considered an implementation detail that should not be accessed directly. | ||
|
||
|
||
**One should, however, be aware that there's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index.** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/Database/Element/Behavior/UniqueConstraintBehavior.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phoenix\Database\Element\Behavior; | ||
|
||
use Phoenix\Database\Element\MigrationTable; | ||
use Phoenix\Database\Element\UniqueConstraint; | ||
|
||
trait UniqueConstraintBehavior | ||
{ | ||
/** @var UniqueConstraint[] */ | ||
private array $uniqueConstraints = []; | ||
|
||
/** @var string[] */ | ||
private array $uniqueConstraintsToDrop = []; | ||
|
||
/** | ||
* One should be aware that for postgres there's no need to manually create indexes on unique columns. | ||
* Doing so would just duplicate the automatically-created index. | ||
* | ||
* @param string|string[] $columns | ||
* @param string $name | ||
* @return MigrationTable | ||
*/ | ||
public function addUniqueConstraint($columns, string $name): MigrationTable | ||
{ | ||
if (!is_array($columns)) { | ||
$columns = [$columns]; | ||
} | ||
$this->uniqueConstraints[] = new UniqueConstraint($columns, $name); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return UniqueConstraint[] | ||
*/ | ||
public function getUniqueConstraints(): array | ||
{ | ||
return $this->uniqueConstraints; | ||
} | ||
|
||
public function dropUniqueConstraint(string $name): MigrationTable | ||
{ | ||
$this->uniqueConstraintsToDrop[] = $name; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getUniqueConstraintsToDrop(): array | ||
{ | ||
return $this->uniqueConstraintsToDrop; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phoenix\Database\Element; | ||
|
||
use Phoenix\Behavior\ParamsCheckerBehavior; | ||
|
||
final class UniqueConstraint | ||
{ | ||
use ParamsCheckerBehavior; | ||
|
||
/** @var string[] */ | ||
private array $columns; | ||
|
||
private string $name; | ||
|
||
/** | ||
* @param string[] $columns | ||
* @param string $name | ||
*/ | ||
public function __construct(array $columns, string $name) | ||
{ | ||
$this->columns = $columns; | ||
$this->name = $name; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getColumns(): array | ||
{ | ||
return $this->columns; | ||
} | ||
} |
Oops, something went wrong.