Migrate database transaction level check to SetupCheck API

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
Côme Chilliet 2023-10-26 15:04:02 +02:00
parent 5957a2bf6b
commit 3c75075eba
No known key found for this signature in database
GPG key ID: A3E2F658B28C760A
7 changed files with 79 additions and 47 deletions

View file

@ -85,6 +85,7 @@ return array(
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => $baseDir . '/../lib/SetupChecks/PhpOutputBuffering.php',
'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => $baseDir . '/../lib/SetupChecks/ReadOnlyConfig.php',
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\UserMigration\\AccountMigrator' => $baseDir . '/../lib/UserMigration/AccountMigrator.php',
'OCA\\Settings\\UserMigration\\AccountMigratorException' => $baseDir . '/../lib/UserMigration/AccountMigratorException.php',
'OCA\\Settings\\WellKnown\\ChangePasswordHandler' => $baseDir . '/../lib/WellKnown/ChangePasswordHandler.php',

View file

@ -100,6 +100,7 @@ class ComposerStaticInitSettings
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpOutputBuffering.php',
'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => __DIR__ . '/..' . '/../lib/SetupChecks/ReadOnlyConfig.php',
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php',
'OCA\\Settings\\UserMigration\\AccountMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigrator.php',
'OCA\\Settings\\UserMigration\\AccountMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigratorException.php',
'OCA\\Settings\\WellKnown\\ChangePasswordHandler' => __DIR__ . '/..' . '/../lib/WellKnown/ChangePasswordHandler.php',

View file

@ -60,6 +60,7 @@ use OCA\Settings\SetupChecks\PhpOutdated;
use OCA\Settings\SetupChecks\PhpOutputBuffering;
use OCA\Settings\SetupChecks\ReadOnlyConfig;
use OCA\Settings\SetupChecks\SupportedDatabase;
use OCA\Settings\SetupChecks\TransactionIsolation;
use OCA\Settings\UserMigration\AccountMigrator;
use OCA\Settings\WellKnown\ChangePasswordHandler;
use OCA\Settings\WellKnown\SecurityTxtHandler;
@ -161,6 +162,7 @@ class Application extends App implements IBootstrap {
$context->registerSetupCheck(PhpOutputBuffering::class);
$context->registerSetupCheck(ReadOnlyConfig::class);
$context->registerSetupCheck(SupportedDatabase::class);
$context->registerSetupCheck(TransactionIsolation::class);
$context->registerUserMigrator(AccountMigrator::class);
}

View file

@ -47,8 +47,6 @@ namespace OCA\Settings\Controller;
use bantu\IniGetWrapper\IniGetWrapper;
use DirectoryIterator;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\TransactionIsolationLevel;
use GuzzleHttp\Exception\ClientException;
use OC;
use OC\AppFramework\Http;
@ -564,20 +562,6 @@ Raw output
return str_contains($this->config->getSystemValue('dbtype'), 'sqlite');
}
protected function hasValidTransactionIsolationLevel(): bool {
try {
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) {
return true;
}
return $this->db->getTransactionIsolation() === TransactionIsolationLevel::READ_COMMITTED;
} catch (Exception $e) {
// ignore
}
return true;
}
protected function hasFileinfoInstalled(): bool {
return \OC_Util::fileInfoLoaded();
}
@ -799,7 +783,6 @@ Raw output
public function check() {
return new DataResponse(
[
'hasValidTransactionIsolationLevel' => $this->hasValidTransactionIsolationLevel(),
'hasFileinfoInstalled' => $this->hasFileinfoInstalled(),
'hasWorkingFileLocking' => $this->hasWorkingFileLocking(),
'hasDBFileLocking' => $this->hasDBFileLocking(),

View file

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
*
* @author Côme Chilliet <come.chilliet@nextcloud.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Settings\SetupChecks;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\TransactionIsolationLevel;
use OC\DB\Connection;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;
class TransactionIsolation implements ISetupCheck {
public function __construct(
private IL10N $l10n,
private IURLGenerator $urlGenerator,
private IDBConnection $connection,
private Connection $db,
) {
}
public function getName(): string {
return $this->l10n->t('Database transaction isolation level');
}
public function getCategory(): string {
return 'database';
}
public function run(): SetupResult {
try {
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_SQLITE) {
return SetupResult::success();
}
if ($this->db->getTransactionIsolation() === TransactionIsolationLevel::READ_COMMITTED) {
return SetupResult::success('Read committed');
} else {
return SetupResult::error(
$this->l10n->t('Your database does not run with "READ COMMITTED" transaction isolation level. This can cause problems when multiple actions are executed in parallel.'),
$this->urlGenerator->linkToDocs('admin-db-transaction')
);
}
} catch (Exception $e) {
return SetupResult::warning(
$this->l10n->t('Was not able to get transaction isolation level: %s', $e->getMessage())
);
}
}
}

View file

@ -189,7 +189,6 @@ class CheckSetupControllerTest extends TestCase {
$this->setupCheckManager,
])
->setMethods([
'hasValidTransactionIsolationLevel',
'hasFileinfoInstalled',
'hasWorkingFileLocking',
'hasDBFileLocking',
@ -377,10 +376,6 @@ class CheckSetupControllerTest extends TestCase {
$this->checkSetupController
->method('isSqliteUsed')
->willReturn(false);
$this->checkSetupController
->expects($this->once())
->method('hasValidTransactionIsolationLevel')
->willReturn(true);
$this->checkSetupController
->expects($this->once())
->method('hasFileinfoInstalled')
@ -484,7 +479,6 @@ class CheckSetupControllerTest extends TestCase {
$expected = new DataResponse(
[
'hasValidTransactionIsolationLevel' => true,
'hasFileinfoInstalled' => true,
'hasWorkingFileLocking' => true,
'hasDBFileLocking' => true,

View file

@ -226,7 +226,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -293,7 +292,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -360,7 +358,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -423,7 +420,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: false,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -485,7 +481,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -547,7 +542,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: false,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -609,7 +603,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: true,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -671,7 +664,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -735,7 +727,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -797,7 +788,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -861,7 +851,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -923,7 +912,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
isFairUseOfFreePushService: true,
@ -1005,7 +993,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1074,7 +1061,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1136,7 +1122,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1198,7 +1183,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1264,7 +1248,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1327,7 +1310,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1387,7 +1369,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1450,7 +1431,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1513,7 +1493,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1575,7 +1554,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1637,7 +1615,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',
@ -1706,7 +1683,6 @@ describe('OC.SetupChecks tests', function() {
hasFileinfoInstalled: true,
hasWorkingFileLocking: true,
hasDBFileLocking: false,
hasValidTransactionIsolationLevel: true,
suggestedOverwriteCliURL: '',
isRandomnessSecure: true,
securityDocs: 'https://docs.nextcloud.com/myDocs.html',