Update 21.0.7 > 21.0.9 Database error when running migration latest for app core

Nextcloud version (eg, 20.0.5): 21.0.7 (21.0.7.0 from config file)
Operating system and version (eg, Ubuntu 20.04): Debian 10 Buster
Apache or nginx version (eg, Apache 2.4.25): Apache/2.4.38 (Debian)
PHP version (eg, 7.4): PHP 8.0.16 (cli)
MariaDB version : Server version: 10.7.3-MariaDB-1:10.7.3+maria~buster

The issue you are facing:
After downloading the update (with web updater), when I was at the step to update database, I got the error Database error when running migration latest for app core

Is this the first time you’ve seen this error? (Y/N): Y

Steps to replicate it:

  1. update from 21.0.7 to 21.0.9 (21.0.9 is the version number displayed on the web updater page (I’m stuck on it now)
  2. run sudo -u www-data php occ upgrade
Setting log level to debug
Updating database schema
Exception: Database error when running migration latest for app core
Update failed
Maintenance mode is kept active
Resetting log level

The output of your Nextcloud log in Admin > Logging:

Can't access to web, where can I find that ?

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'instanceid' => '',
  'passwordsalt' => '',
  'secret' => '',
  'trusted_domains' =>
  array (
    0 => '',
  ),
  'datadirectory' => '/var/www/html/nextcloud/data',
  'overwrite.cli.url' => 'https://ip/nextcloud',
  'dbtype' => 'mysql',
  'version' => '21.0.7.0',
  'dbname' => 'nextcloud',
  'dbhost' => '127.0.0.1:3306',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'oc_Etshy',
  'dbpassword' => '',
  'installed' => true,
  'htaccess.RewriteBase' => '/nextcloud',
  'maintenance' => false,
  'theme' => '',
  'loglevel' => 2,
  'updater.release.channel' => 'stable',
  'mysql.utf8mb4' => true,
  'mail_smtpmode' => 'smtp',
  'mail_smtpsecure' => 'ssl',
  'mail_sendmailmode' => 'smtp',
  'mail_from_address' => '',
  'mail_domain' => 'gmail.com',
  'mail_smtpauthtype' => 'LOGIN',
  'mail_smtpauth' => 1,
  'mail_smtphost' => 'smtp.gmail.com',
  'mail_smtpport' => '465',
  'mail_smtpname' => '',
  'mail_smtppassword' => '',
  'skeletondirectory' => '',
  'filelocking.enabled' => true,
  'encryption.legacy_format_support' => false,
  'encryption.key_storage_migrated' => false,
  'updater.secret' => '',
);

The output of your Apache/nginx/system log in /var/log/____:

Cli error so not relevant I guess ?

The output of nextcloud.log (just the line about the mariadb exception):

