-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add handling for adding/modifying columns with FIRST/AFTER #147
Changes from all commits
3f4246c
c4efdfd
4615adc
31ecd65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1027,6 +1027,279 @@ public function testAlterTableAddNotNullVarcharColumn() { | |
); | ||
} | ||
|
||
public function testAlterTableWithColumnFirstAndAfter() { | ||
$this->assertQuery( | ||
"CREATE TABLE _tmp_table ( | ||
id int(11) NOT NULL, | ||
name varchar(20) NOT NULL default '' | ||
);" | ||
); | ||
|
||
// ADD COLUMN with FIRST | ||
$this->assertQuery( | ||
"ALTER TABLE _tmp_table ADD COLUMN new_first_column VARCHAR(255) NOT NULL DEFAULT '' FIRST" | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't |
||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'name', | ||
'Type' => 'varchar(20)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_first_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
$results | ||
); | ||
|
||
// ADD COLUMN with AFTER | ||
$this->assertQuery( | ||
"ALTER TABLE _tmp_table ADD COLUMN new_column VARCHAR(255) NOT NULL DEFAULT '' AFTER id" | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'name', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't
|
||
'Type' => 'varchar(20)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_first_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
$results | ||
); | ||
|
||
// CHANGE with FIRST | ||
$this->assertQuery( | ||
"ALTER TABLE _tmp_table CHANGE id id int(11) NOT NULL DEFAULT '0' FIRST" | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'name', | ||
'Type' => 'varchar(20)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_first_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After this change, I think the resulting column order should be:
Am I missing something? |
||
$results | ||
); | ||
|
||
// CHANGE with AFTER | ||
$this->assertQuery( | ||
"ALTER TABLE _tmp_table CHANGE id id int(11) NOT NULL DEFAULT '0' AFTER name" | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'name', | ||
'Type' => 'varchar(20)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_first_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new_column', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After this change, I think the resulting column order should be:
Am I missing something? |
||
$results | ||
); | ||
} | ||
|
||
public function testAlterTableWithMultiColumnFirstAndAfter() { | ||
$this->assertQuery( | ||
'CREATE TABLE _tmp_table ( | ||
id int(11) NOT NULL | ||
);' | ||
); | ||
|
||
// ADD COLUMN | ||
$this->assertQuery( | ||
'ALTER TABLE _tmp_table | ||
ADD COLUMN new1 varchar(255) NOT NULL, | ||
ADD COLUMN new2 varchar(255) NOT NULL FIRST, | ||
ADD COLUMN new3 varchar(255) NOT NULL AFTER new1' | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new1', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new2', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new3', | ||
'Type' => 'varchar(255)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After this change, shouldn't the resulting column order be the following?
|
||
$results | ||
); | ||
|
||
// CHANGE | ||
$this->assertQuery( | ||
'ALTER TABLE _tmp_table | ||
CHANGE new1 new1 int(11) NOT NULL FIRST, | ||
CHANGE new2 new2 int(11) NOT NULL, | ||
CHANGE new3 new3 int(11) NOT NULL AFTER new2' | ||
); | ||
$results = $this->assertQuery( 'DESCRIBE _tmp_table;' ); | ||
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '0', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new1', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => null, | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new2', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => 'new3', | ||
'Type' => 'int(11)', | ||
'Null' => 'NO', | ||
'Key' => '', | ||
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), | ||
$results | ||
); | ||
} | ||
|
||
public function testAlterTableAddIndex() { | ||
$result = $this->assertQuery( | ||
"CREATE TABLE _tmp_table ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2939,6 +2939,35 @@ private function execute_alter() { | |
WP_SQLite_Token::FLAG_KEYWORD_DATA_TYPE | ||
) | ||
); | ||
|
||
// Drop "FIRST" and "AFTER <another-column>", as these are not supported in SQLite. | ||
$column_position = $this->rewriter->peek( | ||
array( | ||
'type' => WP_SQLite_Token::TYPE_KEYWORD, | ||
'value' => array( 'FIRST', 'AFTER' ), | ||
) | ||
); | ||
|
||
$comma = $this->rewriter->peek( | ||
array( | ||
'type' => WP_SQLite_Token::TYPE_OPERATOR, | ||
'value' => ',', | ||
) | ||
); | ||
|
||
if ( $column_position && ( ! $comma || $column_position->position < $comma->position ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of SQL syntax is this comma check intended to address? Could you provide an example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @brandonpayton Without the check, it can consume too much, up to the following column definitions. E.g., for a query like this one: ALTER TABLE _tmp_table
ADD COLUMN new1 VARCHAR(255),
ADD COLUMN new2 VARCHAR(255) FIRST You would get a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see. I hadn't noticed that the translator loops to support the comma-separated MySQL ALTER TABLE syntax. Let's add a test for comma-separated syntax as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for explaining! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test in 31ecd65. |
||
$this->rewriter->consume( | ||
array( | ||
'type' => WP_SQLite_Token::TYPE_KEYWORD, | ||
'value' => array( 'FIRST', 'AFTER' ), | ||
) | ||
); | ||
$this->rewriter->drop_last(); | ||
if ( 'AFTER' === strtoupper( $column_position->value ) ) { | ||
$this->rewriter->skip(); | ||
} | ||
} | ||
|
||
$this->update_data_type_cache( | ||
$this->table_name, | ||
$column_name, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to also make assertions about the resulting table structure like this:
sqlite-database-integration/tests/WP_SQLite_Translator_Tests.php
Lines 885 to 907 in dea3efd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Added in c4efdfd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Let's add them after each
assertQuery()
here to check each is doing what is expected.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK! Added in 4615adc.