@@ -2062,9 +2062,9 @@ public function testOnDuplicateUpdate() {
2062
2062
); "
2063
2063
);
2064
2064
2065
- // $result1 = $this->assertQuery( "INSERT INTO _tmp_table (name) VALUES ('first');" );
2066
- // $this->assertEquals( '', $this->engine->get_error_message() );
2067
- // $this->assertEquals( 1, $result1 );
2065
+ $ result1 = $ this ->assertQuery ( "INSERT INTO _tmp_table (name) VALUES ('first'); " );
2066
+ $ this ->assertEquals ( '' , $ this ->engine ->get_error_message () );
2067
+ $ this ->assertEquals ( 1 , $ result1 );
2068
2068
2069
2069
$ result2 = $ this ->assertQuery ( "INSERT INTO _tmp_table (name) VALUES ('FIRST') ON DUPLICATE KEY UPDATE `name` = VALUES(`name`); " );
2070
2070
$ this ->assertEquals ( 1 , $ result2 );
@@ -2452,7 +2452,7 @@ public function testInsertOnDuplicateKey() {
2452
2452
$ result1 = $ this ->assertQuery ( "INSERT INTO _tmp_table (name) VALUES ('first'); " );
2453
2453
$ this ->assertEquals ( 1 , $ result1 );
2454
2454
2455
- $ result2 = $ this ->assertQuery ( "INSERT INTO _tmp_table (name) VALUES ('FIRST') ON DUPLICATE KEY SET name=VALUES(`name`); " );
2455
+ $ result2 = $ this ->assertQuery ( "INSERT INTO _tmp_table (name) VALUES ('FIRST') ON DUPLICATE KEY UPDATE name=VALUES(`name`); " );
2456
2456
$ this ->assertEquals ( 1 , $ result2 );
2457
2457
2458
2458
$ this ->assertQuery ( 'SELECT COUNT(*) as cnt FROM _tmp_table ' );
@@ -2775,7 +2775,7 @@ public function testInsertOnDuplicateKeyCompositePk() {
2775
2775
$ this ->assertEquals ( '' , $ this ->engine ->get_error_message () );
2776
2776
$ this ->assertEquals ( 2 , $ result1 );
2777
2777
2778
- $ result2 = $ this ->assertQuery ( 'INSERT INTO wptests_term_relationships VALUES (1,2,2),(1,3,1) ON DUPLICATE KEY SET term_order = VALUES(term_order); ' );
2778
+ $ result2 = $ this ->assertQuery ( 'INSERT INTO wptests_term_relationships VALUES (1,2,2),(1,3,1) ON DUPLICATE KEY UPDATE term_order = VALUES(term_order); ' );
2779
2779
$ this ->assertEquals ( '' , $ this ->engine ->get_error_message () );
2780
2780
$ this ->assertEquals ( 2 , $ result2 );
2781
2781
@@ -3267,21 +3267,20 @@ public function testTranslateLikeBinary() {
3267
3267
$ this ->assertCount ( 2 , $ result ); // Should match both 'first' and 'FIRST'
3268
3268
}
3269
3269
3270
- public function testOnConflictReplace () {
3270
+ public function testUniqueConstraints () {
3271
3271
$ this ->assertQuery (
3272
3272
"CREATE TABLE _tmp_table (
3273
3273
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
3274
3274
name varchar(20) NOT NULL default 'default-value',
3275
3275
unique_name varchar(20) NOT NULL default 'unique-default-value',
3276
- inline_unique_name varchar(20 ) NOT NULL default 'inline-unique-default-value',
3277
- no_default varchar(20) NOT NULL ,
3278
- UNIQUE KEY unique_name ( unique_name)
3276
+ inline_unique_name varchar(30 ) NOT NULL default 'inline-unique-default-value' UNIQUE ,
3277
+ UNIQUE KEY unique_name (unique_name) ,
3278
+ UNIQUE KEY compound_name (name, unique_name)
3279
3279
); "
3280
3280
);
3281
3281
3282
- $ this ->assertQuery (
3283
- "INSERT INTO _tmp_table VALUES (1, null, null, null, ''); "
3284
- );
3282
+ // Insert a row with default values.
3283
+ $ this ->assertQuery ( 'INSERT INTO _tmp_table (ID) VALUES (1) ' );
3285
3284
$ result = $ this ->assertQuery ( 'SELECT * FROM _tmp_table WHERE ID = 1 ' );
3286
3285
$ this ->assertEquals (
3287
3286
array (
@@ -3290,45 +3289,47 @@ public function testOnConflictReplace() {
3290
3289
'name ' => 'default-value ' ,
3291
3290
'unique_name ' => 'unique-default-value ' ,
3292
3291
'inline_unique_name ' => 'inline-unique-default-value ' ,
3293
- 'no_default ' => '' ,
3294
3292
),
3295
3293
),
3296
3294
$ result
3297
3295
);
3298
3296
3297
+ // Insert another row.
3298
+ $ this ->assertQuery (
3299
+ "INSERT INTO _tmp_table VALUES (2, 'ANOTHER-VALUE', 'ANOTHER-UNIQUE-VALUE', 'ANOTHER-INLINE-UNIQUE-VALUE') "
3300
+ );
3301
+
3302
+ // This should fail because of the UNIQUE constraints.
3299
3303
$ this ->assertQuery (
3300
- "INSERT INTO _tmp_table VALUES (2, '1', '2', '3', '4'); "
3304
+ "UPDATE _tmp_table SET unique_name = 'unique-default-value' WHERE ID = 2 " ,
3305
+ 'UNIQUE constraint failed: _tmp_table.unique_name '
3301
3306
);
3307
+
3302
3308
$ this ->assertQuery (
3303
- 'UPDATE _tmp_table SET name = null WHERE ID = 2; '
3309
+ "UPDATE _tmp_table SET inline_unique_name = 'inline-unique-default-value' WHERE ID = 2 " ,
3310
+ 'UNIQUE constraint failed: _tmp_table.inline_unique_name '
3304
3311
);
3305
3312
3306
- $ result = $ this ->assertQuery ( 'SELECT name FROM _tmp_table WHERE ID = 2 ' );
3313
+ // Updating "name" to the same value as the first row should pass.
3314
+ $ this ->assertQuery (
3315
+ "UPDATE _tmp_table SET name = 'default-value' WHERE ID = 2 "
3316
+ );
3307
3317
$ this ->assertEquals (
3308
3318
array (
3309
3319
(object ) array (
3310
- 'name ' => 'default-value ' ,
3320
+ 'ID ' => '2 ' ,
3321
+ 'name ' => 'default-value ' ,
3322
+ 'unique_name ' => 'ANOTHER-UNIQUE-VALUE ' ,
3323
+ 'inline_unique_name ' => 'ANOTHER-INLINE-UNIQUE-VALUE ' ,
3311
3324
),
3312
3325
),
3313
- $ result
3326
+ $ this -> assertQuery ( ' SELECT * FROM _tmp_table WHERE ID = 2 ' )
3314
3327
);
3315
3328
3316
- // This should fail because of the UNIQUE constraint
3329
+ // Updating also "unique_name" should fail on the compound UNIQUE key.
3317
3330
$ this ->assertQuery (
3318
- 'UPDATE _tmp_table SET unique_name = NULL WHERE ID = 2; ' ,
3319
- 'UNIQUE constraint failed: _tmp_table.unique_name '
3320
- );
3321
-
3322
- // Inline unique constraint aren't supported currently, so this should pass
3323
- $ this ->assertQuery (
3324
- 'UPDATE _tmp_table SET inline_unique_name = NULL WHERE ID = 2; ' ,
3325
- ''
3326
- );
3327
-
3328
- // WPDB allows for NULL values in columns that don't have a default value and a NOT NULL constraint
3329
- $ this ->assertQuery (
3330
- 'UPDATE _tmp_table SET no_default = NULL WHERE ID = 2; ' ,
3331
- ''
3331
+ "UPDATE _tmp_table SET inline_unique_name = 'inline-unique-default-value' WHERE ID = 2 " ,
3332
+ 'UNIQUE constraint failed: _tmp_table.inline_unique_name '
3332
3333
);
3333
3334
3334
3335
$ result = $ this ->assertQuery ( 'SELECT * FROM _tmp_table WHERE ID = 2 ' );
@@ -3337,9 +3338,8 @@ public function testOnConflictReplace() {
3337
3338
(object ) array (
3338
3339
'ID ' => '2 ' ,
3339
3340
'name ' => 'default-value ' ,
3340
- 'unique_name ' => '2 ' ,
3341
- 'inline_unique_name ' => 'inline-unique-default-value ' ,
3342
- 'no_default ' => '' ,
3341
+ 'unique_name ' => 'ANOTHER-UNIQUE-VALUE ' ,
3342
+ 'inline_unique_name ' => 'ANOTHER-INLINE-UNIQUE-VALUE ' ,
3343
3343
),
3344
3344
),
3345
3345
$ result
0 commit comments