Skip to content

Commit a056876

Browse files
authored
Merge pull request #27 from Automattic/temporary-table-support
Temporary table support
2 parents 545f95a + b865620 commit a056876

5 files changed

+376
-153
lines changed

Diff for: .github/workflows/wp-tests-phpunit-run.js

-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ const expectedErrors = [
4747
const expectedFailures = [
4848
'Tests_Comment::test_wp_new_comment_respects_comment_field_lengths',
4949
'Tests_Comment::test_wp_update_comment',
50-
'Tests_DB_dbDelta::test_column_type_change_with_hyphens_in_name',
51-
'Tests_DB_dbDelta::test_query_with_backticks_does_not_cause_a_query_to_alter_all_columns_and_indices_to_run_even_if_none_have_changed',
52-
'Tests_DB_dbDelta::test_query_with_backticks_does_not_throw_an_undefined_index_warning',
5350
'Tests_DB_dbDelta::test_spatial_indices',
5451
'Tests_DB::test_charset_switched_to_utf8mb4',
5552
'Tests_DB::test_close',

Diff for: tests/WP_SQLite_Driver_Tests.php

+64
Original file line numberDiff line numberDiff line change
@@ -3079,6 +3079,20 @@ public function testCreateTableIfNotExists(): void {
30793079
);
30803080
}
30813081

3082+
public function testCreateTemporaryTableIfNotExists(): void {
3083+
$this->assertQuery(
3084+
'CREATE TEMPORARY TABLE t (ID INTEGER, name TEXT)'
3085+
);
3086+
$this->assertQuery(
3087+
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
3088+
);
3089+
3090+
$this->expectExceptionMessage( 'table `t` already exists' );
3091+
$this->assertQuery(
3092+
'CREATE TEMPORARY TABLE t (ID INTEGER, name TEXT)'
3093+
);
3094+
}
3095+
30823096
public function testTranslatesComplexDelete() {
30833097
$this->sqlite->query(
30843098
"CREATE TABLE wptests_dummy (
@@ -3976,4 +3990,54 @@ public function getInformationSchemaIsReadonlyWithUseTestData(): array {
39763990
array( 'TRUNCATE tables' ),
39773991
);
39783992
}
3993+
3994+
public function testTemporaryTableHasPriorityOverStandardTable(): void {
3995+
// Create a standard and a temporary table with the same name.
3996+
$this->assertQuery( 'CREATE TABLE t (a INT, INDEX ia(a))' );
3997+
$this->assertQuery( 'CREATE TEMPORARY TABLE t (b INT, INDEX ib(b))' );
3998+
3999+
// SHOW CREATE TABLE will show the temporary table.
4000+
$result = $this->assertQuery( 'SHOW CREATE TABLE t' );
4001+
$this->assertEquals(
4002+
"CREATE TEMPORARY TABLE `t` (\n"
4003+
. " `b` int DEFAULT NULL,\n"
4004+
. " KEY `ib` (`b`)\n"
4005+
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci',
4006+
$result[0]->{'Create Table'}
4007+
);
4008+
4009+
// SHOW COLUMNS FROM will show the temporary table.
4010+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4011+
$this->assertEquals( 'b', $result[0]->Field );
4012+
4013+
// DESCRIBE will show the temporary table.
4014+
$result = $this->assertQuery( 'DESCRIBE t' );
4015+
$this->assertEquals( 'b', $result[0]->Field );
4016+
4017+
// SHOW INDEXES FROM will show the temporary table.
4018+
$result = $this->assertQuery( 'SHOW INDEXES FROM t' );
4019+
$this->assertEquals( 'ib', $result[0]->Key_name );
4020+
4021+
// ALTER TABLE will use the temporary table.
4022+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN c INT' );
4023+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4024+
$this->assertEquals( 'b', $result[0]->Field );
4025+
$this->assertEquals( 'c', $result[1]->Field );
4026+
4027+
// The temporary table doesn't show up in information schema.
4028+
$result = $this->assertQuery( 'SELECT * FROM information_schema.columns WHERE table_name = "t"' );
4029+
$this->assertCount( 1, $result );
4030+
$this->assertEquals( 'a', $result[0]->COLUMN_NAME );
4031+
4032+
// First DROP TABLE removes the temporary table.
4033+
$this->assertQuery( 'DROP TABLE t' );
4034+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4035+
$this->assertEquals( 'a', $result[0]->Field );
4036+
4037+
// Second DROP TABLE removes the standard table.
4038+
$this->expectException( WP_SQLite_Driver_Exception::class );
4039+
$this->expectExceptionMessage( "Table 'wp.t' doesn't exist" );
4040+
$this->assertQuery( 'DROP TABLE t' );
4041+
$result = $this->assertQuery( 'SHOW COLUMNS FROM t' );
4042+
}
39794043
}

Diff for: tests/WP_SQLite_Driver_Translation_Tests.php

+16-25
Original file line numberDiff line numberDiff line change
@@ -401,23 +401,6 @@ public function testCreateTableWithPrimaryKeyAndAutoincrement(): void {
401401
);
402402
}
403403

