Skip to content

Commit

Permalink
v2.1.13 (#143)
Browse files Browse the repository at this point in the history
Version 2.1.13

---------

Co-authored-by: Jeroen P <[email protected]>
Co-authored-by: Rostislav Wolný <[email protected]>
  • Loading branch information
3 people authored Aug 2, 2024
1 parent 0529506 commit d913e6b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
2 changes: 1 addition & 1 deletion load.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: SQLite Database Integration
* Description: SQLite database driver drop-in.
* Author: The WordPress Team
* Version: 2.1.12
* Version: 2.1.13
* Requires PHP: 7.0
* Textdomain: sqlite-database-integration
*
Expand Down
2 changes: 1 addition & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Contributors: wordpressdotorg, aristath
Requires at least: 6.4
Tested up to: 6.6.1
Requires PHP: 7.0
Stable tag: 2.1.12
Stable tag: 2.1.13
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Tags: performance, database
Expand Down
75 changes: 75 additions & 0 deletions tests/WP_SQLite_Translator_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,30 @@ public function testShowCreateTableWithColumnKeys() {
);
}

public function testShowCreateTableWithCorrectDefaultValues() {
$this->assertQuery(
"CREATE TABLE _tmp__table (
ID BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
default_empty_string VARCHAR(255) default '',
null_no_default VARCHAR(255),
);"
);

$this->assertQuery(
'SHOW CREATE TABLE _tmp__table;'
);
$results = $this->engine->get_query_results();
$this->assertEquals(
'CREATE TABLE `_tmp__table` (
`ID` bigint NOT NULL AUTO_INCREMENT,
`default_empty_string` varchar(255) DEFAULT \'\',
`null_no_default` varchar(255),
PRIMARY KEY (`ID`)
);',
$results[0]->{'Create Table'}
);
}

public function testSelectIndexHintForce() {
$this->assertQuery( "INSERT INTO _options (option_name) VALUES ('first');" );
$result = $this->assertQuery(
Expand Down Expand Up @@ -1074,6 +1098,57 @@ public function testAlterTableModifyColumn() {
$this->assertEquals( 2, $result[0]->ID );
}


public function testAlterTableModifyColumnWithSkippedColumnKeyword() {
$this->assertQuery(
"CREATE TABLE _tmp_table (
ID INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
name varchar(20) NOT NULL default '',
lastname varchar(20) NOT NULL default '',
KEY composite (name, lastname),
UNIQUE KEY name (name)
);"
);
// Insert a record
$result = $this->assertQuery( "INSERT INTO _tmp_table (ID, name, lastname) VALUES (1, 'Johnny', 'Appleseed');" );
$this->assertEquals( 1, $result );

// Primary key violation:
$result = $this->engine->query( "INSERT INTO _tmp_table (ID, name, lastname) VALUES (1, 'Mike', 'Pearseed');" );
$this->assertEquals( false, $result );

// Unique constraint violation:
$result = $this->engine->query( "INSERT INTO _tmp_table (ID, name, lastname) VALUES (2, 'Johnny', 'Appleseed');" );
$this->assertEquals( false, $result );

// Rename the "name" field to "firstname":
$result = $this->engine->query( "ALTER TABLE _tmp_table CHANGE name firstname varchar(50) NOT NULL default 'mark';" );
$this->assertEquals( '', $this->engine->get_error_message() );
$this->assertEquals( 1, $result );

// Confirm the original data is still there:
$result = $this->engine->query( 'SELECT * FROM _tmp_table;' );
$this->assertCount( 1, $result );
$this->assertEquals( 1, $result[0]->ID );
$this->assertEquals( 'Johnny', $result[0]->firstname );
$this->assertEquals( 'Appleseed', $result[0]->lastname );

// Confirm the primary key is intact:
$result = $this->engine->query( "INSERT INTO _tmp_table (ID, firstname, lastname) VALUES (1, 'Mike', 'Pearseed');" );
$this->assertEquals( false, $result );

// Confirm the unique key is intact:
$result = $this->engine->query( "INSERT INTO _tmp_table (ID, firstname, lastname) VALUES (2, 'Johnny', 'Appleseed');" );
$this->assertEquals( false, $result );

// Confirm the autoincrement still works:
$result = $this->engine->query( "INSERT INTO _tmp_table (firstname, lastname) VALUES ('John', 'Doe');" );
$this->assertEquals( true, $result );
$result = $this->engine->query( "SELECT * FROM _tmp_table WHERE firstname='John';" );
$this->assertCount( 1, $result );
$this->assertEquals( 2, $result[0]->ID );
}

public function testAlterTableModifyColumnWithHyphens() {
$result = $this->assertQuery(
'CREATE TABLE wptests_dbdelta_test2 (
Expand Down
10 changes: 6 additions & 4 deletions wp-includes/sqlite/class-wp-sqlite-translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2916,7 +2916,8 @@ private function execute_alter() {
)
);
$op_type = strtoupper( $this->rewriter->consume()->token ?? '' );
$op_subject = strtoupper( $this->rewriter->consume()->token ?? '' );
$op_raw_subject = $this->rewriter->consume()->token ?? '';
$op_subject = strtoupper( $op_raw_subject );
$mysql_index_type = $this->normalize_mysql_index_type( $op_subject );
$is_index_op = (bool) $mysql_index_type;

Expand All @@ -2941,9 +2942,10 @@ private function execute_alter() {
);
} elseif ( 'DROP' === $op_type && 'COLUMN' === $op_subject ) {
$this->rewriter->consume_all();
} elseif ( 'CHANGE' === $op_type && 'COLUMN' === $op_subject ) {
} elseif ( 'CHANGE' === $op_type ) {
// Parse the new column definition.
$from_name = $this->normalize_column_name( $this->rewriter->skip()->token );
$raw_from_name = 'COLUMN' === $op_subject ? $this->rewriter->skip()->token : $op_raw_subject;
$from_name = $this->normalize_column_name( $raw_from_name );
$new_field = $this->parse_mysql_create_table_field();
$alter_terminator = end( $this->rewriter->output_tokens );
$this->update_data_type_cache(
Expand Down Expand Up @@ -3522,7 +3524,7 @@ protected function get_column_definitions( $table_name, $columns ) {
$definition[] = 'NOT NULL';
}

if ( '' !== $column->dflt_value && ! $is_auto_incr ) {
if ( null !== $column->dflt_value && '' !== $column->dflt_value && ! $is_auto_incr ) {
$definition[] = 'DEFAULT ' . $column->dflt_value;
}

Expand Down

0 comments on commit d913e6b

Please sign in to comment.