From e147b6b53e7455a19d52512e96e2b2304bb3698d Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 27 Jun 2019 19:30:40 -0700 Subject: [PATCH] rdbms: implement IDatabase::serverIsReadOnly() for sqlite/mssql Change-Id: I661fcca7eb3af20201aaa6e742c5149f63dbe7d3 --- .../libs/rdbms/database/DatabaseMssql.php | 11 +++++++ .../libs/rdbms/database/DatabaseSqlite.php | 32 ++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/includes/libs/rdbms/database/DatabaseMssql.php b/includes/libs/rdbms/database/DatabaseMssql.php index 6c003dd5a7..0ee36bdd04 100644 --- a/includes/libs/rdbms/database/DatabaseMssql.php +++ b/includes/libs/rdbms/database/DatabaseMssql.php @@ -374,6 +374,17 @@ class DatabaseMssql extends Database { return $statementOnly; } + public function serverIsReadOnly() { + $encDatabase = $this->addQuotes( $this->getDBname() ); + $res = $this->query( + "SELECT IS_READ_ONLY FROM SYS.DATABASES WHERE NAME = $encDatabase", + __METHOD__ + ); + $row = $this->fetchObject( $res ); + + return $row ? (bool)$row->IS_READ_ONLY : false; + } + /** * @return int */ diff --git a/includes/libs/rdbms/database/DatabaseSqlite.php b/includes/libs/rdbms/database/DatabaseSqlite.php index 46c34b4ad5..b9d721e687 100644 --- a/includes/libs/rdbms/database/DatabaseSqlite.php +++ b/includes/libs/rdbms/database/DatabaseSqlite.php @@ -173,18 +173,8 @@ class DatabaseSqlite extends Database { throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." ); } - $fileName = self::generateFileName( $this->dbDir, $dbName ); - if ( !is_readable( $fileName ) ) { - $error = "SQLite database file not readable"; - $this->connLogger->error( - "Error connecting to {db_server}: {error}", - $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] ) - ); - throw new DBConnectionError( $this, $error ); - } - // Only $dbName is used, the other parameters are irrelevant for SQLite databases - $this->openFile( $fileName, $dbName, $tablePrefix ); + $this->openFile( self::generateFileName( $this->dbDir, $dbName ), $dbName, $tablePrefix ); } /** @@ -196,6 +186,15 @@ class DatabaseSqlite extends Database { * @throws DBConnectionError */ protected function openFile( $fileName, $dbName, $tablePrefix ) { + if ( !$this->hasMemoryPath() && !is_readable( $fileName ) ) { + $error = "SQLite database file not readable"; + $this->connLogger->error( + "Error connecting to {db_server}: {error}", + $this->getLogContext( [ 'method' => __METHOD__, 'error' => $error ] ) + ); + throw new DBConnectionError( $this, $error ); + } + $this->dbPath = $fileName; try { $this->conn = new PDO( @@ -772,6 +771,17 @@ class DatabaseSqlite extends Database { return false; } + public function serverIsReadOnly() { + return ( !$this->hasMemoryPath() && !is_writable( $this->dbPath ) ); + } + + /** + * @return bool + */ + private function hasMemoryPath() { + return ( strpos( $this->dbPath, ':memory:' ) === 0 ); + } + /** * @return string Wikitext of a link to the server software's web site */ -- 2.20.1