{
    "reqId": "Cslji1sdica1fxrwnLja",
    "level": 3,
    "time": "2022-03-16T15:00:51+00:00",
    "remoteAddr": "",
    "user": "--",
    "app": "core",
    "method": "",
    "url": "--",
    "message": {
        "Exception": "Exception",
        "Message": "Database error when running migration latest for app core",
        "Code": 0,
        "Trace": [
            {
                "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                "line": 309,
                "function": "migrate",
                "class": "OC\\DB\\MigrationService",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                "line": 251,
                "function": "doCoreUpgrade",
                "class": "OC\\Updater",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                "line": 134,
                "function": "doUpgrade",
                "class": "OC\\Updater",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/core/Command/Upgrade.php",
                "line": 249,
                "function": "upgrade",
                "class": "OC\\Updater",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Command/Command.php",
                "line": 255,
                "function": "execute",
                "class": "OC\\Core\\Command\\Upgrade",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                "line": 1009,
                "function": "run",
                "class": "Symfony\\Component\\Console\\Command\\Command",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                "line": 273,
                "function": "doRunCommand",
                "class": "Symfony\\Component\\Console\\Application",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                "line": 149,
                "function": "doRun",
                "class": "Symfony\\Component\\Console\\Application",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/lib/private/Console/Application.php",
                "line": 215,
                "function": "run",
                "class": "Symfony\\Component\\Console\\Application",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/console.php",
                "line": 100,
                "function": "run",
                "class": "OC\\Console\\Application",
                "type": "->"
            },
            {
                "file": "/var/www/html/nextcloud/occ",
                "line": 11,
                "args": [
                    "/var/www/html/nextcloud/console.php"
                ],
                "function": "require_once"
            }
        ],
        "File": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
        "Line": 430,
        "Previous": {
            "Exception": "Doctrine\\DBAL\\Exception\\ConnectionLost",
            "Message": "An exception occurred while executing a query: SQLSTATE[HY000]: General error: 2006 MySQL server has gone away",
            "Code": 2006,
            "Trace": [
                {
                    "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                    "line": 1780,
                    "function": "convert",
                    "class": "Doctrine\\DBAL\\Driver\\API\\MySQL\\ExceptionConverter",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                    "line": 1719,
                    "function": "handleDriverException",
                    "class": "Doctrine\\DBAL\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                    "line": 1067,
                    "function": "convertExceptionDuringQuery",
                    "class": "Doctrine\\DBAL\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                    "line": 231,
                    "function": "executeQuery",
                    "class": "Doctrine\\DBAL\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                    "line": 1809,
                    "function": "executeQuery",
                    "class": "OC\\DB\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                    "line": 256,
                    "function": "query",
                    "class": "Doctrine\\DBAL\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                    "line": 90,
                    "function": "applySchema",
                    "class": "OC\\DB\\Migrator",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                    "line": 529,
                    "function": "migrate",
                    "class": "OC\\DB\\Migrator",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                    "line": 541,
                    "function": "migrateToSchema",
                    "class": "OC\\DB\\Connection",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                    "line": 426,
                    "function": "executeStep",
                    "class": "OC\\DB\\MigrationService",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                    "line": 309,
                    "function": "migrate",
                    "class": "OC\\DB\\MigrationService",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                    "line": 251,
                    "function": "doCoreUpgrade",
                    "class": "OC\\Updater",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                    "line": 134,
                    "function": "doUpgrade",
                    "class": "OC\\Updater",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/core/Command/Upgrade.php",
                    "line": 249,
                    "function": "upgrade",
                    "class": "OC\\Updater",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Command/Command.php",
                    "line": 255,
                    "function": "execute",
                    "class": "OC\\Core\\Command\\Upgrade",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                    "line": 1009,
                    "function": "run",
                    "class": "Symfony\\Component\\Console\\Command\\Command",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                    "line": 273,
                    "function": "doRunCommand",
                    "class": "Symfony\\Component\\Console\\Application",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                    "line": 149,
                    "function": "doRun",
                    "class": "Symfony\\Component\\Console\\Application",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/lib/private/Console/Application.php",
                    "line": 215,
                    "function": "run",
                    "class": "Symfony\\Component\\Console\\Application",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/console.php",
                    "line": 100,
                    "function": "run",
                    "class": "OC\\Console\\Application",
                    "type": "->"
                },
                {
                    "file": "/var/www/html/nextcloud/occ",
                    "line": 11,
                    "args": [
                        "/var/www/html/nextcloud/console.php"
                    ],
                    "function": "require_once"
                }
            ],
            "File": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Driver/API/MySQL/ExceptionConverter.php",
            "Line": 101,
            "Previous": {
                "Exception": "Doctrine\\DBAL\\Driver\\PDO\\Exception",
                "Message": "SQLSTATE[HY000]: General error: 2006 MySQL server has gone away",
                "Code": 2006,
                "Trace": [
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Driver/PDO/Connection.php",
                        "line": 87,
                        "function": "new",
                        "class": "Doctrine\\DBAL\\Driver\\PDO\\Exception",
                        "type": "::"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                        "line": 1062,
                        "function": "query",
                        "class": "Doctrine\\DBAL\\Driver\\PDO\\Connection",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                        "line": 231,
                        "function": "executeQuery",
                        "class": "Doctrine\\DBAL\\Connection",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                        "line": 1809,
                        "function": "executeQuery",
                        "class": "OC\\DB\\Connection",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                        "line": 256,
                        "function": "query",
                        "class": "Doctrine\\DBAL\\Connection",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                        "line": 90,
                        "function": "applySchema",
                        "class": "OC\\DB\\Migrator",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                        "line": 529,
                        "function": "migrate",
                        "class": "OC\\DB\\Migrator",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                        "line": 541,
                        "function": "migrateToSchema",
                        "class": "OC\\DB\\Connection",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                        "line": 426,
                        "function": "executeStep",
                        "class": "OC\\DB\\MigrationService",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                        "line": 309,
                        "function": "migrate",
                        "class": "OC\\DB\\MigrationService",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                        "line": 251,
                        "function": "doCoreUpgrade",
                        "class": "OC\\Updater",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                        "line": 134,
                        "function": "doUpgrade",
                        "class": "OC\\Updater",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/core/Command/Upgrade.php",
                        "line": 249,
                        "function": "upgrade",
                        "class": "OC\\Updater",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Command/Command.php",
                        "line": 255,
                        "function": "execute",
                        "class": "OC\\Core\\Command\\Upgrade",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                        "line": 1009,
                        "function": "run",
                        "class": "Symfony\\Component\\Console\\Command\\Command",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                        "line": 273,
                        "function": "doRunCommand",
                        "class": "Symfony\\Component\\Console\\Application",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                        "line": 149,
                        "function": "doRun",
                        "class": "Symfony\\Component\\Console\\Application",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/lib/private/Console/Application.php",
                        "line": 215,
                        "function": "run",
                        "class": "Symfony\\Component\\Console\\Application",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/console.php",
                        "line": 100,
                        "function": "run",
                        "class": "OC\\Console\\Application",
                        "type": "->"
                    },
                    {
                        "file": "/var/www/html/nextcloud/occ",
                        "line": 11,
                        "args": [
                            "/var/www/html/nextcloud/console.php"
                        ],
                        "function": "require_once"
                    }
                ],
                "File": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Driver/PDO/Exception.php",
                "Line": 26,
                "Previous": {
                    "Exception": "PDOException",
                    "Message": "SQLSTATE[HY000]: General error: 2006 MySQL server has gone away",
                    "Code": "HY000",
                    "Trace": [
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Driver/PDO/Connection.php",
                            "line": 82,
                            "function": "query",
                            "class": "PDO",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                            "line": 1062,
                            "function": "query",
                            "class": "Doctrine\\DBAL\\Driver\\PDO\\Connection",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                            "line": 231,
                            "function": "executeQuery",
                            "class": "Doctrine\\DBAL\\Connection",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Connection.php",
                            "line": 1809,
                            "function": "executeQuery",
                            "class": "OC\\DB\\Connection",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                            "line": 256,
                            "function": "query",
                            "class": "Doctrine\\DBAL\\Connection",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/Migrator.php",
                            "line": 90,
                            "function": "applySchema",
                            "class": "OC\\DB\\Migrator",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/Connection.php",
                            "line": 529,
                            "function": "migrate",
                            "class": "OC\\DB\\Migrator",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                            "line": 541,
                            "function": "migrateToSchema",
                            "class": "OC\\DB\\Connection",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/DB/MigrationService.php",
                            "line": 426,
                            "function": "executeStep",
                            "class": "OC\\DB\\MigrationService",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                            "line": 309,
                            "function": "migrate",
                            "class": "OC\\DB\\MigrationService",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                            "line": 251,
                            "function": "doCoreUpgrade",
                            "class": "OC\\Updater",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/Updater.php",
                            "line": 134,
                            "function": "doUpgrade",
                            "class": "OC\\Updater",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/core/Command/Upgrade.php",
                            "line": 249,
                            "function": "upgrade",
                            "class": "OC\\Updater",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Command/Command.php",
                            "line": 255,
                            "function": "execute",
                            "class": "OC\\Core\\Command\\Upgrade",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                            "line": 1009,
                            "function": "run",
                            "class": "Symfony\\Component\\Console\\Command\\Command",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                            "line": 273,
                            "function": "doRunCommand",
                            "class": "Symfony\\Component\\Console\\Application",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/3rdparty/symfony/console/Application.php",
                            "line": 149,
                            "function": "doRun",
                            "class": "Symfony\\Component\\Console\\Application",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/lib/private/Console/Application.php",
                            "line": 215,
                            "function": "run",
                            "class": "Symfony\\Component\\Console\\Application",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/console.php",
                            "line": 100,
                            "function": "run",
                            "class": "OC\\Console\\Application",
                            "type": "->"
                        },
                        {
                            "file": "/var/www/html/nextcloud/occ",
                            "line": 11,
                            "args": [
                                "/var/www/html/nextcloud/console.php"
                            ],
                            "function": "require_once"
                        }
                    ],
                    "File": "/var/www/html/nextcloud/3rdparty/doctrine/dbal/src/Driver/PDO/Connection.php",
                    "Line": 82
                }
            }
        },
        "CustomMessage": "--"
    },
    "userAgent": "--",
    "version": "21.0.7.0"
}

