migrations icon indicating copy to clipboard operation
migrations copied to clipboard

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

Open nook24 opened this issue 2 years ago • 7 comments

This is a (multiple allowed):

  • [x] bug

  • [ ] enhancement

  • [ ] feature-discussion (RFC)

  • CakePHP Version: 4.5.0

  • Migrations plugin version: 3.9.0 @stable in composer.json

  • Bake plugin version (if relevant): EXACT RELEASE VERSION OR COMMIT HASH, HERE.

  • Database server mysql Ver 8.0.34-0ubuntu0.22.04.1

  • PHP Version: PHP 8.1.2-1ubuntu2.14

  • Platform / OS: Ubuntu and RHEL

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

nook24 avatar Oct 25 '23 13:10 nook24

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 ?

mcube27 avatar Jun 17 '24 13:06 mcube27

@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.

markstory avatar Jun 21 '24 15:06 markstory

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

That is why I asked if it will be corrected ?

mcube27 avatar Jul 12 '24 13:07 mcube27

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();

markstory avatar Jul 28 '24 23:07 markstory

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?

nook24 avatar Jul 29 '24 12:07 nook24

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.

markstory avatar Jul 29 '24 19:07 markstory

Perfect, many thanks

nook24 avatar Jul 29 '24 20:07 nook24