}
public function dbSchema( $schema = null ) {
+ if ( strlen( $schema ) && $this->getDBname() === null ) {
+ throw new DBUnexpectedError( $this, "Cannot set schema to '$schema'; no database set." );
+ }
+
$old = $this->currentDomain->getSchema();
if ( $schema !== null ) {
$this->currentDomain = new DatabaseDomain(
$this->database = $database;
if ( $schema !== null && ( !is_string( $schema ) || $schema === '' ) ) {
throw new InvalidArgumentException( 'Schema must be null or a non-empty string.' );
+ } elseif ( $database === null && $schema !== null ) {
+ throw new InvalidArgumentException( 'Schema must be null if database is null.' );
}
$this->schema = $schema;
if ( !is_string( $prefix ) ) {
* Check whether the domain $other meets the specifications of this domain
*
* If this instance has a null database specifier, then $other can have any database
- * specified, including the null, and likewise if the schema specifier is null. This
- * is not transitive like equals() since a domain that explicitly wants a certain
+ * specifier, including null. This is likewise true if the schema specifier is null.
+ * This is not transitive like equals() since a domain that explicitly wants a certain
* database or schema cannot be satisfied by one of another (nor null). If the prefix
* is empty and the DB and schema are both null, then the entire domain is considered
* unspecified, and any prefix of $other is considered compatible.
[ '', null, null, '', true ],
'dontcaredb+dontcaredbschema+prefix' =>
[ 'mywiki-mediawiki-prefix_', null, null, 'prefix_', false ],
- 'dontcaredb+schema+prefix' =>
- [ 'mywiki-schema-prefix_', null, 'schema', 'prefix_', false ],
'db+dontcareschema+prefix' =>
[ 'mywiki-schema-prefix_', 'mywiki', null, 'prefix_', false ],
'postgres-db-jobqueue' =>
[ 'mywiki-schema-prefix_', 'thatwiki', 'schema', 'prefix_' ],
'dontcaredb+dontcaredbschema+prefix' =>
[ 'thatwiki-mediawiki-otherprefix_', null, null, 'prefix_' ],
- 'dontcaredb+schema+prefix' =>
- [ 'mywiki-otherschema-prefix_', null, 'schema', 'prefix_' ],
'db+dontcareschema+prefix' =>
[ 'notmywiki-schema-prefix_', 'mywiki', null, 'prefix_' ],
];
$this->assertFalse( $fromId->isCompatible( $compareIdObj ), 'fromId equals string' );
}
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testSchemaWithNoDB1() {
+ new DatabaseDomain( null, 'schema', '' );
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testSchemaWithNoDB2() {
+ DatabaseDomain::newFromId( '-schema-prefix' );
+ }
+
/**
* @covers Wikimedia\Rdbms\DatabaseDomain::isUnspecified
*/
use Wikimedia\Rdbms\DBUnexpectedError;
class DatabaseTest extends PHPUnit\Framework\TestCase {
+ /** @var DatabaseTestHelper */
+ private $db;
use MediaWikiCoversValidator;
* @covers Wikimedia\Rdbms\Database::dbSchema
*/
public function testSchemaAndPrefixMutators() {
+ $ud = DatabaseDomain::newUnspecified();
+
+ $this->assertEquals( $ud->getId(), $this->db->getDomainID() );
+
$old = $this->db->tablePrefix();
$oldDomain = $this->db->getDomainId();
$this->assertInternalType( 'string', $old, 'Prefix is string' );
$oldDomain = $this->db->getDomainId();
$this->assertInternalType( 'string', $old, 'Schema is string' );
$this->assertSame( $old, $this->db->dbSchema(), "Schema unchanged" );
+
+ $this->db->selectDB( 'y' );
$this->assertSame( $old, $this->db->dbSchema( 'xxx' ) );
$this->assertSame( 'xxx', $this->db->dbSchema(), "Schema set" );
$this->db->dbSchema( $old );
$this->assertNotEquals( 'xxx', $this->db->dbSchema() );
- $this->assertSame( $oldDomain, $this->db->getDomainId() );
+ $this->assertSame( "y", $this->db->getDomainId() );
+ }
+
+ /**
+ * @covers Wikimedia\Rdbms\Database::tablePrefix
+ * @covers Wikimedia\Rdbms\Database::dbSchema
+ * @expectedException DBUnexpectedError
+ */
+ public function testSchemaWithNoDB() {
+ $ud = DatabaseDomain::newUnspecified();
+
+ $this->assertEquals( $ud->getId(), $this->db->getDomainID() );
+ $this->assertSame( '', $this->db->dbSchema() );
+
+ $this->db->dbSchema( 'xxx' );
}
/**