Skip to content

Commit 6c8f2dc

Browse files
authored
Merge pull request #25016 from nextcloud/enh/noid/add-check-for-unsupported-db
Add setup check to verify that the used DB version is still supported…
2 parents 5ed673e + bffa61f commit 6c8f2dc

File tree

6 files changed

+180
-3
lines changed

6 files changed

+180
-3
lines changed

apps/settings/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,5 @@
6161
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => $baseDir . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
6262
'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => $baseDir . '/../lib/SetupChecks/PhpDefaultCharset.php',
6363
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => $baseDir . '/../lib/SetupChecks/PhpOutputBuffering.php',
64+
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
6465
);

apps/settings/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class ComposerStaticInitSettings
7676
'OCA\\Settings\\SetupChecks\\LegacySSEKeyFormat' => __DIR__ . '/..' . '/../lib/SetupChecks/LegacySSEKeyFormat.php',
7777
'OCA\\Settings\\SetupChecks\\PhpDefaultCharset' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpDefaultCharset.php',
7878
'OCA\\Settings\\SetupChecks\\PhpOutputBuffering' => __DIR__ . '/..' . '/../lib/SetupChecks/PhpOutputBuffering.php',
79+
'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
7980
);
8081

8182
public static function getInitializer(ClassLoader $loader)

