Skip to content

Commit

Permalink
Update for Cake 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Oct 2, 2022
1 parent 9a6ce1e commit a1355b6
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 60 deletions.
24 changes: 11 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,33 @@ on:

jobs:
testsuite:
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
php-version: ['7.2', '7.4', '8.0', '8.1']
php-version: ['8.1', '8.2']
db-type: [sqlite, mysql, pgsql]
prefer-lowest: ['']
include:
- php-version: '8.2'
db-type: mysql
- php-version: '7.2'
db-type: mysql
- php-version: '8.1'
db-type: sqlite
prefer-lowest: prefer-lowest

steps:
- name: Setup MySQL latest
if: matrix.db-type == 'mysql' && matrix.php-version != '7.2'
if: matrix.db-type == 'mysql' && matrix.php-version != '8.1'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp -p 3306:3306 -d mysql --default-authentication-plugin=mysql_native_password

- name: Setup MySQL 5.6
if: matrix.db-type == 'mysql' && matrix.php-version == '7.2'
if: matrix.db-type == 'mysql' && matrix.php-version == '8.1'
run: docker run --rm --name=mysqld -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=cakephp -p 3306:3306 -d mysql:5.6 --character-set-server=utf8

- name: Setup PostgreSQL latest
if: matrix.db-type == 'pgsql' && matrix.php-version != '7.2'
if: matrix.db-type == 'pgsql' && matrix.php-version != '8.1'
run: docker run --rm --name=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=cakephp -p 5432:5432 -d postgres

- name: Setup PostgreSQL 9.4
if: matrix.db-type == 'pgsql' && matrix.php-version == '7.2'
if: matrix.db-type == 'pgsql' && matrix.php-version == '8.1'
run: docker run --rm --name=postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=cakephp -p 5432:5432 -d postgres:9.4

- uses: actions/checkout@v3
Expand Down Expand Up @@ -74,7 +72,7 @@ jobs:
fi
- name: Setup problem matchers for PHPUnit
if: matrix.php-version == '7.2' && matrix.db-type == 'mysql'
if: matrix.php-version == '8.1' && matrix.db-type == 'mysql'
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Setup Database
Expand Down Expand Up @@ -181,15 +179,15 @@ jobs:

cs-stan:
name: Coding Standard & Static Analysis
runs-on: ubuntu-20.04
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
php-version: '8.1'
coverage: none

- name: Get composer cache directory
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,20 @@
}
],
"require": {
"php": ">=7.2",
"cakephp/database": "^4.0",
"php": ">=8.1",
"cakephp/database": "5.x-dev",
"psr/container": "^1.0 || ^2.0",
"symfony/console": "^3.4|^4.0|^5.0|^6.0",
"symfony/config": "^3.4|^4.0|^5.0|^6.0"
},
"require-dev": {
"ext-json": "*",
"ext-pdo": "*",
"phpunit/phpunit": "^8.5|^9.3",
"phpunit/phpunit": "^9.5.19",
"sebastian/comparator": ">=1.2.3",
"cakephp/cakephp": "5.x-dev",
"cakephp/cakephp-codesniffer": "^4.0",
"symfony/yaml": "^3.4|^4.0|^5.0"
"symfony/yaml": "^3.4|^4.0|^5.0|^6.0"
},
"autoload": {
"psr-4": {
Expand All @@ -73,12 +74,14 @@
"cs-check": "phpcs -np app/ src/ tests/",
"cs-fix": "phpcbf -np app/ src/ tests/",
"stan": "phpstan analyse src/",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.5.0 && mv composer.backup composer.json",
"stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:~1.8.0 && mv composer.backup composer.json",
"test": "phpunit --colors=always"
},
"bin": [
"bin/phinx"
],
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
Expand Down
2 changes: 1 addition & 1 deletion src/Phinx/Db/Adapter/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public function executeActions(Table $table, array $actions);
*
* @return \Cake\Database\Query
*/
public function getQueryBuilder();
public function getQueryBuilder(string $type);

/**
* Executes a SQL statement.
Expand Down
4 changes: 2 additions & 2 deletions src/Phinx/Db/Adapter/AdapterWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,8 +487,8 @@ public function executeActions(Table $table, array $actions)
/**
* @inheritDoc
*/
public function getQueryBuilder()
public function getQueryBuilder(string $type)
{
return $this->getAdapter()->getQueryBuilder();
return $this->getAdapter()->getQueryBuilder($type);
}
}
9 changes: 5 additions & 4 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,10 @@ public function getColumnTypes()
*/
public function getDecoratedConnection()
{
if (isset($this->decoratedConnection)) {
return $this->decoratedConnection;
}

$options = $this->getOptions();
$options = [
'username' => $options['user'] ?? null,
Expand All @@ -1530,9 +1534,6 @@ public function getDecoratedConnection()
'quoteIdentifiers' => true,
] + $options;

$driver = new MysqlDriver($options);
$driver->setConnection($this->connection);

return new Connection(['driver' => $driver] + $options);
return $this->decoratedConnection = $this->buildConnection(MysqlDriver::class, $options);
}
}
37 changes: 35 additions & 2 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace Phinx\Db\Adapter;

