MediaWiki  master
DatabasePostgresTest.php
Go to the documentation of this file.
1 <?php
2 
8 
13 
14  private function doTestInsertIgnore() {
15  $fname = __METHOD__;
16  $reset = new ScopedCallback( function () use ( $fname ) {
17  if ( $this->db->explicitTrxActive() ) {
18  $this->db->rollback( $fname );
19  }
20  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'foo' ), $fname );
21  } );
22 
23  $this->db->query(
24  "CREATE TEMPORARY TABLE {$this->db->tableName( 'foo' )} (i INTEGER NOT NULL PRIMARY KEY)",
25  __METHOD__
26  );
27  $this->db->insert( 'foo', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
28 
29  // Normal INSERT IGNORE
30  $this->db->begin( __METHOD__ );
31  $this->db->insert(
32  'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__, [ 'IGNORE' ]
33  );
34  $this->assertSame( 2, $this->db->affectedRows() );
35  $this->assertSame(
36  [ '1', '2', '3', '5' ],
37  $this->db->selectFieldValues( 'foo', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
38  );
39  $this->db->rollback( __METHOD__ );
40 
41  // INSERT IGNORE doesn't ignore stuff like NOT NULL violations
42  $this->db->begin( __METHOD__ );
43  $this->db->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
44  try {
45  $this->db->insert(
46  'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__, [ 'IGNORE' ]
47  );
48  $this->db->endAtomic( __METHOD__ );
49  $this->fail( 'Expected exception not thrown' );
50  } catch ( DBQueryError $e ) {
51  $this->assertSame( 0, $this->db->affectedRows() );
52  $this->db->cancelAtomic( __METHOD__ );
53  }
54  $this->assertSame(
55  [ '1', '2' ],
56  $this->db->selectFieldValues( 'foo', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
57  );
58  $this->db->rollback( __METHOD__ );
59  }
60 
64  public function testInsertIgnoreOld() {
65  if ( !$this->db instanceof DatabasePostgres ) {
66  $this->markTestSkipped( 'Not PostgreSQL' );
67  }
68  if ( $this->db->getServerVersion() < 9.5 ) {
69  $this->doTestInsertIgnore();
70  } else {
71  // Hack version to make it take the old code path
72  $w = TestingAccessWrapper::newFromObject( $this->db );
73  $oldVer = $w->numericVersion;
74  $w->numericVersion = 9.4;
75  try {
76  $this->doTestInsertIgnore();
77  } finally {
78  $w->numericVersion = $oldVer;
79  }
80  }
81  }
82 
86  public function testInsertIgnoreNew() {
87  if ( !$this->db instanceof DatabasePostgres ) {
88  $this->markTestSkipped( 'Not PostgreSQL' );
89  }
90  if ( $this->db->getServerVersion() < 9.5 ) {
91  $this->markTestSkipped( 'PostgreSQL version is ' . $this->db->getServerVersion() );
92  }
93 
94  $this->doTestInsertIgnore();
95  }
96 
97  private function doTestInsertSelectIgnore() {
98  $fname = __METHOD__;
99  $reset = new ScopedCallback( function () use ( $fname ) {
100  if ( $this->db->explicitTrxActive() ) {
101  $this->db->rollback( $fname );
102  }
103  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'foo' ), $fname );
104  $this->db->query( 'DROP TABLE IF EXISTS ' . $this->db->tableName( 'bar' ), $fname );
105  } );
106 
107  $this->db->query(
108  "CREATE TEMPORARY TABLE {$this->db->tableName( 'foo' )} (i INTEGER)",
109  __METHOD__
110  );
111  $this->db->query(
112  "CREATE TEMPORARY TABLE {$this->db->tableName( 'bar' )} (i INTEGER NOT NULL PRIMARY KEY)",
113  __METHOD__
114  );
115  $this->db->insert( 'bar', [ [ 'i' => 1 ], [ 'i' => 2 ] ], __METHOD__ );
116 
117  // Normal INSERT IGNORE
118  $this->db->begin( __METHOD__ );
119  $this->db->insert( 'foo', [ [ 'i' => 3 ], [ 'i' => 2 ], [ 'i' => 5 ] ], __METHOD__ );
120  $this->db->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
121  $this->assertSame( 2, $this->db->affectedRows() );
122  $this->assertSame(
123  [ '1', '2', '3', '5' ],
124  $this->db->selectFieldValues( 'bar', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
125  );
126  $this->db->rollback( __METHOD__ );
127 
128  // INSERT IGNORE doesn't ignore stuff like NOT NULL violations
129  $this->db->begin( __METHOD__ );
130  $this->db->insert( 'foo', [ [ 'i' => 7 ], [ 'i' => null ] ], __METHOD__ );
131  $this->db->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
132  try {
133  $this->db->insertSelect( 'bar', 'foo', [ 'i' => 'i' ], [], __METHOD__, [ 'IGNORE' ] );
134  $this->db->endAtomic( __METHOD__ );
135  $this->fail( 'Expected exception not thrown' );
136  } catch ( DBQueryError $e ) {
137  $this->assertSame( 0, $this->db->affectedRows() );
138  $this->db->cancelAtomic( __METHOD__ );
139  }
140  $this->assertSame(
141  [ '1', '2' ],
142  $this->db->selectFieldValues( 'bar', 'i', [], __METHOD__, [ 'ORDER BY' => 'i' ] )
143  );
144  $this->db->rollback( __METHOD__ );
145  }
146 
150  public function testInsertSelectIgnoreOld() {
151  if ( !$this->db instanceof DatabasePostgres ) {
152  $this->markTestSkipped( 'Not PostgreSQL' );
153  }
154  if ( $this->db->getServerVersion() < 9.5 ) {
155  $this->doTestInsertSelectIgnore();
156  } else {
157  // Hack version to make it take the old code path
158  $w = TestingAccessWrapper::newFromObject( $this->db );
159  $oldVer = $w->numericVersion;
160  $w->numericVersion = 9.4;
161  try {
162  $this->doTestInsertSelectIgnore();
163  } finally {
164  $w->numericVersion = $oldVer;
165  }
166  }
167  }
168 
172  public function testInsertSelectIgnoreNew() {
173  if ( !$this->db instanceof DatabasePostgres ) {
174  $this->markTestSkipped( 'Not PostgreSQL' );
175  }
176  if ( $this->db->getServerVersion() < 9.5 ) {
177  $this->markTestSkipped( 'PostgreSQL version is ' . $this->db->getServerVersion() );
178  }
179 
180  $this->doTestInsertSelectIgnore();
181  }
182 
186  public function testAttributes() {
187  $this->assertTrue( DatabasePostgres::getAttributes()[Database::ATTR_SCHEMAS_AS_TABLE_GROUPS] );
188  }
189 }
testInsertIgnoreNew()
Wikimedia\Rdbms\DatabasePostgres::insert.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2159
testInsertSelectIgnoreNew()
Wikimedia\Rdbms\DatabasePostgres::nativeInsertSelect.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
testInsertIgnoreOld()
Wikimedia\Rdbms\DatabasePostgres::insert.
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings, LocalSettings).
Definition: Setup.php:123
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
testAttributes()
\Wikimedia\Rdbms\DatabasePostgres::getAttributes
testInsertSelectIgnoreOld()
Wikimedia\Rdbms\DatabasePostgres::nativeInsertSelect.