From 673c5a9b19734b7f63b5f44c3b4942549d5809ed Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 6 Jun 2015 00:36:44 +0200 Subject: [PATCH 1/3] Using our own adapter to avoid issues with transactions. Also aliasing the 'default' connection so that seeding databases can work more seamlesly. Fixes #83 --- src/CakeAdapter.php | 574 +++++++++++++++++++++++++++++++++++++ src/ConfigurationTrait.php | 9 +- 2 files changed, 580 insertions(+), 3 deletions(-) create mode 100644 src/CakeAdapter.php diff --git a/src/CakeAdapter.php b/src/CakeAdapter.php new file mode 100644 index 00000000..bb444462 --- /dev/null +++ b/src/CakeAdapter.php @@ -0,0 +1,574 @@ +_adapter = $adapter; + $this->_connection = $connection; + $pdo = $adapter->getConnection(); + $connection->driver()->connection($pdo); + } + + /** + * Get all migrated version numbers. + * + * @return array + */ + public function getVersions() + { + return $this->_adapter->getVersions(); + } + + /** + * Set adapter configuration options. + * + * @param array $options + * @return AdapterInterface + */ + public function setOptions(array $options) + { + return $this->_adapter->setOptions($options); + } + + /** + * Get all adapter options. + * + * @return array + */ + public function getOptions() + { + return $this->_adapter->getOptions(); + } + + /** + * Check if an option has been set. + * + * @param string $name + * @return boolean + */ + public function hasOption($name) + { + return $this->_adapter->hasOption($name); + } + + /** + * Get a single adapter option, or null if the option does not exist. + * + * @param string $name + * @return mixed + */ + public function getOption($name) + { + return $this->_adapter->getOption($name); + } + + /** + * Sets the console output. + * + * @param OutputInterface $output Output + * @return AdapterInterface + */ + public function setOutput(OutputInterface $output) + { + return $this->_adapter->setOutput($output); + } + + /** + * Gets the console output. + * + * @return OutputInterface + */ + public function getOutput() + { + return $this->_adapter->getOutput(); + } + + /** + * Records a migration being run. + * + * @param MigrationInterface $migration Migration + * @param string $direction Direction + * @param int $startTime Start Time + * @param int $endTime End Time + * @return AdapterInterface + */ + public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) + { + return $this->_adapter->migrated($migration, $direction, $startTime, $endTime); + } + + /** + * Does the schema table exist? + * + * @deprecated use hasTable instead. + * @return boolean + */ + public function hasSchemaTable() + { + return $this->_adapter->hasSchemaTable(); + } + + /** + * Creates the schema table. + * + * @return void + */ + public function createSchemaTable() + { + return $this->_adapter->createSchemaTable(); + } + + /** + * Returns the adapter type. + * + * @return string + */ + public function getAdapterType() + { + return $this->_adapter->getAdapterType(); + } + + /** + * Initializes the database connection. + * + * @throws \RuntimeException When the requested database driver is not installed. + * @return void + */ + public function connect() + { + return $this->_adapter->connect(); + } + + /** + * Closes the database connection. + * + * @return void + */ + public function disconnect() + { + return $this->_adapter->disconnect(); + } + + /** + * Does the adapter support transactions? + * + * @return boolean + */ + public function hasTransactions() + { + return $this->_adapter->hasTransactions(); + } + + /** + * Begin a transaction. + * + * @return void + */ + public function beginTransaction() + { + $this->_connection->begin(); + } + + /** + * Commit a transaction. + * + * @return void + */ + public function commitTransaction() + { + $this->_connection->commit(); + } + + /** + * Rollback a transaction. + * + * @return void + */ + public function rollbackTransaction() + { + $this->_connection->rollback(); + } + + /** + * Executes a SQL statement and returns the number of affected rows. + * + * @param string $sql SQL + * @return int + */ + public function execute($sql) + { + return $this->_adapter->execute($sql); + } + + /** + * Executes a SQL statement and returns the result as an array. + * + * @param string $sql SQL + * @return array + */ + public function query($sql) + { + return $this->_adapter->query($sql); + } + + /** + * Executes a query and returns only one row as an array. + * + * @param string $sql SQL + * @return array + */ + public function fetchRow($sql) + { + return $this->_adapter->fetchRow($sql); + } + + /** + * Executes a query and returns an array of rows. + * + * @param string $sql SQL + * @return array + */ + public function fetchAll($sql) + { + return $this->_adapter->fetchAll($sql); + } + + /** + * Quotes a table name for use in a query. + * + * @param string $tableName Table Name + * @return string + */ + public function quoteTableName($tableName) + { + return $this->_adapter->quoteTableName($tableName); + } + + /** + * Quotes a column name for use in a query. + * + * @param string $columnName Table Name + * @return string + */ + public function quoteColumnName($columnName) + { + return $this->_adapter->quoteColumnName($columnName); + } + + /** + * Checks to see if a table exists. + * + * @param string $tableName Table Name + * @return boolean + */ + public function hasTable($tableName) + { + return $this->_adapter->hasTable($tableName); + } + + /** + * Creates the specified database table. + * + * @param Table $table Table + * @return void + */ + public function createTable(Table $table) + { + return $this->_adapter->createTable($table); + } + + /** + * Renames the specified database table. + * + * @param string $tableName Table Name + * @param string $newName New Name + * @return void + */ + public function renameTable($tableName, $newName) + { + return $this->_adapter->renameTable($tableName, $newName); + } + + /** + * Drops the specified database table. + * + * @param string $tableName Table Name + * @return void + */ + public function dropTable($tableName) + { + return $this->_adapter->dropTable($tableName); + } + + /** + * Returns table columns + * + * @param string $tableName Table Name + * @return Column[] + */ + public function getColumns($tableName) + { + return $this->_adapter->getColumns($tableName); + } + + /** + * Checks to see if a column exists. + * + * @param string $tableName Table Name + * @param string $columnName Column Name + * @return boolean + */ + public function hasColumn($tableName, $columnName) + { + return $this->_adapter->hasColumn($tableName, $columnName); + } + + /** + * Adds the specified column to a database table. + * + * @param Table $table Table + * @param Column $column Column + * @return void + */ + public function addColumn(Table $table, Column $column) + { + return $this->_adapter->addColumn($table, $column); + } + + /** + * Renames the specified column. + * + * @param string $tableName Table Name + * @param string $columnName Column Name + * @param string $newColumnName New Column Name + * @return void + */ + public function renameColumn($tableName, $columnName, $newColumnName) + { + return $this->_adapter->renameColumn($tableName, $columnName, $newColumnName); + } + + /** + * Change a table column type. + * + * @param string $tableName Table Name + * @param string $columnName Column Name + * @param Column $newColumn New Column + * @return Table + */ + public function changeColumn($tableName, $columnName, Column $newColumn) + { + return $this->_adapter->changeColumn($tableName, $columnName, $newColumn); + } + + /** + * Drops the specified column. + * + * @param string $tableName Table Name + * @param string $columnName Column Name + * @return void + */ + public function dropColumn($tableName, $columnName) + { + return $this->_adapter->dropColumn($tableName, $columnName); + } + + /** + * Checks to see if an index exists. + * + * @param string $tableName Table Name + * @param mixed $columns Column(s) + * @return boolean + */ + public function hasIndex($tableName, $columns) + { + return $this->_adapter->hasIndex($tableName, $columns); + } + + /** + * Adds the specified index to a database table. + * + * @param Table $table Table + * @param Index $index Index + * @return void + */ + public function addIndex(Table $table, Index $index) + { + return $this->_adapter->addIndex($table, $index); + } + + /** + * Drops the specified index from a database table. + * + * @param string $tableName + * @param mixed $columns Column(s) + * @return void + */ + public function dropIndex($tableName, $columns) + { + return $this->_adapter->dropIndex($tableName, $columns); + } + + /** + * Drops the index specified by name from a database table. + * + * @param string $tableName + * @param string $indexName + * @return void + */ + public function dropIndexByName($tableName, $indexName) + { + return $this->_adapter->dropIndexByName($tableName, $indexName); + } + + /** + * Checks to see if a foreign key exists. + * + * @param string $tableName + * @param string[] $columns Column(s) + * @param string $constraint Constraint name + * @return boolean + */ + public function hasForeignKey($tableName, $columns, $constraint = null) + { + return $this->_adapter->hasForeignKey($tableName, $columns, $constraint); + } + + /** + * Adds the specified foreign key to a database table. + * + * @param Table $table + * @param ForeignKey $foreignKey + * @return void + */ + public function addForeignKey(Table $table, ForeignKey $foreignKey) + { + return $this->_adapter->addForeignKey($table, $foreignKey); + } + + /** + * Drops the specified foreign key from a database table. + * + * @param string $tableName + * @param string[] $columns Column(s) + * @param string $constraint Constraint name + * @return void + */ + public function dropForeignKey($tableName, $columns, $constraint = null) + { + return $this->_adapter->dropForeignKey($tableName, $columns, $constraint); + } + + /** + * Returns an array of the supported Phinx column types. + * + * @return array + */ + public function getColumnTypes() + { + return $this->_adapter->getColumnTypes(); + } + + /** + * Checks that the given column is of a supported type. + * + * @param Column $column + * @return boolean + */ + public function isValidColumnType(Column $column) + { + return $this->_adapter->isValidColumnType($column); + } + + /** + * Converts the Phinx logical type to the adapter's SQL type. + * + * @param string $type + * @param integer $limit + * @return string + */ + public function getSqlType($type, $limit = null) + { + return $this->_adapter->getSqlType($type, $limit); + } + + /** + * Creates a new database. + * + * @param string $name Database Name + * @param array $options Options + * @return void + */ + public function createDatabase($name, $options = array()) + { + return $this->_adapter->createDatabase($name, $options); + } + + /** + * Checks to see if a database exists. + * + * @param string $name Database Name + * @return boolean + */ + public function hasDatabase($name) + { + return $this->_adapter->hasDatabase($name); + } + + /** + * Drops the specified database. + * + * @param string $name Database Name + * @return void + */ + public function dropDatabase($name) + { + return $this->_adapter->dropDatabase($name); + } + +} diff --git a/src/ConfigurationTrait.php b/src/ConfigurationTrait.php index d1ebc77f..2801ac11 100644 --- a/src/ConfigurationTrait.php +++ b/src/ConfigurationTrait.php @@ -166,9 +166,12 @@ public function setInput(InputInterface $input) public function bootstrap(InputInterface $input, OutputInterface $output) { parent::bootstrap($input, $output); - $connection = ConnectionManager::get($this->getConnectionName($input)); - $pdo = $this->getManager()->getEnvironment('default')->getAdapter()->getConnection(); - $connection->driver()->connection($pdo); + $name = $this->getConnectionName($input); + ConnectionManager::alias($name, 'default'); + $connection = ConnectionManager::get($name); + + $env = $this->getManager()->getEnvironment('default'); + $env->setAdapter(new CakeAdapter($env->getAdapter(), $connection)); } /** From 12f5c8e9943493c5158a0ded7ebb9a3f4b8712b5 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 6 Jun 2015 00:55:05 +0200 Subject: [PATCH 2/3] Making tests pass again --- composer.json | 2 +- src/ConfigurationTrait.php | 5 ++++- tests/bootstrap.php | 4 ++-- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 632e6ff4..f5c6ef33 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "require-dev": { "phpunit/phpunit": "*", - "cakephp/bake": "dev-master" + "cakephp/bake": "@stable" }, "autoload": { "psr-4": { diff --git a/src/ConfigurationTrait.php b/src/ConfigurationTrait.php index 2801ac11..2d10adfb 100644 --- a/src/ConfigurationTrait.php +++ b/src/ConfigurationTrait.php @@ -171,7 +171,10 @@ public function bootstrap(InputInterface $input, OutputInterface $output) $connection = ConnectionManager::get($name); $env = $this->getManager()->getEnvironment('default'); - $env->setAdapter(new CakeAdapter($env->getAdapter(), $connection)); + $adapter = $env->getAdapter(); + if (!$adapter instanceof CakeAdapter) { + $env->setAdapter(new CakeAdapter($adapter, $connection)); + } } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 4d4f8091..e001f047 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -49,13 +49,13 @@ 'engine' => 'File', 'prefix' => 'cake_core_', 'serialize' => true, - 'path' => '/tmp', + 'path' => TMP, ], '_cake_model_' => [ 'engine' => 'File', 'prefix' => 'cake_model_', 'serialize' => true, - 'path' => '/tmp', + 'path' => TMP, ] ]); From b201d6c78a2afd4386e384b200a6c96a2b2d3783 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Sat, 6 Jun 2015 12:49:54 +0200 Subject: [PATCH 3/3] Fixed CS errors --- src/CakeAdapter.php | 104 ++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 52 deletions(-) diff --git a/src/CakeAdapter.php b/src/CakeAdapter.php index bb444462..65ae930d 100644 --- a/src/CakeAdapter.php +++ b/src/CakeAdapter.php @@ -24,21 +24,22 @@ * Decorates an AdapterInterface in order to proxy some method to the actual * connection object. */ -class CakeAdapter implements AdapterInterface { +class CakeAdapter implements AdapterInterface +{ /** * Decorated adapter * * @var \Phinx\Db\Adapter\AdapterInterface */ - protected $_adapter; + protected $adapter; /** * Database connection * * @var \Cake\Database\Connection */ - protected $_connection; + protected $connection; /** * Constructor @@ -48,8 +49,8 @@ class CakeAdapter implements AdapterInterface { */ public function __construct(AdapterInterface $adapter, Connection $connection) { - $this->_adapter = $adapter; - $this->_connection = $connection; + $this->adapter = $adapter; + $this->connection = $connection; $pdo = $adapter->getConnection(); $connection->driver()->connection($pdo); } @@ -61,7 +62,7 @@ public function __construct(AdapterInterface $adapter, Connection $connection) */ public function getVersions() { - return $this->_adapter->getVersions(); + return $this->adapter->getVersions(); } /** @@ -72,7 +73,7 @@ public function getVersions() */ public function setOptions(array $options) { - return $this->_adapter->setOptions($options); + return $this->adapter->setOptions($options); } /** @@ -82,7 +83,7 @@ public function setOptions(array $options) */ public function getOptions() { - return $this->_adapter->getOptions(); + return $this->adapter->getOptions(); } /** @@ -93,7 +94,7 @@ public function getOptions() */ public function hasOption($name) { - return $this->_adapter->hasOption($name); + return $this->adapter->hasOption($name); } /** @@ -104,7 +105,7 @@ public function hasOption($name) */ public function getOption($name) { - return $this->_adapter->getOption($name); + return $this->adapter->getOption($name); } /** @@ -115,7 +116,7 @@ public function getOption($name) */ public function setOutput(OutputInterface $output) { - return $this->_adapter->setOutput($output); + return $this->adapter->setOutput($output); } /** @@ -125,7 +126,7 @@ public function setOutput(OutputInterface $output) */ public function getOutput() { - return $this->_adapter->getOutput(); + return $this->adapter->getOutput(); } /** @@ -139,7 +140,7 @@ public function getOutput() */ public function migrated(MigrationInterface $migration, $direction, $startTime, $endTime) { - return $this->_adapter->migrated($migration, $direction, $startTime, $endTime); + return $this->adapter->migrated($migration, $direction, $startTime, $endTime); } /** @@ -150,7 +151,7 @@ public function migrated(MigrationInterface $migration, $direction, $startTime, */ public function hasSchemaTable() { - return $this->_adapter->hasSchemaTable(); + return $this->adapter->hasSchemaTable(); } /** @@ -160,7 +161,7 @@ public function hasSchemaTable() */ public function createSchemaTable() { - return $this->_adapter->createSchemaTable(); + return $this->adapter->createSchemaTable(); } /** @@ -170,7 +171,7 @@ public function createSchemaTable() */ public function getAdapterType() { - return $this->_adapter->getAdapterType(); + return $this->adapter->getAdapterType(); } /** @@ -181,7 +182,7 @@ public function getAdapterType() */ public function connect() { - return $this->_adapter->connect(); + return $this->adapter->connect(); } /** @@ -191,7 +192,7 @@ public function connect() */ public function disconnect() { - return $this->_adapter->disconnect(); + return $this->adapter->disconnect(); } /** @@ -201,7 +202,7 @@ public function disconnect() */ public function hasTransactions() { - return $this->_adapter->hasTransactions(); + return $this->adapter->hasTransactions(); } /** @@ -211,7 +212,7 @@ public function hasTransactions() */ public function beginTransaction() { - $this->_connection->begin(); + $this->connection->begin(); } /** @@ -221,7 +222,7 @@ public function beginTransaction() */ public function commitTransaction() { - $this->_connection->commit(); + $this->connection->commit(); } /** @@ -231,7 +232,7 @@ public function commitTransaction() */ public function rollbackTransaction() { - $this->_connection->rollback(); + $this->connection->rollback(); } /** @@ -242,7 +243,7 @@ public function rollbackTransaction() */ public function execute($sql) { - return $this->_adapter->execute($sql); + return $this->adapter->execute($sql); } /** @@ -253,7 +254,7 @@ public function execute($sql) */ public function query($sql) { - return $this->_adapter->query($sql); + return $this->adapter->query($sql); } /** @@ -264,7 +265,7 @@ public function query($sql) */ public function fetchRow($sql) { - return $this->_adapter->fetchRow($sql); + return $this->adapter->fetchRow($sql); } /** @@ -275,7 +276,7 @@ public function fetchRow($sql) */ public function fetchAll($sql) { - return $this->_adapter->fetchAll($sql); + return $this->adapter->fetchAll($sql); } /** @@ -286,7 +287,7 @@ public function fetchAll($sql) */ public function quoteTableName($tableName) { - return $this->_adapter->quoteTableName($tableName); + return $this->adapter->quoteTableName($tableName); } /** @@ -297,7 +298,7 @@ public function quoteTableName($tableName) */ public function quoteColumnName($columnName) { - return $this->_adapter->quoteColumnName($columnName); + return $this->adapter->quoteColumnName($columnName); } /** @@ -308,7 +309,7 @@ public function quoteColumnName($columnName) */ public function hasTable($tableName) { - return $this->_adapter->hasTable($tableName); + return $this->adapter->hasTable($tableName); } /** @@ -319,7 +320,7 @@ public function hasTable($tableName) */ public function createTable(Table $table) { - return $this->_adapter->createTable($table); + return $this->adapter->createTable($table); } /** @@ -331,7 +332,7 @@ public function createTable(Table $table) */ public function renameTable($tableName, $newName) { - return $this->_adapter->renameTable($tableName, $newName); + return $this->adapter->renameTable($tableName, $newName); } /** @@ -342,7 +343,7 @@ public function renameTable($tableName, $newName) */ public function dropTable($tableName) { - return $this->_adapter->dropTable($tableName); + return $this->adapter->dropTable($tableName); } /** @@ -353,7 +354,7 @@ public function dropTable($tableName) */ public function getColumns($tableName) { - return $this->_adapter->getColumns($tableName); + return $this->adapter->getColumns($tableName); } /** @@ -365,7 +366,7 @@ public function getColumns($tableName) */ public function hasColumn($tableName, $columnName) { - return $this->_adapter->hasColumn($tableName, $columnName); + return $this->adapter->hasColumn($tableName, $columnName); } /** @@ -377,7 +378,7 @@ public function hasColumn($tableName, $columnName) */ public function addColumn(Table $table, Column $column) { - return $this->_adapter->addColumn($table, $column); + return $this->adapter->addColumn($table, $column); } /** @@ -390,7 +391,7 @@ public function addColumn(Table $table, Column $column) */ public function renameColumn($tableName, $columnName, $newColumnName) { - return $this->_adapter->renameColumn($tableName, $columnName, $newColumnName); + return $this->adapter->renameColumn($tableName, $columnName, $newColumnName); } /** @@ -403,7 +404,7 @@ public function renameColumn($tableName, $columnName, $newColumnName) */ public function changeColumn($tableName, $columnName, Column $newColumn) { - return $this->_adapter->changeColumn($tableName, $columnName, $newColumn); + return $this->adapter->changeColumn($tableName, $columnName, $newColumn); } /** @@ -415,7 +416,7 @@ public function changeColumn($tableName, $columnName, Column $newColumn) */ public function dropColumn($tableName, $columnName) { - return $this->_adapter->dropColumn($tableName, $columnName); + return $this->adapter->dropColumn($tableName, $columnName); } /** @@ -427,7 +428,7 @@ public function dropColumn($tableName, $columnName) */ public function hasIndex($tableName, $columns) { - return $this->_adapter->hasIndex($tableName, $columns); + return $this->adapter->hasIndex($tableName, $columns); } /** @@ -439,7 +440,7 @@ public function hasIndex($tableName, $columns) */ public function addIndex(Table $table, Index $index) { - return $this->_adapter->addIndex($table, $index); + return $this->adapter->addIndex($table, $index); } /** @@ -451,7 +452,7 @@ public function addIndex(Table $table, Index $index) */ public function dropIndex($tableName, $columns) { - return $this->_adapter->dropIndex($tableName, $columns); + return $this->adapter->dropIndex($tableName, $columns); } /** @@ -463,7 +464,7 @@ public function dropIndex($tableName, $columns) */ public function dropIndexByName($tableName, $indexName) { - return $this->_adapter->dropIndexByName($tableName, $indexName); + return $this->adapter->dropIndexByName($tableName, $indexName); } /** @@ -476,7 +477,7 @@ public function dropIndexByName($tableName, $indexName) */ public function hasForeignKey($tableName, $columns, $constraint = null) { - return $this->_adapter->hasForeignKey($tableName, $columns, $constraint); + return $this->adapter->hasForeignKey($tableName, $columns, $constraint); } /** @@ -488,7 +489,7 @@ public function hasForeignKey($tableName, $columns, $constraint = null) */ public function addForeignKey(Table $table, ForeignKey $foreignKey) { - return $this->_adapter->addForeignKey($table, $foreignKey); + return $this->adapter->addForeignKey($table, $foreignKey); } /** @@ -501,7 +502,7 @@ public function addForeignKey(Table $table, ForeignKey $foreignKey) */ public function dropForeignKey($tableName, $columns, $constraint = null) { - return $this->_adapter->dropForeignKey($tableName, $columns, $constraint); + return $this->adapter->dropForeignKey($tableName, $columns, $constraint); } /** @@ -511,7 +512,7 @@ public function dropForeignKey($tableName, $columns, $constraint = null) */ public function getColumnTypes() { - return $this->_adapter->getColumnTypes(); + return $this->adapter->getColumnTypes(); } /** @@ -522,7 +523,7 @@ public function getColumnTypes() */ public function isValidColumnType(Column $column) { - return $this->_adapter->isValidColumnType($column); + return $this->adapter->isValidColumnType($column); } /** @@ -534,7 +535,7 @@ public function isValidColumnType(Column $column) */ public function getSqlType($type, $limit = null) { - return $this->_adapter->getSqlType($type, $limit); + return $this->adapter->getSqlType($type, $limit); } /** @@ -546,7 +547,7 @@ public function getSqlType($type, $limit = null) */ public function createDatabase($name, $options = array()) { - return $this->_adapter->createDatabase($name, $options); + return $this->adapter->createDatabase($name, $options); } /** @@ -557,7 +558,7 @@ public function createDatabase($name, $options = array()) */ public function hasDatabase($name) { - return $this->_adapter->hasDatabase($name); + return $this->adapter->hasDatabase($name); } /** @@ -568,7 +569,6 @@ public function hasDatabase($name) */ public function dropDatabase($name) { - return $this->_adapter->dropDatabase($name); + return $this->adapter->dropDatabase($name); } - }