Skip to content

Commit e49c578

Browse files
authored
Implement INFORMATION_SCHEMA.SCHEMATA, SHOW DATABASES; verify DB names (#213)
2 parents c378fb0 + 66493a8 commit e49c578

9 files changed

Lines changed: 348 additions & 82 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"npm --prefix wordpress run env:install"
5151
],
5252
"wp-test-phpunit": [
53-
"npm --prefix wordpress run test:php --"
53+
"rm -rf wordpress/src/wp-content/database && npm --prefix wordpress run test:php --"
5454
],
5555
"wp-test-clean": [
5656
"npm --prefix wordpress run env:clean"

constants.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@
5252
}
5353
}
5454

55-
/**
56-
* The DB_NAME constant is required by the SQLite driver.
57-
*
58-
* When DB_NAME is not defined, let's use a default value that corresponds to
59-
* the default value of the constant in "wp-config-sample.php" and in Studio.
60-
*/
61-
if ( ! defined( 'DB_NAME' ) ) {
62-
define( 'DB_NAME', 'database_name_here' );
63-
}
64-
6555
// Allow enabling the SQLite AST driver via environment variable.
6656
if ( ! defined( 'WP_SQLITE_AST_DRIVER' ) && isset( $_ENV['WP_SQLITE_AST_DRIVER'] ) && 'true' === $_ENV['WP_SQLITE_AST_DRIVER'] ) {
6757
define( 'WP_SQLITE_AST_DRIVER', true );

tests/WP_SQLite_Driver_Metadata_Tests.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testInformationSchemaTables() {
6161
'CREATE_TIME' => $result[0]->CREATE_TIME,
6262
'UPDATE_TIME' => null,
6363
'CHECK_TIME' => null,
64-
'TABLE_COLLATION' => 'utf8mb4_general_ci',
64+
'TABLE_COLLATION' => 'utf8mb4_0900_ai_ci',
6565
'CHECKSUM' => null,
6666
'CREATE_OPTIONS' => '',
6767
'TABLE_COMMENT' => '',
@@ -371,6 +371,64 @@ public function testRepairTable() {
371371
);
372372
}
373373

374+
public function testShowDatabases(): void {
375+
// Simple.
376+
$this->assertQuery( 'SHOW DATABASES' );
377+
$actual = $this->engine->get_query_results();
378+
$this->assertEquals(
379+
array(
380+
(object) array( 'Database' => 'information_schema' ),
381+
(object) array( 'Database' => 'wp' ),
382+
),
383+
$actual
384+
);
385+
386+
// With LIKE clause.
387+
$this->assertQuery( 'SHOW DATABASES LIKE "w%"' );
388+
$actual = $this->engine->get_query_results();
389+
$this->assertEquals(
390+
array( (object) array( 'Database' => 'wp' ) ),
391+
$actual
392+
);
393+
394+
// With WHERE clause.
395+
$this->assertQuery( 'SHOW DATABASES WHERE `Database` = "wp"' );
396+
$actual = $this->engine->get_query_results();
397+
$this->assertEquals(
398+
array( (object) array( 'Database' => 'wp' ) ),
399+
$actual
400+
);
401+
}
402+
403+
public function testShowTableSchemas(): void {
404+
$this->assertQuery( 'SHOW SCHEMAS' );
405+
406+
$actual = $this->engine->get_query_results();
407+
$this->assertEquals(
408+
array(
409+
(object) array( 'Database' => 'information_schema' ),
410+
(object) array( 'Database' => 'wp' ),
411+
),
412+
$actual
413+
);
414+
415+
// With LIKE clause.
416+
$this->assertQuery( 'SHOW DATABASES LIKE "inf%"' );
417+
$actual = $this->engine->get_query_results();
418+
$this->assertEquals(
419+
array( (object) array( 'Database' => 'information_schema' ) ),
420+
$actual
421+
);
422+
423+
// With WHERE clause.
424+
$this->assertQuery( 'SHOW DATABASES WHERE `Database` = "information_schema"' );
425+
$actual = $this->engine->get_query_results();
426+
$this->assertEquals(
427+
array( (object) array( 'Database' => 'information_schema' ) ),
428+
$actual
429+
);
430+
}
431+
374432
public function testShowTableStatus() {
375433
$this->assertQuery( 'CREATE TABLE t ( comment_author TEXT, comment_content TEXT )' );
376434

@@ -402,7 +460,7 @@ public function testShowTableStatus() {
402460
'Create_time' => $results[0]->Create_time,
403461
'Update_time' => null,
404462
'Check_time' => null,
405-
'Collation' => 'utf8mb4_general_ci',
463+
'Collation' => 'utf8mb4_0900_ai_ci',
406464
'Checksum' => null,
407465
'Create_options' => '',
408466
'Comment' => '',

tests/WP_SQLite_Driver_Tests.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public function testShowCreateTable1() {
258258
PRIMARY KEY (`ID`),
259259
UNIQUE KEY `option_name` (`option_name`),
260260
KEY `composite` (`option_name`, `option_value`)
261-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci",
261+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
262262
$results[0]->{'Create Table'}
263263
);
264264
}
@@ -286,7 +286,7 @@ public function testShowCreateTableQuoted() {
286286
PRIMARY KEY (`ID`),
287287
UNIQUE KEY `option_name` (`option_name`),
288288
KEY `composite` (`option_name`, `option_value`)
289-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci",
289+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
290290
$results[0]->{'Create Table'}
291291
);
292292
}
@@ -305,7 +305,7 @@ public function testShowCreateTableSimpleTable() {
305305
$this->assertEquals(
306306
'CREATE TABLE `_tmp_table` (
307307
`ID` bigint NOT NULL
308-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
308+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
309309
$results[0]->{'Create Table'}
310310
);
311311
}
@@ -338,7 +338,7 @@ public function testShowCreateTableWithAlterAndCreateIndex() {
338338
`option_value` text NOT NULL,
339339
PRIMARY KEY (`ID`),
340340
KEY `option_name` (`option_name`)
341-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
341+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
342342
$results[0]->{'Create Table'}
343343
);
344344
}
@@ -389,7 +389,7 @@ public function testShowCreateTableWithComments(): void {
389389
" `special_chars_12` text NOT NULL COMMENT '🙂',",
390390
" `special_chars_13` text NOT NULL COMMENT '🙂',",
391391
" KEY `idx_id` (`id`) COMMENT 'Index comment'",
392-
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='Table comment'",
392+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Table comment'",
393393
)
394394
),
395395
$results[0]->{'Create Table'}
@@ -441,7 +441,7 @@ public function testShowCreateTablePreservesDoubleUnderscoreKeyNames() {
441441
PRIMARY KEY (`ID`),
442442
KEY `option_name` (`option_name`),
443443
KEY `double__underscores` (`option_name`, `ID`)
444-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
444+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
445445
$results[0]->{'Create Table'}
446446
);
447447
}
@@ -466,7 +466,7 @@ public function testShowCreateTableWithPrimaryKeyColumnsReverseOrdered() {
466466
`ID_B` bigint NOT NULL,
467467
`ID_C` bigint NOT NULL,
468468
PRIMARY KEY (`ID_B`, `ID_A`, `ID_C`)
469-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
469+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
470470
$results[0]->{'Create Table'}
471471
);
472472
}
@@ -532,7 +532,7 @@ public function testShowCreateTableWithDefaultValues(): void {
532532
" `special_chars_12` text NOT NULL COMMENT '🙂',",
533533
" `special_chars_13` text NOT NULL COMMENT '🙂',",
534534
' PRIMARY KEY (`ID`)',
535-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
535+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
536536
)
537537
),
538538
$results[0]->{'Create Table'}
@@ -1964,7 +1964,7 @@ public function testAlterTableAddIndexWithOrder(): void {
19641964
' `id` int DEFAULT NULL,',
19651965
' `value` varchar(255) DEFAULT NULL,',
19661966
' KEY `idx_value` (`value` DESC)',
1967-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
1967+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
19681968
)
19691969
),
19701970
$result[0]->{'Create Table'}
@@ -3937,7 +3937,7 @@ public function testColumnDefaults(): void {
39373937
. " `description` varchar(250) NOT NULL DEFAULT '',\n"
39383938
. " `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n"
39393939
. " `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP\n"
3940-
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
3940+
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
39413941
$result[0]->{'Create Table'}
39423942
);
39433943
}
@@ -4050,7 +4050,7 @@ public function testShowCreateTableWithEmptyDatetimeDefault() {
40504050
`notempty4` year DEFAULT '2024',
40514051
`notempty5` timestamp NULL DEFAULT '1999-12-12 12:12:12',
40524052
PRIMARY KEY (`ID`)
4053-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci",
4053+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci",
40544054
$results[0]->{'Create Table'}
40554055
);
40564056
}
@@ -4085,7 +4085,7 @@ public function testShowCreateTablePreservesKeyLengths() {
40854085
KEY `meta_key_value` (`meta_key`(20), `meta_value`(82)),
40864086
KEY `order_id_meta_key_meta_value` (`order_id`, `meta_key`(100), `meta_value`(82)),
40874087
KEY `order_id_meta_key_meta_data` (`order_id`, `meta_key`(100), `meta_data`(100))
4088-
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
4088+
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
40894089
$results[0]->{'Create Table'}
40904090
);
40914091
}
@@ -4225,7 +4225,7 @@ public function testTemporaryTableHasPriorityOverStandardTable(): void {
42254225
"CREATE TEMPORARY TABLE `t` (\n"
42264226
. " `b` int DEFAULT NULL,\n"
42274227
. " KEY `ib` (`b`)\n"
4228-
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
4228+
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
42294229
$result[0]->{'Create Table'}
42304230
);
42314231

