Skip to content

Commit

Permalink
Merge pull request #595 from cakephp/3.next
Browse files Browse the repository at this point in the history
Fix up Migrations for Phinx 0.13 integration. (#517)
  • Loading branch information
dereuromark authored Dec 9, 2022
2 parents c08c4a0 + b5b3f70 commit 3656173
Show file tree
Hide file tree
Showing 98 changed files with 252 additions and 260 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

This is a Database Migrations system for CakePHP.

The plugin consists of a CakePHP shell wrapper for the [Phinx](https://book.cakephp.org/phinx/0/en/index.html) migrations library.
The plugin consists of a CakePHP CLI wrapper for the [Phinx](https://book.cakephp.org/phinx/0/en/index.html) migrations library.

This branch is for use with CakePHP **4.x**. See [version map](https://github.com/cakephp/migrations/wiki#version-map) for details.

Expand All @@ -30,7 +30,7 @@ bin/cake plugin load Migrations

Or you can manually add the loading statement in the **src/Application.php** file of your application:
```php
public function bootstrap()
public function bootstrap(): void
{
parent::bootstrap();
$this->addPlugin('Migrations');
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
},
"require": {
"php": ">=7.4.0",
"robmorgan/phinx": "^0.12",
"robmorgan/phinx": "^0.13.2",
"cakephp/orm": "^4.3.0",
"cakephp/cache": "^4.3.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5.0 || ^9.5.0",
"phpunit/phpunit": "^9.5.0",
"cakephp/cakephp": "^4.3.0",
"cakephp/bake": "^2.6.0",
"cakephp/cakephp-codesniffer": "^4.1"
Expand Down
2 changes: 1 addition & 1 deletion docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ method to define your own sequence of seeders execution::

class DatabaseSeed extends AbstractSeed
{
public function run()
public function run(): void
{
$this->call('AnotherSeed');
$this->call('YetAnotherSeed');
Expand Down
2 changes: 1 addition & 1 deletion docs/fr/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ votre propre séquence d'exécution de *seeders*::

class DatabaseSeed extends AbstractSeed
{
public function run()
public function run(): void
{
$this->call('AnotherSeed');
$this->call('YetAnotherSeed');
Expand Down
4 changes: 2 additions & 2 deletions docs/ja/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ Migrations
bin/cake migrations migrate
以下の ``migrations`` コマンドは、 ``rollback`` を実行するとあなたのデータベースから
テーブルが削除されます。
テーブルが削除されます。

.. code-block:: bash
Expand Down Expand Up @@ -761,7 +761,7 @@ JSON 形式の文字列として結果を出力できます。 :

class DatabaseSeed extends AbstractSeed
{
public function run()
public function run(): void
{
$this->call('AnotherSeed');
$this->call('YetAnotherSeed');
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@

class DatabaseSeed extends AbstractSeed
{
public function run()
public function run(): void
{
$this->call('AnotherSeed');
$this->call('YetAnotherSeed');
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
parameters:
level: 6
level: 7
paths:
- src/
checkMissingIterableValueType: false
checkGenericClassInNonGenericObjectType: false
bootstrapFiles:
Expand Down
53 changes: 0 additions & 53 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.x-dev@">
<file src="src/CakeManager.php">
<ArgumentTypeCoercion occurrences="1">
<code>$Migration</code>
</ArgumentTypeCoercion>
<ImplementedReturnTypeMismatch occurrences="1">
<code>array|string</code>
</ImplementedReturnTypeMismatch>
</file>
<file src="src/Command/BakeSeedCommand.php">
<RedundantCondition occurrences="1">
<code>return $line !== false;</code>
Expand All @@ -18,16 +10,6 @@
<code>setInput</code>
</PossiblyNullReference>
</file>
<file src="src/MigrationsDispatcher.php">
<ArgumentTypeCoercion occurrences="1">
<code>new $value()</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Shell/Task/SimpleMigrationTask.php">
<PossiblyNullArgument occurrences="1">
<code>$name</code>
</PossiblyNullArgument>
</file>
<file src="src/TableFinderTrait.php">
<InternalClass occurrences="1">
<code>new Filesystem()</code>
Expand All @@ -46,39 +28,4 @@
<code>truncateTables</code>
</InternalMethod>
</file>
<file src="src/Util/ColumnParser.php">
<PossiblyNullArgument occurrences="1">
<code>$fieldType</code>
</PossiblyNullArgument>
</file>
<file src="src/Util/UtilTrait.php">
<InvalidReturnType occurrences="1">
<code>string|null</code>
</InvalidReturnType>
</file>
<file src="vendor/robmorgan/phinx/src/Phinx/Console/Command/Create.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$defaultName</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="vendor/robmorgan/phinx/src/Phinx/Console/Command/Migrate.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$defaultName</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="vendor/robmorgan/phinx/src/Phinx/Console/Command/Rollback.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$defaultName</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="vendor/robmorgan/phinx/src/Phinx/Console/Command/SeedRun.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$defaultName</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="vendor/robmorgan/phinx/src/Phinx/Console/Command/Status.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$defaultName</code>
</NonInvariantDocblockPropertyType>
</file>
</files>
5 changes: 3 additions & 2 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
autoloader="tests/bootstrap.php"
>
<projectFiles>
<directory name="src"/>
<directory name="src/"/>
<ignoreFiles>
<directory name="tests/test_app" />
<directory name="tests/test_app/"/>
</ignoreFiles>
</projectFiles>

Expand All @@ -22,5 +22,6 @@
<MissingClosureReturnType errorLevel="suppress"/>
<PropertyNotSetInConstructor errorLevel="suppress"/>
<UnresolvableInclude errorLevel="suppress"/>
<UnsafeInstantiation errorLevel="suppress"/>
</issueHandlers>
</psalm>
6 changes: 4 additions & 2 deletions src/AbstractMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ class AbstractMigration extends BaseAbstractMigration
* @param array $options Options
* @return \Migrations\Table
*/
public function table($tableName, $options = [])
public function table(string $tableName, array $options = []): Table
{
if ($this->autoId === false) {
$options['id'] = false;
}

return new Table($tableName, $options, $this->getAdapter());
$table = new Table($tableName, $options, $this->getAdapter());

return $table;
}
}
5 changes: 3 additions & 2 deletions src/CakeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Cake\Database\Connection;
use Cake\Database\Driver\Postgres;
use Cake\Database\Query;
use PDO;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\AdapterWrapper;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getCakeConnection()
*
* @return \Cake\Database\Query
*/
public function getQueryBuilder()
public function getQueryBuilder(): Query
{
return $this->getCakeConnection()->newQuery();
}
Expand All @@ -87,7 +88,7 @@ public function getQueryBuilder()
*
* @return string
*/
public function getAdapterType()
public function getAdapterType(): string
{
return $this->getAdapter()->getAdapterType();
}
Expand Down
55 changes: 30 additions & 25 deletions src/CakeManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
namespace Migrations;

use DateTime;
use Phinx\Migration\Manager;
use Symfony\Component\Console\Input\InputInterface;

Expand Down Expand Up @@ -59,20 +60,21 @@ public function resetSeeds()
*
* @param string $environment Environment name.
* @param null|string $format Format (`json` or `array`).
* @return array|string Array of migrations or json string.
* @return array Array of migrations.
*/
public function printStatus($environment, $format = null)
public function printStatus(string $environment, ?string $format = null): array
{
$migrations = [];
$isJson = $format === 'json';
if (count($this->getMigrations('default'))) {
$defaultMigrations = $this->getMigrations('default');
if (count($defaultMigrations)) {
$env = $this->getEnvironment($environment);
$versions = $env->getVersionLog();
$this->maxNameLength = $versions ? max(array_map(function ($version) {
return strlen((string)$version['migration_name']);
}, $versions)) : 0;

foreach ($this->getMigrations('default') as $migration) {
foreach ($defaultMigrations as $migration) {
if (array_key_exists($migration->getVersion(), $versions)) {
$status = 'up';
unset($versions[$migration->getVersion()]);
Expand Down Expand Up @@ -111,18 +113,19 @@ public function printStatus($environment, $format = null)
ksort($migrations);
$migrations = array_values($migrations);

if ($isJson) {
$migrations = json_encode($migrations);
}

return $migrations;
}

/**
* @inheritDoc
* @param string $environment Environment
* @param \DateTime $dateTime Date to migrate to
* @param bool $fake flag that if true, we just record running the migration, but not actually do the
* migration
* @return void
*/
public function migrateToDateTime($environment, \DateTime $dateTime, $fake = false)
public function migrateToDateTime(string $environment, DateTime $dateTime, bool $fake = false): void
{
/** @var array<int> $versions */
$versions = array_keys($this->getMigrations('default'));
$dateString = $dateTime->format('Ymdhis');
$versionToMigrate = null;
Expand Down Expand Up @@ -186,12 +189,13 @@ public function rollbackToDateTime(string $environment, \DateTime $dateTime, boo
/**
* Checks if the migration with version number $version as already been mark migrated
*
* @param int|string $version Version number of the migration to check
* @param int $version Version number of the migration to check
* @return bool
*/
public function isMigrated($version)
public function isMigrated(int $version): bool
{
$adapter = $this->getEnvironment('default')->getAdapter();
/** @var array<int, mixed> $versions */
$versions = array_flip($adapter->getVersions());

return isset($versions[$version]);
Expand All @@ -200,11 +204,11 @@ public function isMigrated($version)
/**
* Marks migration with version number $version migrated
*
* @param int|string $version Version number of the migration to check
* @param int $version Version number of the migration to check
* @param string $path Path where the migration file is located
* @return bool True if success
*/
public function markMigrated($version, $path)
public function markMigrated(int $version, string $path): bool
{
$adapter = $this->getEnvironment('default')->getAdapter();

Expand All @@ -217,6 +221,7 @@ public function markMigrated($version, $path)
}

$migrationFile = $migrationFile[0];
/** @var class-string<\Phinx\Migration\MigrationInterface> $className */
$className = $this->getMigrationClassName($migrationFile);
require_once $migrationFile;
$Migration = new $className('default', $version);
Expand All @@ -233,11 +238,11 @@ public function markMigrated($version, $path)
*
* @param \Symfony\Component\Console\Input\InputInterface $input Input interface from which argument and options
* will be extracted to determine which versions to be marked as migrated
* @return array Array of versions that should be marked as migrated
* @return array<int> Array of versions that should be marked as migrated
* @throws \InvalidArgumentException If the `--exclude` or `--only` options are used without `--target`
* or version not found
*/
public function getVersionsToMark($input)
public function getVersionsToMark($input): array
{
$migrations = $this->getMigrations('default');
$versions = array_keys($migrations);
Expand All @@ -249,8 +254,7 @@ public function getVersionsToMark($input)
return $versions;
}

/** @var string $version */
$version = $targetArg ?: $versionArg;
$version = (int)$targetArg ?: (int)$versionArg;

if ($input->getOption('only') || !empty($versionArg)) {
if (!in_array($version, $versions)) {
Expand All @@ -276,7 +280,7 @@ public function getVersionsToMark($input)
* It will start a transaction and rollback in case one of the operation raises an exception
*
* @param string $path Path where to look for migrations
* @param array $versions Versions which should be marked
* @param array<int> $versions Versions which should be marked
* @param \Symfony\Component\Console\Output\OutputInterface $output OutputInterface used to store
* the command output
* @return void
Expand All @@ -285,7 +289,7 @@ public function markVersionsAsMigrated($path, array $versions, $output)
{
$adapter = $this->getEnvironment('default')->getAdapter();

if (empty($versions)) {
if (!$versions) {
$output->writeln('<info>No migrations were found. Nothing to mark as migrated.</info>');

return;
Expand Down Expand Up @@ -328,7 +332,7 @@ public function markVersionsAsMigrated($path, array $versions, $output)
*/
protected function getMigrationClassName($path)
{
$class = preg_replace('/^[0-9]+_/', '', basename($path));
$class = (string)preg_replace('/^[0-9]+_/', '', basename($path));
$class = str_replace('_', ' ', $class);
$class = ucwords($class);
$class = str_replace(' ', '', $class);
Expand Down Expand Up @@ -357,15 +361,16 @@ public function setInput(InputInterface $input)
* Gets an array of database seeders.
*
* Overload the basic behavior to add an instance of the InputInterface the shell call is
* using in order to gives the ability to the AbstractSeed::call() method to propagate options
* using in order to give the ability to the AbstractSeed::call() method to propagate options
* to the other MigrationsDispatcher it is generating.
*
* @throws \InvalidArgumentException
* @return \Phinx\Seed\AbstractSeed[]
* @param string $environment Environment.
* @return \Phinx\Seed\SeedInterface[]
*/
public function getSeeds()
public function getSeeds(string $environment): array
{
parent::getSeeds();
parent::getSeeds($environment);
if (empty($this->seeds)) {
return [];
}
Expand Down
Loading

0 comments on commit 3656173

Please sign in to comment.