1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
<?php
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\DatabasePostgres;
use Wikimedia\Rdbms\DBQueryError;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\ScopedCallback;
use Wikimedia\TestingAccessWrapper;
/**
* @covers \Wikimedia\Rdbms\Database
* @covers \Wikimedia\Rdbms\DatabasePostgres
* @covers \Wikimedia\Rdbms\Platform\PostgresPlatform
* @group Database
*/
class DatabasePostgresTest extends MediaWikiIntegrationTestCase {
private const SRC_TABLE = 'tmp_src_tbl';
private const DST_TABLE = 'tmp_dst_tbl';
protected function setUp(): void {
parent::setUp();
if ( !$this->getDb() instanceof DatabasePostgres ) {
$this->markTestSkipped( 'Not PostgreSQL' );
}
}
public function addDBDataOnce() {
if ( $this->getDb() instanceof DatabasePostgres ) {
$this->createSourceTable();
$this->createDestTable();
}
}
private function doTestInsertIgnore() {
$fname = __METHOD__;
$reset = new ScopedCallback( function () use ( $fname ) {
if ( $this->getDb()->explicitTrxActive() ) {
$this->getDb()->rollback( $fname );
}
$this->getDb()->query( 'DROP TABLE IF EXISTS ' . $this->getDb()->tableName( 'foo' ), $fname );
} );
$this->getDb()->query(
"CREATE TEMPORARY TABLE {$this->getDb()->tableName( 'foo' )} (i INTEGER NOT NULL PRIMARY KEY)",
__METHOD__
);
$this->getDb()->insert( 'foo', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
// Normal INSERT IGNORE
$this->getDb()->begin( __METHOD__ );
$this->getDb()->insert(
'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__, [ 'IGNORE' ]
);
$this->assertSame( 2, $this->getDb()->affectedRows() );
$this->assertSame(
[ '1', '2', '3', '5' ],
$this->getDb()->newSelectQueryBuilder()
->select( 'i' )
->from( 'foo' )
->orderBy( 'i' )
->caller( __METHOD__ )->fetchFieldValues()
);
$this->getDb()->rollback( __METHOD__ );
// INSERT IGNORE doesn't ignore stuff like NOT NULL violations
$this->getDb()->begin( __METHOD__ );
$this->getDb()->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
try {
$this->getDb()->insert(
'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__, [ 'IGNORE' ]
);
$this->getDb()->endAtomic( __METHOD__ );
$this->fail( 'Expected exception not thrown' );
} catch ( DBQueryError $e ) {
$this->assertSame( 0, $this->getDb()->affectedRows() );
$this->getDb()->cancelAtomic( __METHOD__ );
}
$this->assertSame(
[ '1', '2' ],
$this->getDb()->newSelectQueryBuilder()
->select( 'i' )
->from( 'foo' )
->orderBy( 'i' )
->caller( __METHOD__ )->fetchFieldValues()
);
$this->getDb()->rollback( __METHOD__ );
}
/**
* FIXME: See https://phabricator.wikimedia.org/T259084.
* @group Broken
*/
public function testInsertIgnoreOld() {
if ( $this->getDb()->getServerVersion() < 9.5 ) {
$this->doTestInsertIgnore();
} else {
// Hack version to make it take the old code path
$w = TestingAccessWrapper::newFromObject( $this->getDb() );
$oldVer = $w->numericVersion;
$w->numericVersion = 9.4;
try {
$this->doTestInsertIgnore();
} finally {
$w->numericVersion = $oldVer;
}
}
}
/**
* FIXME: See https://phabricator.wikimedia.org/T259084.
* @group Broken
*/
public function testInsertIgnoreNew() {
if ( $this->getDb()->getServerVersion() < 9.5 ) {
$this->markTestSkipped( 'PostgreSQL version is ' . $this->getDb()->getServerVersion() );
}
$this->doTestInsertIgnore();
}
private function doTestInsertSelectIgnore() {
$fname = __METHOD__;
$reset = new ScopedCallback( function () use ( $fname ) {
if ( $this->getDb()->explicitTrxActive() ) {
$this->getDb()->rollback( $fname );
}
$this->getDb()->query( 'DROP TABLE IF EXISTS ' . $this->getDb()->tableName( 'foo' ), $fname );
$this->getDb()->query( 'DROP TABLE IF EXISTS ' . $this->getDb()->tableName( 'bar' ), $fname );
} );
$this->getDb()->query(
"CREATE TEMPORARY TABLE {$this->getDb()->tableName( 'foo' )} (i INTEGER)",
__METHOD__
);
$this->getDb()->query(
"CREATE TEMPORARY TABLE {$this->getDb()->tableName( 'bar' )} (i INTEGER NOT NULL PRIMARY KEY)",
__METHOD__
);
$this->getDb()->insert( 'bar', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
// Normal INSERT IGNORE
$this->getDb()->begin( __METHOD__ );
$this->getDb()->insert( 'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__ );
$this->getDb()->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
$this->assertSame( 2, $this->getDb()->affectedRows() );
$this->assertSame(
[ '1', '2', '3', '5' ],
$this->getDb()->newSelectQueryBuilder()
->select( 'i' )
->from( 'bar' )
->orderBy( 'i' )
->caller( __METHOD__ )->fetchFieldValues()
);
$this->getDb()->rollback( __METHOD__ );
// INSERT IGNORE doesn't ignore stuff like NOT NULL violations
$this->getDb()->begin( __METHOD__ );
$this->getDb()->insert( 'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__ );
$this->getDb()->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
try {
$this->getDb()->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
$this->getDb()->endAtomic( __METHOD__ );
$this->fail( 'Expected exception not thrown' );
} catch ( DBQueryError $e ) {
$this->assertSame( 0, $this->getDb()->affectedRows() );
$this->getDb()->cancelAtomic( __METHOD__ );
}
$this->assertSame(
[ '1', '2' ],
$this->getDb()->newSelectQueryBuilder()
->select( 'i' )
->from( 'bar' )
->orderBy( 'i' )
->caller( __METHOD__ )->fetchFieldValues()
);
$this->getDb()->rollback( __METHOD__ );
}
/**
* FIXME: See https://phabricator.wikimedia.org/T259084.
* @group Broken
*/
public function testInsertSelectIgnoreOld() {
if ( $this->getDb()->getServerVersion() < 9.5 ) {
$this->doTestInsertSelectIgnore();
} else {
// Hack version to make it take the old code path
$w = TestingAccessWrapper::newFromObject( $this->getDb() );
$oldVer = $w->numericVersion;
$w->numericVersion = 9.4;
try {
$this->doTestInsertSelectIgnore();
} finally {
$w->numericVersion = $oldVer;
}
}
}
/**
* FIXME: See https://phabricator.wikimedia.org/T259084.
* @group Broken
*/
public function testInsertSelectIgnoreNew() {
if ( $this->getDb()->getServerVersion() < 9.5 ) {
$this->markTestSkipped( 'PostgreSQL version is ' . $this->getDb()->getServerVersion() );
}
$this->doTestInsertSelectIgnore();
}
public function testAttributes() {
$dbFactory = $this->getServiceContainer()->getDatabaseFactory();
$this->assertTrue(
$dbFactory->attributesFromType( 'postgres' )[Database::ATTR_SCHEMAS_AS_TABLE_GROUPS]
);
}
public function testInsertIdAfterInsert() {
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
$this->getDb()->insert( self::DST_TABLE, $rows, __METHOD__ );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testInsertIdAfterInsertIgnore() {
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
$this->getDb()->insert( self::DST_TABLE, $rows, __METHOD__, 'IGNORE' );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->getDb()->insert( self::DST_TABLE, $rows, __METHOD__, 'IGNORE' );
$this->assertSame( 0, $this->getDb()->affectedRows() );
$this->assertSame( 0, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testInsertIdAfterReplace() {
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
$this->getDb()->replace( self::DST_TABLE, 'k', $rows, __METHOD__ );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->getDb()->replace( self::DST_TABLE, 'k', $rows, __METHOD__ );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 2, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 2, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testInsertIdAfterUpsert() {
$rows = [ [ 'k' => 'Luca', 'v' => mt_rand( 1, 100 ), 't' => time() ] ];
$set = [
'v = ' . $this->getDb()->buildExcludedValue( 'v' ),
't = ' . $this->getDb()->buildExcludedValue( 't' ) . ' + 1'
];
$this->getDb()->upsert( self::DST_TABLE, $rows, 'k', $set, __METHOD__ );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->getDb()->upsert( self::DST_TABLE, $rows, 'k', $set, __METHOD__ );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testInsertIdAfterInsertSelect() {
$rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ];
$this->getDb()->insert( self::SRC_TABLE, $rows, __METHOD__, 'IGNORE' );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
->select( 'sn' )
->from( self::SRC_TABLE )
->where( [ 'sk' => 'Luca' ] )
->fetchField() );
$this->getDb()->insertSelect(
self::DST_TABLE,
self::SRC_TABLE,
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
[ 'sk' => 'Luca' ],
__METHOD__,
'IGNORE'
);
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testInsertIdAfterInsertSelectIgnore() {
$rows = [ [ 'sk' => 'Luca', 'sv' => mt_rand( 1, 100 ), 'st' => time() ] ];
$this->getDb()->insert( self::SRC_TABLE, $rows, __METHOD__, 'IGNORE' );
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
->select( 'sn' )
->from( self::SRC_TABLE )
->where( [ 'sk' => 'Luca' ] )
->fetchField() );
$this->getDb()->insertSelect(
self::DST_TABLE,
self::SRC_TABLE,
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
[ 'sk' => 'Luca' ],
__METHOD__,
'IGNORE'
);
$this->assertSame( 1, $this->getDb()->affectedRows() );
$this->assertSame( 1, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->getDb()->insertSelect(
self::DST_TABLE,
self::SRC_TABLE,
[ 'k' => 'sk', 'v' => 'sv', 't' => 'st' ],
[ 'sk' => 'Luca' ],
__METHOD__,
'IGNORE'
);
$this->assertSame( 0, $this->getDb()->affectedRows() );
$this->assertSame( 0, $this->getDb()->insertId() );
$this->assertNWhereKEqualsLuca( 1, self::DST_TABLE );
$this->assertSame( 1, $this->getDb()->affectedRows() );
}
public function testFieldAndIndexInfo() {
global $wgDBname;
$this->db->selectDomain( $wgDBname );
$this->db->query(
"CREATE TEMPORARY TABLE tmp_schema_tbl (" .
"n serial not null, " .
"k text not null, " .
"v integer, " .
"t integer, " .
"PRIMARY KEY(n)" .
")"
);
$this->db->query( "CREATE UNIQUE INDEX k ON tmp_schema_tbl (k)" );
$this->db->query( "CREATE INDEX t ON tmp_schema_tbl (t)" );
$this->assertTrue( $this->db->fieldExists( 'tmp_schema_tbl', 'n' ) );
$this->assertTrue( $this->db->fieldExists( 'tmp_schema_tbl', 'k' ) );
$this->assertTrue( $this->db->fieldExists( 'tmp_schema_tbl', 'v' ) );
$this->assertTrue( $this->db->fieldExists( 'tmp_schema_tbl', 't' ) );
$this->assertFalse( $this->db->fieldExists( 'tmp_schema_tbl', 'x' ) );
$this->assertTrue( $this->db->indexExists( 'tmp_schema_tbl', 'k' ) );
$this->assertTrue( $this->db->indexExists( 'tmp_schema_tbl', 't' ) );
$this->assertFalse( $this->db->indexExists( 'tmp_schema_tbl', 'x' ) );
$this->assertFalse( $this->db->indexExists( 'tmp_schema_tbl', 'PRIMARY' ) );
$this->assertTrue( $this->db->indexExists( 'tmp_schema_tbl', 'tmp_schema_tbl_pkey' ) );
$this->assertTrue( $this->db->indexUnique( 'tmp_schema_tbl', 'k' ) );
$this->assertFalse( $this->db->indexUnique( 'tmp_schema_tbl', 't' ) );
$this->assertNull( $this->db->indexUnique( 'tmp_schema_tbl', 'x' ) );
$this->assertNull( $this->db->indexUnique( 'tmp_schema_tbl', 'PRIMARY' ) );
$this->assertTrue( $this->db->indexExists( 'tmp_schema_tbl', 'tmp_schema_tbl_pkey' ) );
}
private function assertNWhereKEqualsLuca( $expected, $table ) {
$this->assertSame( $expected, (int)$this->getDb()->newSelectQueryBuilder()
->select( 'n' )
->from( $table )
->where( [ 'k' => 'Luca' ] )
->fetchField() );
}
private function createSourceTable() {
$encTable = $this->getDb()->tableName( 'tmp_src_tbl' );
$this->getDb()->query( "DROP TABLE IF EXISTS $encTable" );
$this->getDb()->query(
"CREATE TEMPORARY TABLE $encTable (" .
"sn serial not null, " .
"sk text unique not null, " .
"sv integer, " .
"st integer, " .
"PRIMARY KEY(sn)" .
")"
);
}
private function createDestTable() {
$encTable = $this->getDb()->tableName( 'tmp_dst_tbl' );
$this->getDb()->query( "DROP TABLE IF EXISTS $encTable" );
$this->getDb()->query(
"CREATE TEMPORARY TABLE $encTable (" .
"n serial not null, " .
"k text unique not null, " .
"v integer, " .
"t integer, " .
"PRIMARY KEY(n)" .
")"
);
}
}
|