From 425603d57e84036dafca4d1e6ade68d305716422 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 7 Jan 2025 10:32:18 -0500 Subject: [PATCH 1/4] Move getSchemaDialect up to PdoAdapter. --- src/Db/Adapter/MysqlAdapter.php | 13 ------------- src/Db/Adapter/PdoAdapter.php | 13 +++++++++++++ src/Db/Adapter/PostgresAdapter.php | 13 ------------- src/Db/Adapter/SqliteAdapter.php | 13 ------------- src/Db/Adapter/SqlserverAdapter.php | 13 ------------- 5 files changed, 13 insertions(+), 52 deletions(-) diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index 29bbea08..ea135532 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -11,7 +11,6 @@ use Cake\Core\Configure; use Cake\Database\Connection; use Cake\Database\Exception\QueryException; -use Cake\Database\Schema\SchemaDialect; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Literal; @@ -174,18 +173,6 @@ public function quoteColumnName(string $columnName): string return $driver->quoteIdentifier($columnName); } - /** - * Get the schema dialect for this adapter. - * - * @return \Cake\Database\Schema\SchemaDialect - */ - protected function getSchemaDialect(): SchemaDialect - { - $driver = $this->getConnection()->getDriver(); - - return $driver->schemaDialect(); - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/PdoAdapter.php b/src/Db/Adapter/PdoAdapter.php index e5d0c068..5db7ef43 100644 --- a/src/Db/Adapter/PdoAdapter.php +++ b/src/Db/Adapter/PdoAdapter.php @@ -16,6 +16,7 @@ use Cake\Database\Query\InsertQuery; use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; +use Cake\Database\Schema\SchemaDialect; use InvalidArgumentException; use Migrations\Config\Config; use Migrations\Db\Action\AddColumn; @@ -55,6 +56,18 @@ abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterfa */ protected ?Connection $connection = null; + /** + * Get the schema dialect for this adapter. + * + * @return \Cake\Database\Schema\SchemaDialect + */ + protected function getSchemaDialect(): SchemaDialect + { + $driver = $this->getConnection()->getDriver(); + + return $driver->schemaDialect(); + } + /** * Writes a message to stdout if verbose output is on * diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index 99a26d73..ab2a6b24 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -9,7 +9,6 @@ namespace Migrations\Db\Adapter; use Cake\Database\Connection; -use Cake\Database\Schema\SchemaDialect; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Literal; @@ -124,18 +123,6 @@ public function rollbackTransaction(): void $this->getConnection()->rollback(); } - /** - * Get the schema dialect for this adapter. - * - * @return \Cake\Database\Schema\SchemaDialect - */ - protected function getSchemaDialect(): SchemaDialect - { - $driver = $this->getConnection()->getDriver(); - - return $driver->schemaDialect(); - } - /** * Quotes a schema name for use in a query. * diff --git a/src/Db/Adapter/SqliteAdapter.php b/src/Db/Adapter/SqliteAdapter.php index e5f525f2..33933770 100644 --- a/src/Db/Adapter/SqliteAdapter.php +++ b/src/Db/Adapter/SqliteAdapter.php @@ -9,7 +9,6 @@ namespace Migrations\Db\Adapter; use BadMethodCallException; -use Cake\Database\Schema\SchemaDialect; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Expression; @@ -226,18 +225,6 @@ public function quoteColumnName($columnName): string return $driver->quoteIdentifier($columnName); } - /** - * Get the schema dialect for this adapter. - * - * @return \Cake\Database\Schema\SchemaDialect - */ - protected function getSchemaDialect(): SchemaDialect - { - $driver = $this->getConnection()->getDriver(); - - return $driver->schemaDialect(); - } - /** * Generates a regular expression to match identifiers that may or * may not be quoted with any of the supported quotes. diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index e28fbb9b..5b914ec7 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -9,7 +9,6 @@ namespace Migrations\Db\Adapter; use BadMethodCallException; -use Cake\Database\Schema\SchemaDialect; use InvalidArgumentException; use Migrations\Db\AlterInstructions; use Migrations\Db\Literal; @@ -100,18 +99,6 @@ public function rollbackTransaction(): void $this->getConnection()->rollback(); } - /** - * Get the schema dialect for this adapter. - * - * @return \Cake\Database\Schema\SchemaDialect - */ - protected function getSchemaDialect(): SchemaDialect - { - $driver = $this->getConnection()->getDriver(); - - return $driver->schemaDialect(); - } - /** * Quotes a schema name for use in a query. * From 1ac86784345230ae3bfa48348477ce25f2323724 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 7 Jan 2025 10:48:23 -0500 Subject: [PATCH 2/4] Move more methods up to PdoAdapter --- src/Db/Adapter/MysqlAdapter.php | 63 ------------------------- src/Db/Adapter/PdoAdapter.php | 73 +++++++++++++++++++++++++---- src/Db/Adapter/PostgresAdapter.php | 63 ------------------------- src/Db/Adapter/SqliteAdapter.php | 63 ------------------------- src/Db/Adapter/SqlserverAdapter.php | 62 ------------------------ 5 files changed, 63 insertions(+), 261 deletions(-) diff --git a/src/Db/Adapter/MysqlAdapter.php b/src/Db/Adapter/MysqlAdapter.php index ea135532..21807131 100644 --- a/src/Db/Adapter/MysqlAdapter.php +++ b/src/Db/Adapter/MysqlAdapter.php @@ -90,19 +90,6 @@ class MysqlAdapter extends PdoAdapter public const FIRST = 'FIRST'; - /** - * {@inheritDoc} - * - * @throws \RuntimeException - * @throws \InvalidArgumentException - * @return void - */ - public function connect(): void - { - $this->getConnection()->getDriver()->connect(); - $this->setConnection($this->getConnection()); - } - /** * @inheritDoc */ @@ -113,46 +100,6 @@ public function setConnection(Connection $connection): AdapterInterface return parent::setConnection($connection); } - /** - * @inheritDoc - */ - public function disconnect(): void - { - $this->getConnection()->getDriver()->disconnect(); - } - - /** - * @inheritDoc - */ - public function hasTransactions(): bool - { - return true; - } - - /** - * @inheritDoc - */ - public function beginTransaction(): void - { - $this->getConnection()->begin(); - } - - /** - * @inheritDoc - */ - public function commitTransaction(): void - { - $this->getConnection()->commit(); - } - - /** - * @inheritDoc - */ - public function rollbackTransaction(): void - { - $this->getConnection()->rollback(); - } - /** * @inheritDoc */ @@ -163,16 +110,6 @@ public function quoteTableName(string $tableName): string return $driver->quoteIdentifier($tableName); } - /** - * @inheritDoc - */ - public function quoteColumnName(string $columnName): string - { - $driver = $this->getConnection()->getDriver(); - - return $driver->quoteIdentifier($columnName); - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/PdoAdapter.php b/src/Db/Adapter/PdoAdapter.php index 5db7ef43..ae960144 100644 --- a/src/Db/Adapter/PdoAdapter.php +++ b/src/Db/Adapter/PdoAdapter.php @@ -68,6 +68,69 @@ protected function getSchemaDialect(): SchemaDialect return $driver->schemaDialect(); } + /** + * {@inheritDoc} + * + * @throws \RuntimeException + * @throws \InvalidArgumentException + * @return void + */ + public function connect(): void + { + $this->getConnection()->getDriver()->connect(); + $this->setConnection($this->getConnection()); + } + + /** + * @inheritDoc + */ + public function disconnect(): void + { + $this->getConnection()->getDriver()->disconnect(); + } + + /** + * @inheritDoc + */ + public function beginTransaction(): void + { + $this->getConnection()->begin(); + } + + /** + * @inheritDoc + */ + public function commitTransaction(): void + { + $this->getConnection()->commit(); + } + + /** + * @inheritDoc + */ + public function rollbackTransaction(): void + { + $this->getConnection()->rollback(); + } + + /** + * @inheritDoc + */ + public function hasTransactions(): bool + { + return true; + } + + /** + * @inheritDoc + */ + public function quoteColumnName($columnName): string + { + $driver = $this->getConnection()->getDriver(); + + return $driver->quoteIdentifier($columnName); + } + /** * Writes a message to stdout if verbose output is on * @@ -199,16 +262,6 @@ public function getDecoratedConnection(): Connection return $this->getConnection(); } - /** - * @inheritDoc - */ - abstract public function connect(): void; - - /** - * @inheritDoc - */ - abstract public function disconnect(): void; - /** * @inheritDoc */ diff --git a/src/Db/Adapter/PostgresAdapter.php b/src/Db/Adapter/PostgresAdapter.php index ab2a6b24..22454693 100644 --- a/src/Db/Adapter/PostgresAdapter.php +++ b/src/Db/Adapter/PostgresAdapter.php @@ -70,59 +70,6 @@ public function setConnection(Connection $connection): AdapterInterface return parent::setConnection($connection); } - /** - * {@inheritDoc} - * - * @throws \RuntimeException - * @throws \InvalidArgumentException - * @return void - */ - public function connect(): void - { - $this->getConnection()->getDriver()->connect(); - $this->setConnection($this->getConnection()); - } - - /** - * @inheritDoc - */ - public function disconnect(): void - { - $this->getConnection()->getDriver()->disconnect(); - } - - /** - * @inheritDoc - */ - public function hasTransactions(): bool - { - return true; - } - - /** - * @inheritDoc - */ - public function beginTransaction(): void - { - $this->getConnection()->begin(); - } - - /** - * @inheritDoc - */ - public function commitTransaction(): void - { - $this->getConnection()->commit(); - } - - /** - * @inheritDoc - */ - public function rollbackTransaction(): void - { - $this->getConnection()->rollback(); - } - /** * Quotes a schema name for use in a query. * @@ -144,16 +91,6 @@ public function quoteTableName(string $tableName): string return $this->quoteSchemaName($parts['schema']) . '.' . $this->quoteColumnName($parts['table']); } - /** - * @inheritDoc - */ - public function quoteColumnName(string $columnName): string - { - $driver = $this->getConnection()->getDriver(); - - return $driver->quoteIdentifier($columnName); - } - /** * @inheritDoc */ diff --git a/src/Db/Adapter/SqliteAdapter.php b/src/Db/Adapter/SqliteAdapter.php index 33933770..6899d554 100644 --- a/src/Db/Adapter/SqliteAdapter.php +++ b/src/Db/Adapter/SqliteAdapter.php @@ -133,19 +133,6 @@ public function databaseVersionAtLeast(string $ver): bool return version_compare($actual, $ver, '>='); } - /** - * {@inheritDoc} - * - * @throws \RuntimeException - * @throws \InvalidArgumentException - * @return void - */ - public function connect(): void - { - $this->getConnection()->getDriver()->connect(); - $this->setConnection($this->getConnection()); - } - /** * @inheritDoc */ @@ -165,46 +152,6 @@ public function setOptions(array $options): AdapterInterface return $this; } - /** - * @inheritDoc - */ - public function disconnect(): void - { - $this->getConnection()->getDriver()->disconnect(); - } - - /** - * @inheritDoc - */ - public function hasTransactions(): bool - { - return true; - } - - /** - * @inheritDoc - */ - public function beginTransaction(): void - { - $this->getConnection()->begin(); - } - - /** - * @inheritDoc - */ - public function commitTransaction(): void - { - $this->getConnection()->commit(); - } - - /** - * @inheritDoc - */ - public function rollbackTransaction(): void - { - $this->getConnection()->rollBack(); - } - /** * @inheritDoc */ @@ -215,16 +162,6 @@ public function quoteTableName($tableName): string return $driver->quoteIdentifier($tableName); } - /** - * @inheritDoc - */ - public function quoteColumnName($columnName): string - { - $driver = $this->getConnection()->getDriver(); - - return $driver->quoteIdentifier($columnName); - } - /** * Generates a regular expression to match identifiers that may or * may not be quoted with any of the supported quotes. diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index 5b914ec7..f1e4ec28 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -47,58 +47,6 @@ class SqlserverAdapter extends PdoAdapter self::PHINX_TYPE_DECIMAL => true, ]; - /** - * {@inheritDoc} - * - * @throws \InvalidArgumentException - * @return void - */ - public function connect(): void - { - $this->getConnection()->getDriver()->connect(); - $this->setConnection($this->getConnection()); - } - - /** - * @inheritDoc - */ - public function disconnect(): void - { - $this->getConnection()->getDriver()->disconnect(); - } - - /** - * @inheritDoc - */ - public function hasTransactions(): bool - { - return true; - } - - /** - * @inheritDoc - */ - public function beginTransaction(): void - { - $this->getConnection()->begin(); - } - - /** - * @inheritDoc - */ - public function commitTransaction(): void - { - $this->getConnection()->commit(); - } - - /** - * @inheritDoc - */ - public function rollbackTransaction(): void - { - $this->getConnection()->rollback(); - } - /** * Quotes a schema name for use in a query. * @@ -120,16 +68,6 @@ public function quoteTableName(string $tableName): string return $this->quoteSchemaName($parts['schema']) . '.' . $this->quoteColumnName($parts['table']); } - /** - * @inheritDoc - */ - public function quoteColumnName(string $columnName): string - { - $driver = $this->getConnection()->getDriver(); - - return $driver->quoteIdentifier($columnName); - } - /** * @inheritDoc */ From b94a07a538bb40c2244001d6d94ab2591564e2c3 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 7 Jan 2025 11:06:50 -0500 Subject: [PATCH 3/4] Fix a few smaller issues --- src/Db/Adapter/SqliteAdapter.php | 2 +- src/Db/Adapter/SqlserverAdapter.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Db/Adapter/SqliteAdapter.php b/src/Db/Adapter/SqliteAdapter.php index 6899d554..89b20205 100644 --- a/src/Db/Adapter/SqliteAdapter.php +++ b/src/Db/Adapter/SqliteAdapter.php @@ -754,7 +754,7 @@ protected function bufferIndicesAndTriggers(AlterInstructions $instructions, str "SELECT * FROM sqlite_master WHERE - (\"type\" = 'index' OR `type` = 'trigger') + (\"type\" = 'index' OR \"type\" = 'trigger') AND tbl_name = ? AND sql IS NOT NULL ", diff --git a/src/Db/Adapter/SqlserverAdapter.php b/src/Db/Adapter/SqlserverAdapter.php index f1e4ec28..d184d6ff 100644 --- a/src/Db/Adapter/SqlserverAdapter.php +++ b/src/Db/Adapter/SqlserverAdapter.php @@ -136,7 +136,7 @@ public function createTable(Table $table, array $columns = [], array $indexes = if (is_string($primaryKey)) { // handle primary_key => 'id' $pkSql .= $this->quoteColumnName($primaryKey); } elseif (is_array($primaryKey)) { // handle primary_key => array('tag_id', 'resource_id') - $pkSql .= implode(',', array_map([$this, 'quoteColumnName'], $primaryKey)); + $pkSql .= implode(',', array_map($this->quoteColumnName(...), $primaryKey)); } $pkSql .= ')'; $sqlBuffer[] = $pkSql; @@ -190,7 +190,7 @@ protected function getChangePrimaryKeyInstructions(Table $table, $newColumns): A if (is_string($newColumns)) { // handle primary_key => 'id' $sql .= $this->quoteColumnName($newColumns); } elseif (is_array($newColumns)) { // handle primary_key => array('tag_id', 'resource_id') - $sql .= implode(',', array_map([$this, 'quoteColumnName'], $newColumns)); + $sql .= implode(',', array_map($this->quoteColumnName(...), $newColumns)); } $sql .= ')'; $instructions->addPostStep($sql); From e38ab6ced42f0b2804cce0c4fba034ffcb551fe5 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 7 Jan 2025 11:39:29 -0500 Subject: [PATCH 4/4] Fix type compatibility issue --- src/Db/Adapter/PdoAdapter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Db/Adapter/PdoAdapter.php b/src/Db/Adapter/PdoAdapter.php index ae960144..227c1ce2 100644 --- a/src/Db/Adapter/PdoAdapter.php +++ b/src/Db/Adapter/PdoAdapter.php @@ -124,7 +124,7 @@ public function hasTransactions(): bool /** * @inheritDoc */ - public function quoteColumnName($columnName): string + public function quoteColumnName(string $columnName): string { $driver = $this->getConnection()->getDriver();