apps/settings/lib/Controller/CheckSetupController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
use OCA\Settings\SetupChecks\LegacySSEKeyFormat;
6464
use OCA\Settings\SetupChecks\PhpDefaultCharset;
6565
use OCA\Settings\SetupChecks\PhpOutputBuffering;
66+
use OCA\Settings\SetupChecks\SupportedDatabase;
6667
use OCP\AppFramework\Controller;
6768
use OCP\AppFramework\Http\DataDisplayResponse;
6869
use OCP\AppFramework\Http\DataResponse;
@@ -107,6 +108,8 @@ class CheckSetupController extends Controller {
107108
private $secureRandom;
108109
/** @var IniGetWrapper */
109110
private $iniGetWrapper;
111+
/** @var IDBConnection */
112+
private $connection;
110113

111114
public function __construct($AppName,
112115
IRequest $request,
@@ -122,7 +125,8 @@ public function __construct($AppName,
122125
IDateTimeFormatter $dateTimeFormatter,
123126
MemoryInfo $memoryInfo,
124127
ISecureRandom $secureRandom,
125-
IniGetWrapper $iniGetWrapper) {
128+
IniGetWrapper $iniGetWrapper,
129+
IDBConnection $connection) {
126130
parent::__construct($AppName, $request);
127131
$this->config = $config;
128132
$this->clientService = $clientService;
@@ -137,6 +141,7 @@ public function __construct($AppName,
137141
$this->memoryInfo = $memoryInfo;
138142
$this->secureRandom = $secureRandom;
139143
$this->iniGetWrapper = $iniGetWrapper;
144+
$this->connection = $connection;
140145
}
141146

142147
/**
@@ -714,6 +719,7 @@ public function check() {
714719
$phpOutputBuffering = new PhpOutputBuffering();
715720
$legacySSEKeyFormat = new LegacySSEKeyFormat($this->l10n, $this->config, $this->urlGenerator);
716721
$checkUserCertificates = new CheckUserCertificates($this->l10n, $this->config, $this->urlGenerator);
722+
$supportedDatabases = new SupportedDatabase($this->l10n, $this->connection);
717723

718724
return new DataResponse(
719725
[
@@ -760,6 +766,7 @@ public function check() {
760766
LegacySSEKeyFormat::class => ['pass' => $legacySSEKeyFormat->run(), 'description' => $legacySSEKeyFormat->description(), 'severity' => $legacySSEKeyFormat->severity(), 'linkToDocumentation' => $legacySSEKeyFormat->linkToDocumentation()],
761767
CheckUserCertificates::class => ['pass' => $checkUserCertificates->run(), 'description' => $checkUserCertificates->description(), 'severity' => $checkUserCertificates->severity(), 'elements' => $checkUserCertificates->elements()],
762768
'isDefaultPhoneRegionSet' => $this->config->getSystemValueString('default_phone_region', '') !== '',
769+
SupportedDatabase::class => ['pass' => $supportedDatabases->run(), 'description' => $supportedDatabases->description(), 'severity' => $supportedDatabases->severity()],
763770
]
764771
);
765772
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Morris Jobke <hey@morrisjobke.de>
7+
*
8+
* @author Morris Jobke <hey@morrisjobke.de>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Settings\SetupChecks;
28+
29+
use Doctrine\DBAL\Platforms\MariaDb1027Platform;
30+
use Doctrine\DBAL\Platforms\MySQL57Platform;
31+
use Doctrine\DBAL\Platforms\MySQL80Platform;
32+
use Doctrine\DBAL\Platforms\MySqlPlatform;
33+
use Doctrine\DBAL\Platforms\OraclePlatform;
34+
use Doctrine\DBAL\Platforms\PostgreSQL100Platform;
35+
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
36+
use Doctrine\DBAL\Platforms\SqlitePlatform;
37+
use OCP\IDBConnection;
38+
use OCP\IL10N;
39+
40+
class SupportedDatabase {
41+
/** @var IL10N */
42+
private $l10n;
43+
/** @var IDBConnection */
44+
private $connection;
45+
46+
private $checked = false;
47+
private $description = '';
48+
49+
public function __construct(IL10N $l10n, IDBConnection $connection) {
50+
$this->l10n = $l10n;
51+
$this->connection = $connection;
52+
}
53+
54+
public function check() {
55+
if ($this->checked === true) {
56+
return;
57+
}
58+
$this->checked = true;
59+
60+
switch (get_class($this->connection->getDatabasePlatform())) {
61+
case MySQL80Platform::class: # extends MySQL57Platform
62+
case MySQL57Platform::class: # extends MySQLPlatform
63+
case MariaDb1027Platform::class: # extends MySQLPlatform
64+
case MySqlPlatform::class:
65+
$result = $this->connection->prepare('SHOW VARIABLES LIKE "version";');
66+
$result->execute();
67+
$row = $result->fetch();
68+
$version = strtolower($row['Value']);
69+
70+
if (strpos($version, 'mariadb') !== false) {
71+
if (version_compare($version, '10.2', '<')) {
72+
$this->description = $this->l10n->t('MariaDB version "%s" is used. Nextcloud 21 will no longer support this version and requires MariaDB 10.2 or higher.', $row['Value']);
73+
return;
74+
}
75+
} else {
76+
if (version_compare($version, '8', '<')) {
77+
$this->description = $this->l10n->t('MySQL version "%s" is used. Nextcloud 21 will no longer support this version and requires MySQL 8 or higher.', $row['Value']);
78+
return;
79+
}
80+
}
81+
break;
82+
case SqlitePlatform::class:
83+
break;
84+
case PostgreSQL100Platform::class: # extends PostgreSQL94Platform
85+
case PostgreSQL94Platform::class:
86+
$result = $this->connection->prepare('SHOW server_version;');
87+
$result->execute();
88+
$row = $result->fetch();
89+
if (version_compare($row['server_version'], '9.6', '<')) {
90+
$this->description = $this->l10n->t('PostgreSQL version "%s" is used. Nextcloud 21 will no longer support this version and requires PostgreSQL 9.6 or higher.', $row['server_version']);
91+
return;
92+
}
93+
break;
94+
case OraclePlatform::class:
95+
break;
96+
}
97+
}
98+
99+
public function description(): string {
100+
$this->check();
101+
return $this->description;
102+
}
103+
104+
public function severity(): string {
105+
return 'info';
106+
}
107+
108+
public function run(): bool {
109+
$this->check();
110+
return $this->description === '';
111+
}
112+
}

apps/settings/tests/Controller/CheckSetupControllerTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
namespace OCA\Settings\Tests\Controller;
3636

3737
use bantu\IniGetWrapper\IniGetWrapper;
38+
use Doctrine\DBAL\Platforms\SqlitePlatform;
3839
use OC;
3940
use OC\DB\Connection;
4041
use OC\IntegrityCheck\Checker;
@@ -48,6 +49,7 @@
4849
use OCP\Http\Client\IClientService;
4950
use OCP\IConfig;
5051
use OCP\IDateTimeFormatter;
52+
use OCP\IDBConnection;
5153
use OCP\IL10N;
5254
use OCP\ILogger;
5355
use OCP\IRequest;
@@ -95,6 +97,8 @@ class CheckSetupControllerTest extends TestCase {
9597
private $secureRandom;
9698
/** @var IniGetWrapper|\PHPUnit\Framework\MockObject\MockObject */
9799
private $iniGetWrapper;
100+
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
101+
private $connection;
98102

99103
/**
100104
* Holds a list of directories created during tests.
@@ -135,6 +139,8 @@ protected function setUp(): void {
135139
->getMock();
136140
$this->secureRandom = $this->getMockBuilder(SecureRandom::class)->getMock();
137141
$this->iniGetWrapper = $this->getMockBuilder(IniGetWrapper::class)->getMock();
142+
$this->connection = $this->getMockBuilder(IDBConnection::class)
143+
->disableOriginalConstructor()->getMock();
138144
$this->checkSetupController = $this->getMockBuilder(CheckSetupController::class)
139145
->setConstructorArgs([
140146
'settings',
@@ -152,6 +158,7 @@ protected function setUp(): void {
152158
$this->memoryInfo,
153159
$this->secureRandom,
154160
$this->iniGetWrapper,
161+
$this->connection,
155162
])
156163
->setMethods([
157164
'isReadOnlyConfig',
@@ -553,6 +560,9 @@ public function testCheck() {
553560
}
554561
return '';
555562
});
563+
$sqlitePlatform = $this->getMockBuilder(SqlitePlatform::class)->getMock();
564+
$this->connection->method('getDatabasePlatform')
565+
->willReturn($sqlitePlatform);
556566

557567
$expected = new DataResponse(
558568
[
@@ -606,6 +616,7 @@ public function testCheck() {
606616
'OCA\Settings\SetupChecks\CheckUserCertificates' => ['pass' => false, 'description' => 'There are some user imported SSL certificates present, that are not used anymore with Nextcloud 21. They can be imported on the command line via "occ security:certificates:import" command. Their paths inside the data directory are shown below.', 'severity' => 'warning', 'elements' => ['a', 'b']],
607617
'imageMagickLacksSVGSupport' => false,
608618
'isDefaultPhoneRegionSet' => false,
619+
'OCA\Settings\SetupChecks\SupportedDatabase' => ['pass' => true, 'description' => '', 'severity' => 'info'],
609620
]
610621
);
611622
$this->assertEquals($expected, $this->checkSetupController->check());
@@ -629,6 +640,7 @@ public function testGetCurlVersion() {
629640
$this->memoryInfo,
630641
$this->secureRandom,
631642
$this->iniGetWrapper,
643+
$this->connection,
632644
])
633645
->setMethods(null)->getMock();
634646

@@ -1397,7 +1409,8 @@ public function testIsMysqlUsedWithoutUTF8MB4(string $db, bool $useUTF8MB4, bool
13971409
$this->dateTimeFormatter,
13981410
$this->memoryInfo,
13991411
$this->secureRandom,
1400-
$this->iniGetWrapper
1412+
$this->iniGetWrapper,
1413+
$this->connection
14011414
);
14021415

14031416
$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isMysqlUsedWithoutUTF8MB4'));
@@ -1446,7 +1459,8 @@ public function testIsEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed(string $m
14461459
$this->dateTimeFormatter,
14471460
$this->memoryInfo,
14481461
$this->secureRandom,
1449-
$this->iniGetWrapper
1462+
$this->iniGetWrapper,
1463+
$this->connection
14501464
);
14511465

14521466
$this->assertSame($expected, $this->invokePrivate($checkSetupController, 'isEnoughTempSpaceAvailableIfS3PrimaryStorageIsUsed'));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Morris Jobke <hey@morrisjobke.de>
7+
*
8+
* @author Morris Jobke <hey@morrisjobke.de>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace OCA\Settings\Tests;
28+
29+
use OCA\Settings\SetupChecks\SupportedDatabase;
30+
use OCP\IL10N;
31+
use Test\TestCase;
32+
33+
/**
34+
* @group DB
35+
*/
36+
class SupportedDatabaseTest extends TestCase {
37+
public function testPass(): void {
38+
$l10n = $this->getMockBuilder(IL10N::class)->getMock();
39+
$check = new SupportedDatabase($l10n, \OC::$server->getDatabaseConnection());
40+
$this->assertTrue($check->run());
41+
}
42+
}

0 commit comments

Comments
 (0)