Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for Cake 5 #2106

Merged
merged 7 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 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 @@ -112,11 +110,11 @@ jobs:
uses: codecov/codecov-action@v3

testsuite-windows:
runs-on: windows-2019
runs-on: windows-2022
name: Windows - PHP 8.1 & SQL Server

env:
EXTENSIONS: pdo_sqlsrv
EXTENSIONS: mbstring, intl, pdo_sqlsrv
PHP_VERSION: '8.1'

steps:
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: '8.0'
php-version: '8.1'
coverage: none

- name: Get composer cache directory
Expand Down
17 changes: 10 additions & 7 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",
"psr/container": "^1.0 || ^2.0",
"symfony/console": "^3.4|^4.0|^5.0|^6.0",
"php": ">=8.1",
"cakephp/database": "5.x-dev",
"psr/container": "^2.0",
"symfony/console": "^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
11 changes: 7 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
<directory>tests/</directory>
</testsuite>

<filter>
<whitelist>
<coverage>
<include>
<directory suffix=".php">src/Phinx/</directory>
</whitelist>
</filter>
</include>
<report>
<html outputDirectory="logs/report" lowUpperBound="50" highLowerBound="80"/>
</report>
</coverage>

<logging>
<log type="coverage-html" target="logs/report"
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);
}
}
10 changes: 5 additions & 5 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Phinx\Db\Adapter;

use Cake\Database\Connection;
use Cake\Database\Driver\Mysql as MysqlDriver;
use InvalidArgumentException;
use PDO;
Expand Down Expand Up @@ -1522,6 +1521,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 +1533,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);
}
}
36 changes: 34 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,36 @@ 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->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
11 changes: 5 additions & 6 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Phinx\Db\Adapter;

use Cake\Database\Connection;
use Cake\Database\Driver\Postgres as PostgresDriver;
use InvalidArgumentException;
use PDO;
Expand Down Expand Up @@ -1507,6 +1506,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 +1518,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
14 changes: 5 additions & 9 deletions src/Phinx/Db/Adapter/SQLiteAdapter.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\Sqlite as SqliteDriver;
use InvalidArgumentException;
use PDO;
Expand Down Expand Up @@ -1650,6 +1649,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 +1664,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
Loading