refactor(dbal): Port away from getDatabasePlatform() instanceof pattern

Use getDatabaseProvider instead.

Signed-off-by: Carl Schwan <carl.schwan@nextcloud.com>
pull/55674/head
Carl Schwan 2025-10-10 16:09:18 +07:00
parent 7a43bf26ca
commit 9095a36b94
6 changed files with 17 additions and 28 deletions

@ -8,9 +8,9 @@ declare(strict_types=1);
*/
namespace OCA\Settings\SetupChecks;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use OC\DB\Connection;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\SetupCheck\ISetupCheck;
@ -34,7 +34,8 @@ class MysqlRowFormat implements ISetupCheck {
}
public function run(): SetupResult {
if (!$this->connection->getDatabasePlatform() instanceof MySQLPlatform) {
if (!$this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL
&& !$this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_MARIADB) {
return SetupResult::success($this->l10n->t('You are not using MySQL'));
}

@ -8,10 +8,6 @@ declare(strict_types=1);
*/
namespace OCA\Settings\SetupChecks;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IURLGenerator;
@ -43,9 +39,8 @@ class SupportedDatabase implements ISetupCheck {
}
public function run(): SetupResult {
$version = null;
$databasePlatform = $this->connection->getDatabasePlatform();
if ($databasePlatform instanceof MySQLPlatform) {
$databasePlatform = $this->connection->getDatabaseProvider();
if ($databasePlatform === IDBConnection::PLATFORM_MYSQL || $databasePlatform === IDBConnection::PLATFORM_MARIADB) {
$statement = $this->connection->prepare("SHOW VARIABLES LIKE 'version';");
$result = $statement->execute();
$row = $result->fetch();
@ -91,7 +86,7 @@ class SupportedDatabase implements ISetupCheck {
);
}
}
} elseif ($databasePlatform instanceof PostgreSQLPlatform) {
} elseif ($databasePlatform === IDBConnection::PLATFORM_POSTGRES) {
$statement = $this->connection->prepare('SHOW server_version;');
$result = $statement->execute();
$row = $result->fetch();
@ -111,9 +106,9 @@ class SupportedDatabase implements ISetupCheck {
])
);
}
} elseif ($databasePlatform instanceof OraclePlatform) {
} elseif ($databasePlatform === IDBConnection::PLATFORM_ORACLE) {
$version = 'Oracle';
} elseif ($databasePlatform instanceof SqlitePlatform) {
} elseif ($databasePlatform === IDBConnection::PLATFORM_SQLITE) {
return SetupResult::warning(
$this->l10n->t('SQLite is currently being used as the backend database. For larger installations we recommend that you switch to a different database backend. This is particularly recommended when using the desktop client for file synchronisation. To migrate to another database use the command line tool: "occ db:convert-type".'),
$this->urlGenerator->linkToDocs('admin-db-conversion')

@ -2445,11 +2445,6 @@
<code><![CDATA[getAppValue]]></code>
</DeprecatedMethod>
</file>
<file src="apps/settings/lib/SetupChecks/SupportedDatabase.php">
<DeprecatedMethod>
<code><![CDATA[getDatabasePlatform]]></code>
</DeprecatedMethod>
</file>
<file src="apps/sharebymail/lib/Settings/SettingsManager.php">
<DeprecatedMethod>
<code><![CDATA[getAppValue]]></code>

@ -7,7 +7,6 @@
*/
namespace OC\DB;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\SchemaException;
@ -578,7 +577,7 @@ class MigrationService {
throw new \InvalidArgumentException('Column "' . $table->getName() . '"."' . $thing->getName() . '" is NotNull, but has empty string or null as default.');
}
if ($this->connection->getDatabasePlatform() instanceof OraclePlatform) {
if ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) {
// Oracle doesn't support boolean column with non-null value
if ($thing->getNotnull() && Type::lookupName($thing->getType()) === Types::BOOLEAN) {
$thing->setNotnull(false);

@ -7,7 +7,6 @@
*/
namespace OC\User;
use Doctrine\DBAL\Platforms\OraclePlatform;
use OC\Hooks\PublicEmitter;
use OC\Memcache\WithLocalCache;
use OCP\DB\QueryBuilder\IQueryBuilder;
@ -16,6 +15,7 @@ use OCP\HintException;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IGroup;
use OCP\IUser;
use OCP\IUserBackend;
@ -564,7 +564,7 @@ class Manager extends PublicEmitter implements IUserManager {
* @since 12.0.0
*/
public function countDisabledUsers(): int {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = Server::get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select($queryBuilder->func()->count('*'))
->from('preferences')
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core')))
@ -592,7 +592,7 @@ class Manager extends PublicEmitter implements IUserManager {
* @since 11.0.0
*/
public function countSeenUsers() {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = Server::get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select($queryBuilder->func()->count('*'))
->from('preferences')
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login')))
@ -626,7 +626,7 @@ class Manager extends PublicEmitter implements IUserManager {
* @return string[] with user ids
*/
private function getSeenUserIds($limit = null, $offset = null) {
$queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
$queryBuilder = Server::get(IDBConnection::class)->getQueryBuilder();
$queryBuilder->select(['userid'])
->from('preferences')
->where($queryBuilder->expr()->eq(
@ -728,7 +728,7 @@ class Manager extends PublicEmitter implements IUserManager {
// We can't load all users who already logged in
$limit = min(100, $limit ?: 25);
$connection = \OC::$server->getDatabaseConnection();
$connection = Server::get(IDBConnection::class);
$queryBuilder = $connection->getQueryBuilder();
$queryBuilder->select('pref_login.userid')
->from('preferences', 'pref_login')
@ -739,7 +739,7 @@ class Manager extends PublicEmitter implements IUserManager {
;
// Oracle don't want to run ORDER BY on CLOB column
$loginOrder = $connection->getDatabasePlatform() instanceof OraclePlatform
$loginOrder = $connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE
? $queryBuilder->expr()->castColumn('pref_login.configvalue', IQueryBuilder::PARAM_INT)
: 'pref_login.configvalue';
$queryBuilder

@ -8,7 +8,6 @@
namespace Test\DB;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Index;
@ -708,8 +707,8 @@ class MigrationsTest extends \Test\TestCase {
#[TestWith([true])]
#[TestWith([false])]
public function testEnsureOracleConstraintsBooleanNotNull(bool $isOracle): void {
$this->db->method('getDatabasePlatform')
->willReturn($isOracle ? $this->createMock(OraclePlatform::class) : null);
$this->db->method('getDatabaseProvider')
->willReturn($isOracle ? IDBConnection::PLATFORM_ORACLE : IDBConnection::PLATFORM_MARIADB);
$column = $this->createMock(Column::class);
$column->expects($this->any())