-
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
Add handling for adding/modifying columns with FIRST/AFTER #147
Conversation
c9753ec
to
2d21a4a
Compare
2d21a4a
to
0cbf391
Compare
0cbf391
to
3f4246c
Compare
|
||
$this->assertQuery( | ||
"ALTER TABLE _tmp_table ADD COLUMN new_first_column VARCHAR(255) NOT NULL DEFAULT '' FIRST" | ||
); |
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
$this->assertQuery( 'DESCRIBE _tmp_table;' ); | |
$results = $this->engine->get_query_results(); | |
$this->assertEquals( | |
array( | |
(object) array( | |
'Field' => 'name', | |
'Type' => 'varchar(20)', | |
'Null' => 'NO', | |
'Key' => '', | |
'Default' => '', | |
'Extra' => '', | |
), | |
(object) array( | |
'Field' => 'column', | |
'Type' => 'int', | |
'Null' => 'YES', | |
'Key' => '', | |
'Default' => null, | |
'Extra' => '', | |
), | |
), | |
$results | |
); |
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.
) | ||
); | ||
|
||
if ( $column_position && ( ! $comma || $column_position->position < $comma->position ) ) { |
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.
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 comment
The 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 SQLSTATE[HY000]: General error: 1 near ",": syntax error.
, since you've consumed up to FIRST
from the second column when processing the first column.
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Added a test in 31ecd65.
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.
Thanks for this PR, @JanJakes!
I left a comment about the tests and a question about part of the implementation.
@brandonpayton Thanks! I added a commit and tried the to explain the need for the comma check. |
2c1fe61
to
31ecd65
Compare
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.
IIUC, something must not be working properly because the column order in the tests do not appear to be changing.
$this->assertEquals( | ||
array( | ||
(object) array( | ||
'Field' => 'id', |
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.
Shouldn't new_first_column
be first now?
'Extra' => '', | ||
), | ||
(object) array( | ||
'Field' => '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.
Shouldn't new_column
be after id
now, so the resulting column order would be:
- new_first_column
- id
- new_column
- name
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), |
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.
After this change, I think the resulting column order should be:
- id
- new_first_column
- new_column
- name
Am I missing something?
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), |
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.
After this change, I think the resulting column order should be:
- new_first_column
- new_column
- name
- id
Am I missing something?
'Default' => '', | ||
'Extra' => '', | ||
), | ||
), |
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.
After this change, shouldn't the resulting column order be the following?
- new2
- id
- new1
- new3
@brandonpayton Please, see the description of the PR. As it says, "SQLite doesn't support FIRST/AFTER, so this implementation simply ignores these". Actually implementing the order would mean changing simple ADD COLUMN rewriting to CHANGE-like copy-table behavior. |
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.
@brandonpayton Please, see the description of the PR. As it says, "SQLite doesn't support FIRST/AFTER, so this implementation simply ignores these".
🤦♂️ Right. I read that originally but lost the detail while multi-tasking.
These changes look reasonable to me and test well.
Add handling for adding/modifying columns with FIRST/AFTER, such as:
SQLite doesn't support FIRST/AFTER, so this implementation simply ignores these. Previously, for ADD COLUMN statements, it ended up with
SQLSTATE[HY000]: General error: 1 near "FIRST": syntax error.
andSQLSTATE[HY000]: General error: 1 near "AFTER": syntax error.
; for CHANGE statements, this was already ignored and I only added a test.