use BadMethodCallException;
use Cake\Database\Connection;
use Cake\Database\Query;
use InvalidArgumentException;
use PDO;
use PDOException;
Expand All @@ -32,6 +34,7 @@
use Phinx\Db\Util\AlterInstructions;
use Phinx\Migration\MigrationInterface;
use Phinx\Util\Literal;
use ReflectionProperty;
use RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;

Expand All @@ -47,6 +50,11 @@ abstract class PdoAdapter extends AbstractAdapter implements DirectActionInterfa
*/
protected $connection;

/**
* @var \Cake\Database\Connection|null
*/
protected $decoratedConnection;

/**
* Writes a message to stdout if verbose output is on
*
Expand Down Expand Up @@ -201,12 +209,37 @@ public function execute($sql)
*/
abstract public function getDecoratedConnection();

/**
* Build connection instance.
*
* @param class-string<\Cake\Database\Driver> $driverClass Driver class name.
* @param array $options Options.
* @return \Cake\Database\Connection
*/
protected function buildConnection(string $driverClass, array $options)
{
$driver = new $driverClass($options);
$prop = new ReflectionProperty($driver, 'pdo');
$prop->setAccessible(true);
$prop->setValue($driver, $this->connection);

return new Connection(['driver' => $driver] + $options);
}

/**
* @inheritDoc
*/
public function getQueryBuilder()
public function getQueryBuilder(string $type)
{
return $this->getDecoratedConnection()->newQuery();
return match ($type) {
Query::TYPE_SELECT => $this->getDecoratedConnection()->selectQuery(),
Query::TYPE_INSERT => $this->getDecoratedConnection()->insertQuery(),
Query::TYPE_UPDATE => $this->getDecoratedConnection()->updateQuery(),
Query::TYPE_DELETE => $this->getDecoratedConnection()->deleteQuery(),
default => throw new InvalidArgumentException(
'Query type must be one of: `select`, `insert`, `update`, `delete`.'
)
};
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,10 @@ public function castToBool($value)
*/
public function getDecoratedConnection()
{
if (isset($this->decoratedConnection)) {
return $this->decoratedConnection;
}

$options = $this->getOptions();
$options = [
'username' => $options['user'] ?? null,
Expand All @@ -1515,11 +1519,7 @@ public function getDecoratedConnection()
'quoteIdentifiers' => true,
] + $options;

$driver = new PostgresDriver($options);

$driver->setConnection($this->connection);

return new Connection(['driver' => $driver] + $options);
return $this->decoratedConnection = $this->buildConnection(PostgresDriver::class, $options);
}

/**
Expand Down
13 changes: 5 additions & 8 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1650,6 +1650,10 @@ protected function getForeignKeySqlDefinition(ForeignKey $foreignKey)
*/
public function getDecoratedConnection()
{
if (isset($this->decoratedConnection)) {
return $this->decoratedConnection;
}

$options = $this->getOptions();
$options['quoteIdentifiers'] = true;

Expand All @@ -1661,13 +1665,6 @@ public function getDecoratedConnection()
}
}

