|
11 | 11 |
|
12 | 12 | namespace CodeIgniter\Database\Live;
|
13 | 13 |
|
| 14 | +use CodeIgniter\Database\Exceptions\DatabaseException; |
14 | 15 | use CodeIgniter\Database\Forge;
|
15 | 16 | use CodeIgniter\Database\RawSql;
|
16 | 17 | use CodeIgniter\Test\CIUnitTestCase;
|
@@ -39,10 +40,6 @@ final class SclubricantsTest extends CIUnitTestCase
|
39 | 40 | protected function setUp(): void
|
40 | 41 | {
|
41 | 42 | parent::setUp();
|
42 |
| - |
43 |
| - if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) { |
44 |
| - $this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.'); |
45 |
| - } |
46 | 43 | }
|
47 | 44 |
|
48 | 45 | public function setupTable()
|
@@ -150,6 +147,10 @@ public function setupTable()
|
150 | 147 |
|
151 | 148 | public function testUpdateBatchUpdateFieldsAndAlias()
|
152 | 149 | {
|
| 150 | + if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) { |
| 151 | + $this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.'); |
| 152 | + } |
| 153 | + |
153 | 154 | $data = [
|
154 | 155 | [
|
155 | 156 |
|
@@ -238,6 +239,10 @@ public function testUpdateBatchUpdateFieldsAndAlias()
|
238 | 239 |
|
239 | 240 | public function testUpdateBatchWithoutOnConstraint()
|
240 | 241 | {
|
| 242 | + if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) { |
| 243 | + $this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.'); |
| 244 | + } |
| 245 | + |
241 | 246 | $data = [
|
242 | 247 | [
|
243 | 248 | 'name' => 'Derek Nothing', // won't update
|
@@ -274,8 +279,20 @@ public function testUpdateBatchWithoutOnConstraint()
|
274 | 279 | }
|
275 | 280 | }
|
276 | 281 |
|
| 282 | + public function testUpsertNoData() |
| 283 | + { |
| 284 | + $this->expectException(DatabaseException::class); |
| 285 | + $this->expectExceptionMessage('No data availble to process.'); |
| 286 | + |
| 287 | + $this->db->table('user')->onConstraint('email')->upsertBatch(); |
| 288 | + } |
| 289 | + |
277 | 290 | public function testUpdateWithQuery()
|
278 | 291 | {
|
| 292 | + if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) { |
| 293 | + $this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.'); |
| 294 | + } |
| 295 | + |
279 | 296 | $this->setupTable();
|
280 | 297 |
|
281 | 298 | $updateFields = ['country', 'updated_at' => new RawSql('CURRENT_TIMESTAMP')];
|
@@ -380,12 +397,33 @@ public function testSetBatchOneRow()
|
380 | 397 | $evenMoreData-> email = '[email protected]';
|
381 | 398 | $evenMoreData->country = 'Netherlands';
|
382 | 399 |
|
383 |
| - $builder->updateBatch($evenMoreData, 'email'); |
| 400 | + $builder->onConstraint('email')->upsertBatch($evenMoreData, true, 2); |
384 | 401 |
|
385 | 402 | $result = $this->db->table('user')->get()->getResultArray();
|
386 | 403 |
|
387 | 404 | foreach ($result as $row) {
|
388 | 405 | $this->assertSame('Netherlands', $row['country']);
|
389 | 406 | }
|
390 | 407 | }
|
| 408 | + |
| 409 | + public function testRawSqlConstraint() |
| 410 | + { |
| 411 | + if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) { |
| 412 | + $this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.'); |
| 413 | + } |
| 414 | + |
| 415 | + $data = [ |
| 416 | + [ |
| 417 | + 'name' => 'Derek Jones', |
| 418 | + |
| 419 | + 'country' => 'Germany', |
| 420 | + ], |
| 421 | + ]; |
| 422 | + |
| 423 | + $builder = $this->db->table('user'); |
| 424 | + |
| 425 | + $builder->setBatch($data, true, 'db_myalias')->updateFields('name, country')->onConstraint(new RawSql($this->db->protectIdentifiers('user.email') . ' = ' . $this->db->protectIdentifiers('myalias.email')))->updateBatch(); |
| 426 | + |
| 427 | + $this-> seeInDatabase( 'user', [ 'email' => '[email protected]', 'country' => 'Germany']); |
| 428 | + } |
391 | 429 | }
|
0 commit comments