From fae0341423dde6bba0327f1abacb7561432dca31 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 5 Dec 2023 23:11:26 -0500 Subject: [PATCH 1/2] Port the Db/Action code and tests from phinx Simple import of more files from phinx. Working towards having all of the necessary pieces in place before incrementally removing phinx adapter. --- src/Db/Action/Action.php | 39 +++++++++++++ src/Db/Action/AddColumn.php | 64 +++++++++++++++++++++ src/Db/Action/AddForeignKey.php | 79 ++++++++++++++++++++++++++ src/Db/Action/AddIndex.php | 68 +++++++++++++++++++++++ src/Db/Action/ChangeColumn.php | 89 ++++++++++++++++++++++++++++++ src/Db/Action/ChangeComment.php | 43 +++++++++++++++ src/Db/Action/ChangePrimaryKey.php | 43 +++++++++++++++ src/Db/Action/CreateTable.php | 13 +++++ src/Db/Action/DropForeignKey.php | 69 +++++++++++++++++++++++ src/Db/Action/DropIndex.php | 76 +++++++++++++++++++++++++ src/Db/Action/DropTable.php | 13 +++++ src/Db/Action/RemoveColumn.php | 60 ++++++++++++++++++++ src/Db/Action/RenameColumn.php | 80 +++++++++++++++++++++++++++ src/Db/Action/RenameTable.php | 43 +++++++++++++++ src/Db/Literal.php | 43 +++++++++++++++ src/Db/Table/Column.php | 8 +-- tests/TestCase/Db/LiteralTest.php | 25 +++++++++ 17 files changed, 851 insertions(+), 4 deletions(-) create mode 100644 src/Db/Action/Action.php create mode 100644 src/Db/Action/AddColumn.php create mode 100644 src/Db/Action/AddForeignKey.php create mode 100644 src/Db/Action/AddIndex.php create mode 100644 src/Db/Action/ChangeColumn.php create mode 100644 src/Db/Action/ChangeComment.php create mode 100644 src/Db/Action/ChangePrimaryKey.php create mode 100644 src/Db/Action/CreateTable.php create mode 100644 src/Db/Action/DropForeignKey.php create mode 100644 src/Db/Action/DropIndex.php create mode 100644 src/Db/Action/DropTable.php create mode 100644 src/Db/Action/RemoveColumn.php create mode 100644 src/Db/Action/RenameColumn.php create mode 100644 src/Db/Action/RenameTable.php create mode 100644 src/Db/Literal.php create mode 100644 tests/TestCase/Db/LiteralTest.php diff --git a/src/Db/Action/Action.php b/src/Db/Action/Action.php new file mode 100644 index 00000000..66adb808 --- /dev/null +++ b/src/Db/Action/Action.php @@ -0,0 +1,39 @@ +table = $table; + } + + /** + * The table this action will be applied to + * + * @return \Migrations\Db\Table\Table + */ + public function getTable(): Table + { + return $this->table; + } +} diff --git a/src/Db/Action/AddColumn.php b/src/Db/Action/AddColumn.php new file mode 100644 index 00000000..e9d97b6a --- /dev/null +++ b/src/Db/Action/AddColumn.php @@ -0,0 +1,64 @@ +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 $options The column options + * @return static + */ + public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static + { + $column = new Column(); + $column->setName($columnName); + $column->setType($type); + $column->setOptions($options); // map options to column methods + + return new static($table, $column); + } + + /** + * Returns the column to be added + * + * @return \Migrations\Db\Table\Column + */ + public function getColumn(): Column + { + return $this->column; + } +} diff --git a/src/Db/Action/AddForeignKey.php b/src/Db/Action/AddForeignKey.php new file mode 100644 index 00000000..21b26706 --- /dev/null +++ b/src/Db/Action/AddForeignKey.php @@ -0,0 +1,79 @@ +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 $options Extra options for the foreign key + * @param string|null $name The name of the foreign key + * @return static + */ + public static function build(Table $table, string|array $columns, Table|string $referencedTable, string|array $referencedColumns = ['id'], array $options = [], ?string $name = null): static + { + 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 static($table, $fk); + } + + /** + * Returns the foreign key to be added + * + * @return \Migrations\Db\Table\ForeignKey + */ + public function getForeignKey(): ForeignKey + { + return $this->foreignKey; + } +} diff --git a/src/Db/Action/AddIndex.php b/src/Db/Action/AddIndex.php new file mode 100644 index 00000000..cee16c2f --- /dev/null +++ b/src/Db/Action/AddIndex.php @@ -0,0 +1,68 @@ +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 $options Additional options for the index creation + * @return static + */ + public static function build(Table $table, string|array|Index $columns, array $options = []): static + { + // create a new index object if strings or an array of strings were supplied + $index = $columns; + + if (!$columns instanceof Index) { + $index = new Index(); + + $index->setColumns($columns); + $index->setOptions($options); + } + + return new static($table, $index); + } + + /** + * Returns the index to be added + * + * @return \Migrations\Db\Table\Index + */ + public function getIndex(): Index + { + return $this->index; + } +} diff --git a/src/Db/Action/ChangeColumn.php b/src/Db/Action/ChangeColumn.php new file mode 100644 index 00000000..559a376c --- /dev/null +++ b/src/Db/Action/ChangeColumn.php @@ -0,0 +1,89 @@ +columnName = $columnName; + $this->column = $column; + + // if the name was omitted use the existing column name + if ($column->getName() === null || strlen($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 $options Additional options for the column + * @return static + */ + public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static + { + $column = new Column(); + $column->setName($columnName); + $column->setType($type); + $column->setOptions($options); // map options to column methods + + return new static($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; + } +} diff --git a/src/Db/Action/ChangeComment.php b/src/Db/Action/ChangeComment.php new file mode 100644 index 00000000..b483fa3c --- /dev/null +++ b/src/Db/Action/ChangeComment.php @@ -0,0 +1,43 @@ +newComment = $newComment; + } + + /** + * Return the new comment for the table + * + * @return string|null + */ + public function getNewComment(): ?string + { + return $this->newComment; + } +} diff --git a/src/Db/Action/ChangePrimaryKey.php b/src/Db/Action/ChangePrimaryKey.php new file mode 100644 index 00000000..760f7fab --- /dev/null +++ b/src/Db/Action/ChangePrimaryKey.php @@ -0,0 +1,43 @@ +newColumns = $newColumns; + } + + /** + * Return the new columns for the primary key + * + * @return string|string[]|null + */ + public function getNewColumns(): string|array|null + { + return $this->newColumns; + } +} diff --git a/src/Db/Action/CreateTable.php b/src/Db/Action/CreateTable.php new file mode 100644 index 00000000..a7f8d2bb --- /dev/null +++ b/src/Db/Action/CreateTable.php @@ -0,0 +1,13 @@ +foreignKey = $foreignKey; + } + + /** + * Creates a new DropForeignKey object after building the ForeignKey + * definition out of the passed arguments. + * + * @param \Migrations\Db\Table\Table $table The table to delete the foreign key from + * @param string|string[] $columns The columns participating in the foreign key + * @param string|null $constraint The constraint name + * @return static + */ + public static function build(Table $table, string|array $columns, ?string $constraint = null): static + { + if (is_string($columns)) { + $columns = [$columns]; + } + + $foreignKey = new ForeignKey(); + $foreignKey->setColumns($columns); + + if ($constraint) { + $foreignKey->setConstraint($constraint); + } + + return new static($table, $foreignKey); + } + + /** + * Returns the foreign key to remove + * + * @return \Migrations\Db\Table\ForeignKey + */ + public function getForeignKey(): ForeignKey + { + return $this->foreignKey; + } +} diff --git a/src/Db/Action/DropIndex.php b/src/Db/Action/DropIndex.php new file mode 100644 index 00000000..33dfbc43 --- /dev/null +++ b/src/Db/Action/DropIndex.php @@ -0,0 +1,76 @@ +index = $index; + } + + /** + * Creates a new DropIndex object after assembling the passed + * arguments. + * + * @param \Migrations\Db\Table\Table $table The table where the index is + * @param string[] $columns the indexed columns + * @return static + */ + public static function build(Table $table, array $columns = []): static + { + $index = new Index(); + $index->setColumns($columns); + + return new static($table, $index); + } + + /** + * Creates a new DropIndex when the name of the index to drop + * is known. + * + * @param \Migrations\Db\Table\Table $table The table where the index is + * @param string $name The name of the index + * @return static + */ + public static function buildFromName(Table $table, string $name): static + { + $index = new Index(); + $index->setName($name); + + return new static($table, $index); + } + + /** + * Returns the index to be dropped + * + * @return \Migrations\Db\Table\Index + */ + public function getIndex(): Index + { + return $this->index; + } +} diff --git a/src/Db/Action/DropTable.php b/src/Db/Action/DropTable.php new file mode 100644 index 00000000..37ce58fa --- /dev/null +++ b/src/Db/Action/DropTable.php @@ -0,0 +1,13 @@ +column = $column; + } + + /** + * Creates a new RemoveColumn object after assembling the + * passed arguments. + * + * @param \Migrations\Db\Table\Table $table The table where the column is + * @param string $columnName The name of the column to drop + * @return static + */ + public static function build(Table $table, string $columnName): static + { + $column = new Column(); + $column->setName($columnName); + + return new static($table, $column); + } + + /** + * Returns the column to be dropped + * + * @return \Migrations\Db\Table\Column + */ + public function getColumn(): Column + { + return $this->column; + } +} diff --git a/src/Db/Action/RenameColumn.php b/src/Db/Action/RenameColumn.php new file mode 100644 index 00000000..3ef88bf3 --- /dev/null +++ b/src/Db/Action/RenameColumn.php @@ -0,0 +1,80 @@ +newName = $newName; + $this->column = $column; + } + + /** + * Creates a new RenameColumn object after building the passed + * arguments + * + * @param \Migrations\Db\Table\Table $table The table where the column is + * @param string $columnName The name of the column to be changed + * @param string $newName The new name for the column + * @return static + */ + public static function build(Table $table, string $columnName, string $newName): static + { + $column = new Column(); + $column->setName($columnName); + + return new static($table, $column, $newName); + } + + /** + * Returns the column to be changed + * + * @return \Migrations\Db\Table\Column + */ + public function getColumn(): Column + { + return $this->column; + } + + /** + * Returns the new name for the column + * + * @return string + */ + public function getNewName(): string + { + return $this->newName; + } +} diff --git a/src/Db/Action/RenameTable.php b/src/Db/Action/RenameTable.php new file mode 100644 index 00000000..9808c311 --- /dev/null +++ b/src/Db/Action/RenameTable.php @@ -0,0 +1,43 @@ +newName = $newName; + } + + /** + * Return the new name for the table + * + * @return string + */ + public function getNewName(): string + { + return $this->newName; + } +} diff --git a/src/Db/Literal.php b/src/Db/Literal.php new file mode 100644 index 00000000..45e9bb4f --- /dev/null +++ b/src/Db/Literal.php @@ -0,0 +1,43 @@ +value = $value; + } + + /** + * @return string Returns the literal's value + */ + public function __toString(): string + { + return $this->value; + } + + /** + * @param string $value The literal's value + * @return self + */ + public static function from(string $value): Literal + { + return new self($value); + } +} diff --git a/src/Db/Table/Column.php b/src/Db/Table/Column.php index 187f0b2f..94f20ace 100644 --- a/src/Db/Table/Column.php +++ b/src/Db/Table/Column.php @@ -8,10 +8,10 @@ namespace Migrations\Db\Table; +use Migrations\Db\Literal; use Phinx\Config\FeatureFlags; use Phinx\Db\Adapter\AdapterInterface; use Phinx\Db\Adapter\PostgresAdapter; -use Phinx\Util\Literal; use RuntimeException; /** @@ -65,7 +65,7 @@ class Column protected string $name; /** - * @var string|\Phinx\Util\Literal + * @var string|\Migrations\Db\Literal */ protected string|Literal $type; @@ -195,7 +195,7 @@ public function getName(): ?string /** * Sets the column type. * - * @param string|\Phinx\Util\Literal $type Column type + * @param string|\Migrations\Db\Literal $type Column type * @return $this */ public function setType(string|Literal $type) @@ -208,7 +208,7 @@ public function setType(string|Literal $type) /** * Gets the column type. * - * @return string|\Phinx\Util\Literal + * @return string|\Migrations\Db\Literal */ public function getType(): string|Literal { diff --git a/tests/TestCase/Db/LiteralTest.php b/tests/TestCase/Db/LiteralTest.php new file mode 100644 index 00000000..e8c55fdb --- /dev/null +++ b/tests/TestCase/Db/LiteralTest.php @@ -0,0 +1,25 @@ +assertEquals($str, (string)$instance); + } + + public function testFrom() + { + $str = 'test1'; + $instance = Literal::from($str); + $this->assertInstanceOf('\Migrations\Db\Literal', $instance); + $this->assertEquals($str, (string)$instance); + } +} From 804e6bc5942a3d5c0f0fb088541720758e2980d0 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Wed, 6 Dec 2023 20:02:39 -0500 Subject: [PATCH 2/2] fix phpcs, stan, and psalm --- src/Db/Action/AddColumn.php | 8 ++++---- src/Db/Action/AddForeignKey.php | 6 +++--- src/Db/Action/AddIndex.php | 12 ++++++------ src/Db/Action/ChangeColumn.php | 10 +++++----- src/Db/Action/DropForeignKey.php | 6 +++--- src/Db/Action/DropIndex.php | 12 ++++++------ src/Db/Action/RemoveColumn.php | 6 +++--- src/Db/Action/RenameColumn.php | 6 +++--- 8 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Db/Action/AddColumn.php b/src/Db/Action/AddColumn.php index e9d97b6a..3572bb5a 100644 --- a/src/Db/Action/AddColumn.php +++ b/src/Db/Action/AddColumn.php @@ -8,9 +8,9 @@ namespace Migrations\Db\Action; +use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\Table; -use Migrations\Db\Literal; class AddColumn extends Action { @@ -40,16 +40,16 @@ public function __construct(Table $table, Column $column) * @param string $columnName The column name * @param string|\Migrations\Db\Literal $type The column type * @param array $options The column options - * @return static + * @return self */ - public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static + 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 static($table, $column); + return new AddColumn($table, $column); } /** diff --git a/src/Db/Action/AddForeignKey.php b/src/Db/Action/AddForeignKey.php index 21b26706..15423a1f 100644 --- a/src/Db/Action/AddForeignKey.php +++ b/src/Db/Action/AddForeignKey.php @@ -42,9 +42,9 @@ public function __construct(Table $table, ForeignKey $fk) * @param string|string[] $referencedColumns The columns in the referenced table * @param array $options Extra options for the foreign key * @param string|null $name The name of the foreign key - * @return static + * @return self */ - public static function build(Table $table, string|array $columns, Table|string $referencedTable, string|array $referencedColumns = ['id'], array $options = [], ?string $name = null): static + 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 @@ -64,7 +64,7 @@ public static function build(Table $table, string|array $columns, Table|string $ $fk->setConstraint($name); } - return new static($table, $fk); + return new AddForeignKey($table, $fk); } /** diff --git a/src/Db/Action/AddIndex.php b/src/Db/Action/AddIndex.php index cee16c2f..10215871 100644 --- a/src/Db/Action/AddIndex.php +++ b/src/Db/Action/AddIndex.php @@ -39,21 +39,21 @@ public function __construct(Table $table, Index $index) * @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 $options Additional options for the index creation - * @return static + * @return self */ - public static function build(Table $table, string|array|Index $columns, array $options = []): static + 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 - $index = $columns; - - if (!$columns instanceof Index) { + if (!($columns instanceof Index)) { $index = new Index(); $index->setColumns($columns); $index->setOptions($options); + } else { + $index = $columns; } - return new static($table, $index); + return new AddIndex($table, $index); } /** diff --git a/src/Db/Action/ChangeColumn.php b/src/Db/Action/ChangeColumn.php index 559a376c..63327890 100644 --- a/src/Db/Action/ChangeColumn.php +++ b/src/Db/Action/ChangeColumn.php @@ -8,9 +8,9 @@ namespace Migrations\Db\Action; +use Migrations\Db\Literal; use Migrations\Db\Table\Column; use Migrations\Db\Table\Table; -use Migrations\Db\Literal; class ChangeColumn extends Action { @@ -42,7 +42,7 @@ public function __construct(Table $table, string $columnName, Column $column) $this->column = $column; // if the name was omitted use the existing column name - if ($column->getName() === null || strlen($column->getName()) === 0) { + if ($column->getName() === null || strlen((string)$column->getName()) === 0) { $column->setName($columnName); } } @@ -55,16 +55,16 @@ public function __construct(Table $table, string $columnName, Column $column) * @param string $columnName The name of the column to change * @param string|\Migrations\Db\Literal $type The type of the column * @param array $options Additional options for the column - * @return static + * @return self */ - public static function build(Table $table, string $columnName, string|Literal $type, array $options = []): static + 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 static($table, $columnName, $column); + return new ChangeColumn($table, $columnName, $column); } /** diff --git a/src/Db/Action/DropForeignKey.php b/src/Db/Action/DropForeignKey.php index 28e1fc36..b003c4c5 100644 --- a/src/Db/Action/DropForeignKey.php +++ b/src/Db/Action/DropForeignKey.php @@ -39,9 +39,9 @@ public function __construct(Table $table, ForeignKey $foreignKey) * @param \Migrations\Db\Table\Table $table The table to delete the foreign key from * @param string|string[] $columns The columns participating in the foreign key * @param string|null $constraint The constraint name - * @return static + * @return self */ - public static function build(Table $table, string|array $columns, ?string $constraint = null): static + public static function build(Table $table, string|array $columns, ?string $constraint = null): self { if (is_string($columns)) { $columns = [$columns]; @@ -54,7 +54,7 @@ public static function build(Table $table, string|array $columns, ?string $const $foreignKey->setConstraint($constraint); } - return new static($table, $foreignKey); + return new DropForeignKey($table, $foreignKey); } /** diff --git a/src/Db/Action/DropIndex.php b/src/Db/Action/DropIndex.php index 33dfbc43..eef579aa 100644 --- a/src/Db/Action/DropIndex.php +++ b/src/Db/Action/DropIndex.php @@ -38,14 +38,14 @@ public function __construct(Table $table, Index $index) * * @param \Migrations\Db\Table\Table $table The table where the index is * @param string[] $columns the indexed columns - * @return static + * @return self */ - public static function build(Table $table, array $columns = []): static + public static function build(Table $table, array $columns = []): self { $index = new Index(); $index->setColumns($columns); - return new static($table, $index); + return new DropIndex($table, $index); } /** @@ -54,14 +54,14 @@ public static function build(Table $table, array $columns = []): static * * @param \Migrations\Db\Table\Table $table The table where the index is * @param string $name The name of the index - * @return static + * @return self */ - public static function buildFromName(Table $table, string $name): static + public static function buildFromName(Table $table, string $name): self { $index = new Index(); $index->setName($name); - return new static($table, $index); + return new DropIndex($table, $index); } /** diff --git a/src/Db/Action/RemoveColumn.php b/src/Db/Action/RemoveColumn.php index 9a79c5b1..30307570 100644 --- a/src/Db/Action/RemoveColumn.php +++ b/src/Db/Action/RemoveColumn.php @@ -38,14 +38,14 @@ public function __construct(Table $table, Column $column) * * @param \Migrations\Db\Table\Table $table The table where the column is * @param string $columnName The name of the column to drop - * @return static + * @return self */ - public static function build(Table $table, string $columnName): static + public static function build(Table $table, string $columnName): self { $column = new Column(); $column->setName($columnName); - return new static($table, $column); + return new RemoveColumn($table, $column); } /** diff --git a/src/Db/Action/RenameColumn.php b/src/Db/Action/RenameColumn.php index 3ef88bf3..c2b34274 100644 --- a/src/Db/Action/RenameColumn.php +++ b/src/Db/Action/RenameColumn.php @@ -48,14 +48,14 @@ public function __construct(Table $table, Column $column, string $newName) * @param \Migrations\Db\Table\Table $table The table where the column is * @param string $columnName The name of the column to be changed * @param string $newName The new name for the column - * @return static + * @return self */ - public static function build(Table $table, string $columnName, string $newName): static + public static function build(Table $table, string $columnName, string $newName): self { $column = new Column(); $column->setName($columnName); - return new static($table, $column, $newName); + return new RenameColumn($table, $column, $newName); } /**