-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #666 from cakephp/port-action
Port the Db/Action code and tests from phinx
- Loading branch information
Showing
17 changed files
with
851 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Table\Table; | ||
|
||
abstract class Action | ||
{ | ||
/** | ||
* @var \Migrations\Db\Table\Table | ||
*/ | ||
protected Table $table; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table the Table to apply the action to | ||
*/ | ||
public function __construct(Table $table) | ||
{ | ||
$this->table = $table; | ||
} | ||
|
||
/** | ||
* The table this action will be applied to | ||
* | ||
* @return \Migrations\Db\Table\Table | ||
*/ | ||
public function getTable(): Table | ||
{ | ||
return $this->table; | ||
} | ||
} |
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,64 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Literal; | ||
use Migrations\Db\Table\Column; | ||
use Migrations\Db\Table\Table; | ||
|
||
class AddColumn extends Action | ||
{ | ||
/** | ||
* The column to add | ||
* | ||
* @var \Migrations\Db\Table\Column | ||
*/ | ||
protected Column $column; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to add the column to | ||
* @param \Migrations\Db\Table\Column $column The column to add | ||
*/ | ||
public function __construct(Table $table, Column $column) | ||
{ | ||
parent::__construct($table); | ||
$this->column = $column; | ||
} | ||
|
||
/** | ||
* Returns a new AddColumn object after assembling the given commands | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to add the column to | ||
* @param string $columnName The column name | ||
* @param string|\Migrations\Db\Literal $type The column type | ||
* @param array<string, mixed> $options The column options | ||
* @return self | ||
*/ | ||
public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): self | ||
{ | ||
$column = new Column(); | ||
$column->setName($columnName); | ||
$column->setType($type); | ||
$column->setOptions($options); // map options to column methods | ||
|
||
return new AddColumn($table, $column); | ||
} | ||
|
||
/** | ||
* Returns the column to be added | ||
* | ||
* @return \Migrations\Db\Table\Column | ||
*/ | ||
public function getColumn(): Column | ||
{ | ||
return $this->column; | ||
} | ||
} |
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,79 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Table\ForeignKey; | ||
use Migrations\Db\Table\Table; | ||
|
||
class AddForeignKey extends Action | ||
{ | ||
/** | ||
* The foreign key to add | ||
* | ||
* @var \Migrations\Db\Table\ForeignKey | ||
*/ | ||
protected ForeignKey $foreignKey; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to add the foreign key to | ||
* @param \Migrations\Db\Table\ForeignKey $fk The foreign key to add | ||
*/ | ||
public function __construct(Table $table, ForeignKey $fk) | ||
{ | ||
parent::__construct($table); | ||
$this->foreignKey = $fk; | ||
} | ||
|
||
/** | ||
* Creates a new AddForeignKey object after building the foreign key with | ||
* the passed attributes | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table object to add the foreign key to | ||
* @param string|string[] $columns The columns for the foreign key | ||
* @param \Migrations\Db\Table\Table|string $referencedTable The table the foreign key references | ||
* @param string|string[] $referencedColumns The columns in the referenced table | ||
* @param array<string, mixed> $options Extra options for the foreign key | ||
* @param string|null $name The name of the foreign key | ||
* @return self | ||
*/ | ||
public static function build(Table $table, string|array $columns, Table|string $referencedTable, string|array $referencedColumns = ['id'], array $options = [], ?string $name = null): self | ||
{ | ||
if (is_string($referencedColumns)) { | ||
$referencedColumns = [$referencedColumns]; // str to array | ||
} | ||
|
||
if (is_string($referencedTable)) { | ||
$referencedTable = new Table($referencedTable); | ||
} | ||
|
||
$fk = new ForeignKey(); | ||
$fk->setReferencedTable($referencedTable) | ||
->setColumns($columns) | ||
->setReferencedColumns($referencedColumns) | ||
->setOptions($options); | ||
|
||
if ($name !== null) { | ||
$fk->setConstraint($name); | ||
} | ||
|
||
return new AddForeignKey($table, $fk); | ||
} | ||
|
||
/** | ||
* Returns the foreign key to be added | ||
* | ||
* @return \Migrations\Db\Table\ForeignKey | ||
*/ | ||
public function getForeignKey(): ForeignKey | ||
{ | ||
return $this->foreignKey; | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Table\Index; | ||
use Migrations\Db\Table\Table; | ||
|
||
class AddIndex extends Action | ||
{ | ||
/** | ||
* The index to add to the table | ||
* | ||
* @var \Migrations\Db\Table\Index | ||
*/ | ||
protected Index $index; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to add the index to | ||
* @param \Migrations\Db\Table\Index $index The index to be added | ||
*/ | ||
public function __construct(Table $table, Index $index) | ||
{ | ||
parent::__construct($table); | ||
$this->index = $index; | ||
} | ||
|
||
/** | ||
* Creates a new AddIndex object after building the index object with the | ||
* provided arguments | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to add the index to | ||
* @param string|string[]|\Migrations\Db\Table\Index $columns The columns to index | ||
* @param array<string, mixed> $options Additional options for the index creation | ||
* @return self | ||
*/ | ||
public static function build(Table $table, string|array|Index $columns, array $options = []): self | ||
{ | ||
// create a new index object if strings or an array of strings were supplied | ||
if (!($columns instanceof Index)) { | ||
$index = new Index(); | ||
|
||
$index->setColumns($columns); | ||
$index->setOptions($options); | ||
} else { | ||
$index = $columns; | ||
} | ||
|
||
return new AddIndex($table, $index); | ||
} | ||
|
||
/** | ||
* Returns the index to be added | ||
* | ||
* @return \Migrations\Db\Table\Index | ||
*/ | ||
public function getIndex(): Index | ||
{ | ||
return $this->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Literal; | ||
use Migrations\Db\Table\Column; | ||
use Migrations\Db\Table\Table; | ||
|
||
class ChangeColumn extends Action | ||
{ | ||
/** | ||
* The column definition | ||
* | ||
* @var \Migrations\Db\Table\Column | ||
*/ | ||
protected Column $column; | ||
|
||
/** | ||
* The name of the column to be changed | ||
* | ||
* @var string | ||
*/ | ||
protected string $columnName; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to alter | ||
* @param string $columnName The name of the column to change | ||
* @param \Migrations\Db\Table\Column $column The column definition | ||
*/ | ||
public function __construct(Table $table, string $columnName, Column $column) | ||
{ | ||
parent::__construct($table); | ||
$this->columnName = $columnName; | ||
$this->column = $column; | ||
|
||
// if the name was omitted use the existing column name | ||
if ($column->getName() === null || strlen((string)$column->getName()) === 0) { | ||
$column->setName($columnName); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a new ChangeColumn object after building the column definition | ||
* out of the provided arguments | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to alter | ||
* @param string $columnName The name of the column to change | ||
* @param string|\Migrations\Db\Literal $type The type of the column | ||
* @param array<string, mixed> $options Additional options for the column | ||
* @return self | ||
*/ | ||
public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): self | ||
{ | ||
$column = new Column(); | ||
$column->setName($columnName); | ||
$column->setType($type); | ||
$column->setOptions($options); // map options to column methods | ||
|
||
return new ChangeColumn($table, $columnName, $column); | ||
} | ||
|
||
/** | ||
* Returns the name of the column to change | ||
* | ||
* @return string | ||
*/ | ||
public function getColumnName(): string | ||
{ | ||
return $this->columnName; | ||
} | ||
|
||
/** | ||
* Returns the column definition | ||
* | ||
* @return \Migrations\Db\Table\Column | ||
*/ | ||
public function getColumn(): Column | ||
{ | ||
return $this->column; | ||
} | ||
} |
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,43 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Action; | ||
|
||
use Migrations\Db\Table\Table; | ||
|
||
class ChangeComment extends Action | ||
{ | ||
/** | ||
* The new comment for the table | ||
* | ||
* @var string|null | ||
*/ | ||
protected ?string $newComment = null; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Migrations\Db\Table\Table $table The table to be changed | ||
* @param string|null $newComment The new comment for the table | ||
*/ | ||
public function __construct(Table $table, ?string $newComment) | ||
{ | ||
parent::__construct($table); | ||
$this->newComment = $newComment; | ||
} | ||
|
||
/** | ||
* Return the new comment for the table | ||
* | ||
* @return string|null | ||
*/ | ||
public function getNewComment(): ?string | ||
{ | ||
return $this->newComment; | ||
} | ||
} |
Oops, something went wrong.