Debian Codegraph @

tests/phpunit/integration/includes/db/DatabasePostgresTest.php

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