@@ -5015,7 +5015,7 @@ public function testIndexNamePrecedesConstraintName(): void {
50155015
'CREATE TABLE `t` (',
50165016
' `id` int DEFAULT NULL,',
50175017
' UNIQUE KEY `idx_id` (`id`)',
5018-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
5018+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
50195019
)
50205020
),
50215021
$result[0]->{'Create Table'}
@@ -5641,7 +5641,7 @@ public function testCreateIndexWithOrder(): void {
56415641
' `id` int DEFAULT NULL,',
56425642
' `value` varchar(255) DEFAULT NULL,',
56435643
' KEY `idx_value` (`value` DESC)',
5644-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
5644+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
56455645
)
56465646
),
56475647
$result[0]->{'Create Table'}
@@ -5741,7 +5741,7 @@ public function testCreateComplexIndex(): void {
57415741
' `created_at` datetime DEFAULT NULL,',
57425742
' PRIMARY KEY (`id`),',
57435743
" UNIQUE KEY `idx_complex` (`score`, `name`(16) DESC, `created_at` DESC) COMMENT 'Test comment'",
5744-
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
5744+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
57455745
)
57465746
),
57475747
$result[0]->{'Create Table'}
@@ -5975,4 +5975,49 @@ public function testComplexInformationSchemaQueries(): void {
59755975
$this->assertCount( 1, $result );
59765976
$this->assertEquals( $create_table_query, $result[0]->{'Create Table'} );
59775977
}
5978+
5979+
public function testDatabaseNameEmpty(): void {
5980+
$pdo = new PDO( 'sqlite::memory:' );
5981+
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
5982+
5983+
$this->expectException( WP_SQLite_Driver_Exception::class );
5984+
$this->expectExceptionMessage( 'The database name cannot be empty.' );
5985+
new WP_SQLite_Driver( $connection, '' );
5986+
}
5987+
5988+
public function testDatabaseNameMismatch(): void {
5989+
$pdo = new PDO( 'sqlite::memory:' );
5990+
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
5991+
5992+
// Create a driver with database name 'db-one'.
5993+
new WP_SQLite_Driver( $connection, 'db-one' );
5994+
5995+
// Create another driver with the same name - no errors.
5996+
new WP_SQLite_Driver( $connection, 'db-one' );
5997+
5998+
// Create a driver with a different name - failure.
5999+
$this->expectException( WP_SQLite_Driver_Exception::class );
6000+
$this->expectExceptionMessage( "Incorrect database name. The database was created with name 'db-one', but 'db-two' is used in the current session." );
6001+
new WP_SQLite_Driver( $connection, 'db-two' );
6002+
}
6003+
6004+
public function testDatabaseNameMismatchWithExistingInformationSchemaTableData(): void {
6005+
$pdo = new PDO( 'sqlite::memory:' );
6006+
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
6007+
6008+
// Create a driver with database name 'db-one'.
6009+
$driver = new WP_SQLite_Driver( $connection, 'db-one' );
6010+
6011+
// Create a table so that there is a record in the information schema.
6012+
$driver->query( 'CREATE TABLE t (id INT)' );
6013+
6014+
// Delete all variables, including driver version and database name.
6015+
$pdo->exec( sprintf( 'DELETE FROM %s', WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME ) );
6016+
6017+
// Create a driver with a different name - failure.
6018+
// An information schema record with a different database name already exists.
6019+
$this->expectException( WP_SQLite_Driver_Exception::class );
6020+
$this->expectExceptionMessage( "Incorrect database name. The database was created with name 'db-one', but 'db-two' is used in the current session." );
6021+
new WP_SQLite_Driver( $connection, 'db-two' );
6022+
}
59786023
}

0 commit comments

Comments
 (0)