404-
// @TODO: IF NOT EXISTS
405-
/*public function testCreateTableWithIfNotExists(): void {
406-
$this->assertQuery(
407-
'CREATE TABLE IF NOT EXISTS "t" ( "id" INTEGER ) STRICT',
408-
'CREATE TABLE IF NOT EXISTS t (id INT)'
409-
);
410-
411-
$this->assertExecutedInformationSchemaQueries(
412-
array(
413-
'INSERT INTO _wp_sqlite_mysql_information_schema_tables (table_schema, table_name, table_type, engine, row_format, table_collation)'
414-
. " VALUES ('wp', 't', 'BASE TABLE', 'InnoDB', 'Dynamic', 'utf8mb4_general_ci')",
415-
'INSERT INTO _wp_sqlite_mysql_information_schema_columns (table_schema, table_name, column_name, ordinal_position, column_default, is_nullable, data_type, character_maximum_length, character_octet_length, numeric_precision, numeric_scale, datetime_precision, character_set_name, collation_name, column_type, column_key, extra, privileges, column_comment, generation_expression, srs_id)'
416-
. " VALUES ('wp', 't', 'id', 1, null, 'YES', 'int', null, null, 10, 0, null, null, null, 'int', '', '', 'select,insert,update,references', '', '', null)",
417-
)
418-
);
419-
}*/
420-
421404
public function testCreateTableWithInlineUniqueIndexes(): void {
422405
$this->assertQuery(
423406
array(
@@ -480,7 +463,8 @@ public function testCreateTableWithStandaloneUniqueIndexes(): void {
480463
);
481464
}
482465

483-
public function testCreateTableFromSelectQuery(): void {
466+
// @TODO: Implement information schema support for CREATE TABLE ... AS SELECT.
467+
/*public function testCreateTableFromSelectQuery(): void {
484468
// CREATE TABLE AS SELECT ...
485469
$this->assertQuery(
486470
'CREATE TABLE `t1` AS SELECT * FROM `t2` STRICT',
@@ -493,22 +477,19 @@ public function testCreateTableFromSelectQuery(): void {
493477
'CREATE TABLE `t1` AS SELECT * FROM `t2` STRICT',
494478
'CREATE TABLE t1 SELECT * FROM t2'
495479
);
496-
}
480+
}*/
497481

498482
public function testCreateTemporaryTable(): void {
499483
$this->assertQuery(
500484
'CREATE TEMPORARY TABLE `t` ( `id` INTEGER ) STRICT',
501485
'CREATE TEMPORARY TABLE t (id INT)'
502486
);
503-
504-
// With IF NOT EXISTS.
505-
$this->assertQuery(
506-
'CREATE TEMPORARY TABLE IF NOT EXISTS `t` ( `id` INTEGER ) STRICT',
507-
'CREATE TEMPORARY TABLE IF NOT EXISTS t (id INT)'
508-
);
509487
}
510488

511489
public function testDropTemporaryTable(): void {
490+
// Create a temporary table first so DROP doesn't fail.
491+
$this->driver->query( 'CREATE TEMPORARY TABLE t (id INT)' );
492+
512493
$this->assertQuery(
513494
'DROP TABLE `temp`.`t`',
514495
'DROP TEMPORARY TABLE t'
@@ -1347,6 +1328,16 @@ private function assertQuery( $expected, string $query ): void {
13471328
$executed_queries = array_values( array_slice( $executed_queries, 1, -1, true ) );
13481329
}
13491330

1331+
// Remove temporary table existence checks.
1332+
$executed_queries = array_values(
1333+
array_filter(
1334+
$executed_queries,
1335+
function ( $query ) {
1336+
return "SELECT 1 FROM sqlite_temp_schema WHERE type = 'table' AND name = ?" !== $query;
1337+
}
1338+
)
1339+
);
1340+
13501341
// Remove "information_schema" queries.
13511342
$executed_queries = array_values(
13521343
array_filter(

0 commit comments

Comments
 (0)