if ($this->connection === null) {
throw new RuntimeException('You need to connect first.');
}

$driver = new SqliteDriver($options);
$driver->setConnection($this->connection);

return new Connection(['driver' => $driver] + $options);
return $this->decoratedConnection = $this->buildConnection(SqliteDriver::class, $options);
}
}
10 changes: 5 additions & 5 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Phinx\Db\Adapter;

use BadMethodCallException;
use Cake\Database\Connection;
use Cake\Database\Driver\Sqlserver as SqlServerDriver;
use InvalidArgumentException;
use PDO;
Expand Down Expand Up @@ -1355,6 +1354,10 @@ public function migrated(MigrationInterface $migration, $direction, $startTime,
*/
public function getDecoratedConnection()
{
if (isset($this->decoratedConnection)) {
return $this->decoratedConnection;
}

$options = $this->getOptions();
$options = [
'username' => $options['user'] ?? null,
Expand All @@ -1363,9 +1366,6 @@ public function getDecoratedConnection()
'quoteIdentifiers' => true,
] + $options;

$driver = new SqlServerDriver($options);
$driver->setConnection($this->connection);

return new Connection(['driver' => $driver] + $options);
return $this->decoratedConnection = $this->buildConnection(SqlServerDriver::class, $options);
}
}
4 changes: 2 additions & 2 deletions src/Phinx/Migration/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ public function query($sql)
/**
* @inheritDoc
*/
public function getQueryBuilder()
public function getQueryBuilder(string $type)
{
return $this->getAdapter()->getQueryBuilder();
return $this->getAdapter()->getQueryBuilder($type);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Phinx/Migration/MigrationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ public function query($sql);
* the dry-run settings.
*
* @see https://api.cakephp.org/3.6/class-Cake.Database.Query.html
* @param string $type Query
* @return \Cake\Database\Query
*/
public function getQueryBuilder();
public function getQueryBuilder(string $type);

/**
* Executes a query and returns only one row as an array.
Expand Down
7 changes: 4 additions & 3 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Test\Phinx\Db\Adapter;

use Cake\Database\Query;
use PDOException;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\MysqlAdapter;
Expand Down Expand Up @@ -1980,7 +1981,7 @@ public function testQueryBuilder()
->addColumn('int_col', 'integer')
->save();

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(Query::TYPE_INSERT);
$stm = $builder
->insert(['string_col', 'int_col'])
->into('table1')
Expand All @@ -1990,7 +1991,7 @@ public function testQueryBuilder()

$this->assertEquals(2, $stm->rowCount());

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(Query::TYPE_SELECT);
$stm = $builder
->select('*')
->from('table1')
Expand All @@ -2003,7 +2004,7 @@ public function testQueryBuilder()
$stm->fetch('assoc')
);

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(query::TYPE_DELETE);
$stm = $builder
->delete('table1')
->where(['int_col <' => 2])
Expand Down
7 changes: 4 additions & 3 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Test\Phinx\Db\Adapter;

use Cake\Database\Query;
use Phinx\Db\Adapter\AbstractAdapter;
use Phinx\Db\Adapter\PostgresAdapter;
use Phinx\Db\Adapter\UnsupportedColumnTypeException;
Expand Down Expand Up @@ -2249,7 +2250,7 @@ public function testQueryBuilder()
->addColumn('int_col', 'integer')
->save();

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(Query::TYPE_INSERT);
$stm = $builder
->insert(['string_col', 'int_col'])
->into('table1')
Expand All @@ -2259,7 +2260,7 @@ public function testQueryBuilder()

$this->assertEquals(2, $stm->rowCount());

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(query::TYPE_SELECT);
$stm = $builder
->select('*')
->from('table1')
Expand All @@ -2272,7 +2273,7 @@ public function testQueryBuilder()
$stm->fetch('assoc')
);

$builder = $this->adapter->getQueryBuilder();
$builder = $this->adapter->getQueryBuilder(Query::TYPE_DELETE);
$stm = $builder
->delete('table1')
->where(['int_col <' => 2])
Expand Down
Loading

0 comments on commit a1355b6

Please sign in to comment.