MediaWiki  1.32.0
DatabasePostgresTest.php
Go to the documentation of this file.
1 <?php
2 
5 use Wikimedia\ScopedCallback;
6 use Wikimedia\TestingAccessWrapper;
7 
12 
13  private function doTestInsertIgnore() {
14  $fname = __METHOD__;
15  $reset = new ScopedCallback( function () use ( $fname ) {
16  if ( $this->db->explicitTrxActive() ) {
17  $this->db->rollback( $fname );
18  }
19  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'foo' ), $fname );
20  } );
21 
22  $this->db->query(
23  "CREATE TEMPORARY TABLE {$this->db->tableName( 'foo' )} (i INTEGER NOT NULL PRIMARY KEY)",
24  __METHOD__
25  );
26  $this->db->insert( 'foo', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
27 
28  // Normal INSERT IGNORE
29  $this->db->begin( __METHOD__ );
30  $this->db->insert(
31  'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__, [ 'IGNORE' ]
32  );
33  $this->assertSame( 2, $this->db->affectedRows() );
34  $this->assertSame(
35  [ '1', '2', '3', '5' ],
36  $this->db->selectFieldValues( 'foo', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
37  );
38  $this->db->rollback( __METHOD__ );
39 
40  // INSERT IGNORE doesn't ignore stuff like NOT NULL violations
41  $this->db->begin( __METHOD__ );
42  $this->db->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
43  try {
44  $this->db->insert(
45  'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__, [ 'IGNORE' ]
46  );
47  $this->db->endAtomic( __METHOD__ );
48  $this->fail( 'Expected exception not thrown' );
49  } catch ( DBQueryError $e ) {
50  $this->assertSame( 0, $this->db->affectedRows() );
51  $this->db->cancelAtomic( __METHOD__ );
52  }
53  $this->assertSame(
54  [ '1', '2' ],
55  $this->db->selectFieldValues( 'foo', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
56  );
57  $this->db->rollback( __METHOD__ );
58  }
59 
63  public function testInsertIgnoreOld() {
64  if ( !$this->db instanceof DatabasePostgres ) {
65  $this->markTestSkipped( 'Not PostgreSQL' );
66  }
67  if ( $this->db->getServerVersion() < 9.5 ) {
68  $this->doTestInsertIgnore();
69  } else {
70  // Hack version to make it take the old code path
71  $w = TestingAccessWrapper::newFromObject( $this->db );
72  $oldVer = $w->numericVersion;
73  $w->numericVersion = 9.4;
74  try {
75  $this->doTestInsertIgnore();
76  } finally {
77  $w->numericVersion = $oldVer;
78  }
79  }
80  }
81 
85  public function testInsertIgnoreNew() {
86  if ( !$this->db instanceof DatabasePostgres ) {
87  $this->markTestSkipped( 'Not PostgreSQL' );
88  }
89  if ( $this->db->getServerVersion() < 9.5 ) {
90  $this->markTestSkipped( 'PostgreSQL version is ' . $this->db->getServerVersion() );
91  }
92 
93  $this->doTestInsertIgnore();
94  }
95 
96  private function doTestInsertSelectIgnore() {
97  $fname = __METHOD__;
98  $reset = new ScopedCallback( function () use ( $fname ) {
99  if ( $this->db->explicitTrxActive() ) {
100  $this->db->rollback( $fname );
101  }
102  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'foo' ), $fname );
103  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'bar' ), $fname );
104  } );
105 
106  $this->db->query(
107  "CREATE TEMPORARY TABLE {$this->db->tableName( 'foo' )} (i INTEGER)",
108  __METHOD__
109  );
110  $this->db->query(
111  "CREATE TEMPORARY TABLE {$this->db->tableName( 'bar' )} (i INTEGER NOT NULL PRIMARY KEY)",
112  __METHOD__
113  );
114  $this->db->insert( 'bar', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
115 
116  // Normal INSERT IGNORE
117  $this->db->begin( __METHOD__ );
118  $this->db->insert( 'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__ );
119  $this->db->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
120  $this->assertSame( 2, $this->db->affectedRows() );
121  $this->assertSame(
122  [ '1', '2', '3', '5' ],
123  $this->db->selectFieldValues( 'bar', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
124  );
125  $this->db->rollback( __METHOD__ );
126 
127  // INSERT IGNORE doesn't ignore stuff like NOT NULL violations
128  $this->db->begin( __METHOD__ );
129  $this->db->insert( 'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__ );
130  $this->db->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
131  try {
132  $this->db->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
133  $this->db->endAtomic( __METHOD__ );
134  $this->fail( 'Expected exception not thrown' );
135  } catch ( DBQueryError $e ) {
136  $this->assertSame( 0, $this->db->affectedRows() );
137  $this->db->cancelAtomic( __METHOD__ );
138  }
139  $this->assertSame(
140  [ '1', '2' ],
141  $this->db->selectFieldValues( 'bar', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
142  );
143  $this->db->rollback( __METHOD__ );
144  }
145 
149  public function testInsertSelectIgnoreOld() {
150  if ( !$this->db instanceof DatabasePostgres ) {
151  $this->markTestSkipped( 'Not PostgreSQL' );
152  }
153  if ( $this->db->getServerVersion() < 9.5 ) {
154  $this->doTestInsertSelectIgnore();
155  } else {
156  // Hack version to make it take the old code path
157  $w = TestingAccessWrapper::newFromObject( $this->db );
158  $oldVer = $w->numericVersion;
159  $w->numericVersion = 9.4;
160  try {
161  $this->doTestInsertSelectIgnore();
162  } finally {
163  $w->numericVersion = $oldVer;
164  }
165  }
166  }
167 
171  public function testInsertSelectIgnoreNew() {
172  if ( !$this->db instanceof DatabasePostgres ) {
173  $this->markTestSkipped( 'Not PostgreSQL' );
174  }
175  if ( $this->db->getServerVersion() < 9.5 ) {
176  $this->markTestSkipped( 'PostgreSQL version is ' . $this->db->getServerVersion() );
177  }
178 
179  $this->doTestInsertSelectIgnore();
180  }
181 
182 }
DatabasePostgresTest\testInsertSelectIgnoreOld
testInsertSelectIgnoreOld()
Wikimedia\Rdbms\DatabasePostgres::nativeInsertSelect.
Definition: DatabasePostgresTest.php:149
DatabasePostgresTest
Database.
Definition: DatabasePostgresTest.php:11
DatabasePostgresTest\testInsertSelectIgnoreNew
testInsertSelectIgnoreNew()
Wikimedia\Rdbms\DatabasePostgres::nativeInsertSelect.
Definition: DatabasePostgresTest.php:171
Wikimedia\Rdbms\DatabasePostgres
Definition: DatabasePostgres.php:33
DatabasePostgresTest\testInsertIgnoreNew
testInsertIgnoreNew()
Wikimedia\Rdbms\DatabasePostgres::insert.
Definition: DatabasePostgresTest.php:85
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
DatabasePostgresTest\doTestInsertIgnore
doTestInsertIgnore()
Definition: DatabasePostgresTest.php:13
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:121
DatabasePostgresTest\testInsertIgnoreOld
testInsertIgnoreOld()
Wikimedia\Rdbms\DatabasePostgres::insert.
Definition: DatabasePostgresTest.php:63
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2213
DatabasePostgresTest\doTestInsertSelectIgnore
doTestInsertSelectIgnore()
Definition: DatabasePostgresTest.php:96