Skip to content

Commit

Permalink
support DROP COLUMN IF EXISTS
Browse files Browse the repository at this point in the history
  • Loading branch information
osheroff committed Sep 1, 2023
1 parent 4c1823e commit 8d7ca38
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/antlr4/imports/mysql_alter_table.g4
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ alter_specification:
add_column: ADD COLUMN? if_not_exists? column_definition col_position?;
add_column_parens: ADD COLUMN? if_not_exists? '(' (column_definition|index_definition) (',' (column_definition|index_definition))* ')';
change_column: CHANGE COLUMN? full_column_name column_definition col_position?;
drop_column: DROP COLUMN? full_column_name CASCADE?;
if_exists: IF EXISTS;
drop_column: DROP COLUMN? if_exists? full_column_name CASCADE?;
modify_column: MODIFY COLUMN? column_definition col_position?;
drop_key: DROP FOREIGN? (INDEX|KEY) name;
drop_primary_key: DROP PRIMARY KEY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void exitRename_column(Rename_columnContext ctx) {
@Override
public void exitDrop_column(mysqlParser.Drop_columnContext ctx) {
String colName = ctx.full_column_name().col_name.getText();
alterStatement().columnMods.add(new RemoveColumnMod(unquote(colName)));
alterStatement().columnMods.add(new RemoveColumnMod(unquote(colName), ctx.if_exists() != null));
}
@Override
public void exitCol_position(mysqlParser.Col_positionContext ctx) {
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/zendesk/maxwell/schema/ddl/RemoveColumnMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@
import java.util.List;

class RemoveColumnMod extends ColumnMod {
public RemoveColumnMod(String name) {
private final boolean ifExists;
public RemoveColumnMod(String name, boolean ifExists) {
super(name);
this.ifExists = ifExists;
}

@Override
public void apply(Table table, List<DeferredPositionUpdate> deferred) throws InvalidSchemaError {
table.removeColumn(originalIndex(table));
int originalIndex = table.findColumnIndex(name);

if ( originalIndex == -1 ) {
if ( ifExists )
return;
else
throw new InvalidSchemaError("Could not find column " + name + " in " + table.getName());
}

table.removeColumn(originalIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ public void testModifyAndMoveColumn() throws Exception {

}

@Test
public void testDropColumnIfExists() throws Exception {
String sql[] = {
"CREATE TABLE t ( a varchar(255), b int)",
"ALTER TABLE t drop column if exists aa",
"ALTER TABLE t drop column if exists b, drop column if exists nothere"
};
testIntegration(sql);

}

@Test
public void testAddQualifiedColumn() throws Exception {
MaxwellTestSupport.assertMaximumVersion(server, new MysqlVersion(8, 0));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ public void testParsingSomeAlters() {
"ALTER TABLE tournaments ADD INDEX idx_team_name (('$.teams.name'))",
"ALTER TABLE tournaments ADD INDEX idx_team_name ((ABS(col)))",
"ALTER TABLE tournaments ADD INDEX idx_team_name ((col1 * 40) DESC)",
"CREATE TABLE employees (data JSON, INDEX idx ((CAST(data->>'$.name' AS CHAR(30)) COLLATE utf8mb4_bin)))"
"CREATE TABLE employees (data JSON, INDEX idx ((CAST(data->>'$.name' AS CHAR(30)) COLLATE utf8mb4_bin)))",
"ALTER TABLE tasks DROP COLUMN IF EXISTS snoozed_until"
};

for ( String s : testSQL ) {
Expand Down

0 comments on commit 8d7ca38

Please sign in to comment.