cakephp / migrations

CakePHP database migrations plugin
Other
137 stars 115 forks source link

Using newQuery() is deprecated. Instead, use `insertQuery()`, `deleteQuery()`, `selectQuery()` or `updateQuery()` #651

Open nook24 opened 10 months ago

nook24 commented 10 months ago

This is a (multiple allowed):

What you did

When using CakePHP 4.5.0 with the migration plugin, I get the following deprecated messages


 == InstallSeed: seeding
2023-10-25 15:10:21 notice: As of 4.5.0, using newQuery() is deprecated. Instead, use `insertQuery()`, `deleteQuery()`, `selectQuery()` or `updateQuery()`. The query objects returned by these methods will emit deprecations that will become fatal errors in 5.0.See https://book.cakephp.org/4/en/appendices/4-5-migration-guide.html for more information.
/opt/openitc/frontend/vendor/cakephp/migrations/src/CakeAdapter.php, line: 83
You can disable all deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED`. Adding `vendor/cakephp/migrations/src/CakeAdapter.php` to `Error.ignoredDeprecationPaths` in your `config/app.php` config will mute deprecations from that file only.
Trace:
/opt/openitc/frontend/vendor/cakephp/cakephp/src/Core/functions.php /opt/openitc/frontend/vendor/cakephp/cakephp/src/Core/functions.php, line 300
/opt/openitc/frontend/vendor/cakephp/cakephp/src/Database/Connection.php /opt/openitc/frontend/vendor/cakephp/cakephp/src/Database/Connection.php, line 506
Cake\Database\Connection->newQuery() /opt/openitc/frontend/vendor/cakephp/migrations/src/CakeAdapter.php, line 83
Migrations\CakeAdapter->getQueryBuilder() /opt/openitc/frontend/config/Seeds/InstallSeed.php, line 153
InstallSeed->run() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php, line 146
Phinx\Migration\Manager\Environment->executeSeed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php, line 420
Phinx\Migration\Manager->executeSeed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php, line 612
Phinx\Migration\Manager->seed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Console/Command/SeedRun.php, line 106
Phinx\Console\Command\SeedRun->execute() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php, line 37
Migrations\Command\Phinx\Seed->parentExecute() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/Phinx/Seed.php, line 76
Migrations\Command\Phinx\Seed->execute() /opt/openitc/frontend/vendor/symfony/console/Command/Command.php, line 326
Symfony\Component\Console\Command\Command->run() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 1063
Symfony\Component\Console\Application->doRunCommand() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 320
Symfony\Component\Console\Application->doRun() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 174
Symfony\Component\Console\Application->run() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/MigrationsCommand.php, line 126
Migrations\Command\MigrationsCommand->execute() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/BaseCommand.php, line 190
Cake\Console\BaseCommand->run() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/MigrationsCommand.php, line 198
Migrations\Command\MigrationsCommand->run() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/CommandRunner.php, line 334
Cake\Console\CommandRunner->runCommand() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/CommandRunner.php, line 172
Cake\Console\CommandRunner->run() /opt/openitc/frontend/bin/cake.php, line 12
[main]

deprecated: 16384 :: As of 4.5.0, using newQuery() is deprecated. Instead, use `insertQuery()`, `deleteQuery()`, `selectQuery()` or `updateQuery()`. The query objects returned by these methods will emit deprecations that will become fatal errors in 5.0.See https://book.cakephp.org/4/en/appendices/4-5-migration-guide.html for more information.
/opt/openitc/frontend/vendor/cakephp/migrations/src/CakeAdapter.php, line: 83
You can disable all deprecation warnings by setting `Error.errorLevel` to `E_ALL & ~E_USER_DEPRECATED`. Adding `vendor/cakephp/migrations/src/CakeAdapter.php` to `Error.ignoredDeprecationPaths` in your `config/app.php` config will mute deprecations from that file only. on line 300 of /opt/openitc/frontend/vendor/cakephp/cakephp/src/Core/functions.php
Stack Trace:

/opt/openitc/frontend/vendor/cakephp/cakephp/src/Core/functions.php /opt/openitc/frontend/vendor/cakephp/cakephp/src/Core/functions.php, line 300
/opt/openitc/frontend/vendor/cakephp/cakephp/src/Database/Connection.php /opt/openitc/frontend/vendor/cakephp/cakephp/src/Database/Connection.php, line 506
Cake\Database\Connection->newQuery() /opt/openitc/frontend/vendor/cakephp/migrations/src/CakeAdapter.php, line 83
Migrations\CakeAdapter->getQueryBuilder() /opt/openitc/frontend/config/Seeds/InstallSeed.php, line 153
InstallSeed->run() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager/Environment.php, line 146
Phinx\Migration\Manager\Environment->executeSeed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php, line 420
Phinx\Migration\Manager->executeSeed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Migration/Manager.php, line 612
Phinx\Migration\Manager->seed() /opt/openitc/frontend/vendor/robmorgan/phinx/src/Phinx/Console/Command/SeedRun.php, line 106
Phinx\Console\Command\SeedRun->execute() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/Phinx/CommandTrait.php, line 37
Migrations\Command\Phinx\Seed->parentExecute() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/Phinx/Seed.php, line 76
Migrations\Command\Phinx\Seed->execute() /opt/openitc/frontend/vendor/symfony/console/Command/Command.php, line 326
Symfony\Component\Console\Command\Command->run() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 1063
Symfony\Component\Console\Application->doRunCommand() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 320
Symfony\Component\Console\Application->doRun() /opt/openitc/frontend/vendor/symfony/console/Application.php, line 174
Symfony\Component\Console\Application->run() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/MigrationsCommand.php, line 126
Migrations\Command\MigrationsCommand->execute() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/BaseCommand.php, line 190
Cake\Console\BaseCommand->run() /opt/openitc/frontend/vendor/cakephp/migrations/src/Command/MigrationsCommand.php, line 198
Migrations\Command\MigrationsCommand->run() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/CommandRunner.php, line 334
Cake\Console\CommandRunner->runCommand() /opt/openitc/frontend/vendor/cakephp/cakephp/src/Console/CommandRunner.php, line 172
Cake\Console\CommandRunner->run() /opt/openitc/frontend/bin/cake.php, line 12
[main]
 == InstallSeed: seeded 0.7359s

This is /opt/openitc/frontend/config/Seeds/InstallSeed.php, line 153:

<?php

$QueryBuilder = $this->getAdapter()->getQueryBuilder();   // line 153

$stm = $QueryBuilder->select('*')
    ->from($table->getName())
    ->where([
        'plugin' => $record['plugin'],
        'task'   => $record['task']
    ])
    ->execute();
    $result = $stm->fetchAll();

    if (empty($result)) {
         $table->insert($record)->save();
    }

Expected Behavior

No deprecated messages

mcube27 commented 2 months ago

Hi, Will this bug be corrected ? It's not wasy to migrate to CakePhp5 for big applications, but we need to run tests that need migrations and that contain some queries ?

markstory commented 2 months ago

@mcube27 No it won't. If you don't want to see deprecations you can turn them off. But you'll need to address the deprecations before you can upgrade.

Upgrading migrations may also help as currently migrations is not using newQuery() anywhere.

mcube27 commented 2 months ago

We use migration sometimes to make sql queries using $this->getQueryBuilder().

That is why I asked if it will be corrected ?

markstory commented 1 month ago

After attempting to fix this with #731, I don't think there is a reasonable solution for this deprecation warning. Phinx providers the getQueryBuilder() method. All of phinx's branches have moved past compatibility with cakephp 4.x making releasing new patch builds more complicated.

If you want to get rid of the deprecations now you'd have to alter your usage of migrations to

$query = $this->adapter->getDecoratedConnection()->selectQuery();
nook24 commented 1 month ago

Thanks for the update. We have scheduled the migration to CakePHP 5 for the end of this year. I guess to replace the old getQueryBuilder() calls should not be to hard.

The changes from your PR are a good reference guess. https://github.com/cakephp/migrations/pull/731/files

Is there any documentation about getDecoratedConnection() available?

markstory commented 1 month ago

Is there any documentation about getDecoratedConnection() available?

No, but it will return a Cake\Database\Connection with the connection that migrations are being run on. That method will be available in both 4.x and 5.x compatible migrations once #732 is merged.

nook24 commented 1 month ago

Perfect, many thanks