| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MediaWikiServices; |
| 4 |
use Wikimedia\Rdbms\Database; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class DatabaseIntegrationTest extends MediaWikiIntegrationTestCase { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
protected $db; |
| 15 |
|
| 16 |
protected function setUp(): void { |
| 17 |
parent::setUp(); |
| 18 |
$this->db = MediaWikiServices::getInstance() |
| 19 |
->getConnectionProvider() |
| 20 |
->getPrimaryDatabase(); |
| 21 |
} |
| 22 |
|
| 23 |
public function testUnknownTableCorruptsResults() { |
| 24 |
$res = $this->db->newSelectQueryBuilder() |
| 25 |
->select( '*' ) |
| 26 |
->from( 'page' ) |
| 27 |
->where( [ 'page_id' => 1 ] ) |
| 28 |
->fetchResultSet(); |
| 29 |
$this->assertFalse( $this->db->tableExists( 'foobarbaz' ) ); |
| 30 |
$this->assertIsInt( $res->numRows() ); |
| 31 |
} |
| 32 |
|
| 33 |
public function testUniformTablePrefix() { |
| 34 |
global $IP; |
| 35 |
$path = "$IP/maintenance/tables.json"; |
| 36 |
$tables = json_decode( file_get_contents( $path ), true ); |
| 37 |
|
| 38 |
|
| 39 |
$excludeList = [ |
| 40 |
'user_newtalk', |
| 41 |
'objectcache', |
| 42 |
]; |
| 43 |
|
| 44 |
$prefixes = []; |
| 45 |
foreach ( $tables as $table ) { |
| 46 |
$tableName = $table['name']; |
| 47 |
|
| 48 |
if ( in_array( $tableName, $excludeList ) ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
foreach ( $table['columns'] as $column ) { |
| 53 |
$prefixes[] = strtok( $column['name'], '_' ); |
| 54 |
} |
| 55 |
foreach ( $table['indexes'] ?? [] as $index ) { |
| 56 |
$prefixes[] = strtok( $index['name'], '_' ); |
| 57 |
} |
| 58 |
|
| 59 |
if ( count( array_unique( $prefixes ) ) === 1 ) { |
| 60 |
$prefixes = []; |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
$list = implode( '_, ', $prefixes ) . '_'; |
| 65 |
|
| 66 |
$this->fail( |
| 67 |
"Columns and indexes of '$tableName' table should" |
| 68 |
. " have uniform prefix. Non-uniform found: [ $list ]" |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
$this->assertSame( [], $prefixes ); |
| 73 |
} |
| 74 |
|
| 75 |
public function automaticSqlGenerationParams() { |
| 76 |
return [ |
| 77 |
[ 'mysql' ], |
| 78 |
[ 'sqlite' ], |
| 79 |
[ 'postgres' ], |
| 80 |
]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function testAutomaticSqlGeneration( $type ) { |
| 87 |
global $IP; |
| 88 |
$abstractSchemaPath = "$IP/maintenance/tables.json"; |
| 89 |
if ( $type === 'mysql' ) { |
| 90 |
$oldPath = "$IP/maintenance/tables-generated.sql"; |
| 91 |
} else { |
| 92 |
$oldPath = "$IP/maintenance/$type/tables-generated.sql"; |
| 93 |
} |
| 94 |
$oldContent = file_get_contents( $oldPath ); |
| 95 |
$newPath = $this->getNewTempFile(); |
| 96 |
$maintenanceScript = new GenerateSchemaSql(); |
| 97 |
$maintenanceScript->loadWithArgv( |
| 98 |
[ '--json=' . $abstractSchemaPath, '--sql=' . $newPath, '--type=' . $type, '--quiet' ] |
| 99 |
); |
| 100 |
$maintenanceScript->execute(); |
| 101 |
$this->assertEquals( |
| 102 |
$oldContent, |
| 103 |
file_get_contents( $newPath ), |
| 104 |
"The generated schema in '$type' type has to be the same" |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function testBooleanValues() { |
| 112 |
$res = $this->db->newSelectQueryBuilder() |
| 113 |
->select( [ 'false' => '1=0', 'true' => '1=1' ] ) |
| 114 |
->fetchResultSet(); |
| 115 |
$obj = $res->fetchObject(); |
| 116 |
$this->assertCount( 2, (array)$obj ); |
| 117 |
$this->assertSame( '0', $obj->false ); |
| 118 |
$this->assertSame( '1', $obj->true ); |
| 119 |
|
| 120 |
$res->seek( 0 ); |
| 121 |
$row = $res->fetchRow(); |
| 122 |
$this->assertCount( 4, $row ); |
| 123 |
$this->assertSame( '0', $row[0] ); |
| 124 |
$this->assertSame( '1', $row[1] ); |
| 125 |
$this->assertSame( '0', $row['false'] ); |
| 126 |
$this->assertSame( '1', $row['true'] ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testListTables() { |
| 130 |
$prefix = $this->db->tablePrefix() . 'listtables_'; |
| 131 |
$table = $prefix . 'table'; |
| 132 |
$view = $prefix . 'view'; |
| 133 |
$allTables = $this->db->listTables(); |
| 134 |
$this->assertIsArray( $allTables ); |
| 135 |
|
| 136 |
$this->assertSame( [], $this->db->listTables( $prefix ) ); |
| 137 |
|
| 138 |
try { |
| 139 |
$this->db->query( "CREATE TABLE $table (i INT)" ); |
| 140 |
$this->assertSame( [ $table ], $this->db->listTables( $prefix ) ); |
| 141 |
|
| 142 |
$this->db->query( "CREATE VIEW $view AS SELECT * FROM $table" ); |
| 143 |
$this->assertSame( [ $table ], $this->db->listTables( $prefix ) ); |
| 144 |
} finally { |
| 145 |
$this->db->query( "DROP VIEW IF EXISTS $view" ); |
| 146 |
$this->db->query( "DROP TABLE IF EXISTS $table" ); |
| 147 |
} |
| 148 |
} |
| 149 |
} |