I tried to run mysqlcheck -- all-databases and every table are OK

Edit : Seems like it’s a timeout issue.
here are my latest timeout settings

MariaDB [(none)]> SHOW VARIABLES LIKE '%timeout%';
+---------------------------------------+----------+
| Variable_name                         | Value    |
+---------------------------------------+----------+
| connect_timeout                       | 43200    |
| deadlock_timeout_long                 | 50000000 |
| deadlock_timeout_short                | 10000    |
| delayed_insert_timeout                | 300      |
| idle_readonly_transaction_timeout     | 0        |
| idle_transaction_timeout              | 0        |
| idle_write_transaction_timeout        | 0        |
| innodb_flush_log_at_timeout           | 1        |
| innodb_lock_wait_timeout              | 240      |
| innodb_rollback_on_timeout            | OFF      |
| interactive_timeout                   | 300      |
| lock_wait_timeout                     | 86400    |
| net_read_timeout                      | 240      |
| net_write_timeout                     | 240      |
| rpl_semi_sync_master_timeout          | 10000    |
| rpl_semi_sync_slave_kill_conn_timeout | 5        |
| slave_net_timeout                     | 60       |
| thread_pool_idle_timeout              | 60       |
| wait_timeout                          | 300      |
+---------------------------------------+----------+
19 rows in set (0.004 sec)

Investigated a bit.
Mariadb loose connection on this SQL

ALTER TABLE oc_jobs ADD argument_hash VARCHAR(32) DEFAULT NULL;

The table oc_jobs just have a few lines but it somehow loose connection.
I increased some option about timeout, max size, etc, but still not working

I still don’t know what’s the cause of the Connection lost issue.

I solved it by making a new temp table with the new column, deleted the old table and rename the temp table to the normal name.

If someone know what could cause this connection lost, I still accept solutions