Skip to content
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

Fix a 'recreate compound primary key' issue #117

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,7 @@ private AlterTablePreparedData prepareAlterTableData(String tableName) {
}
} else if (alterItem instanceof SqlDropPrimaryKey) {
primaryKeyDropped = true;
droppedIndexes.add("PRIMARY");
} else if (alterItem instanceof SqlAddPrimaryKey) {
SqlAddPrimaryKey addPrimaryKey = (SqlAddPrimaryKey) alterItem;
for (SqlIndexColumnName indexColumnName : addPrimaryKey.getColumns()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,33 @@ public void testAlterTableDropAddPrimaryKey() throws SQLException {
dropTableIfExists(tableName);
}

@Test
public void testAlterTableDropAddCompPrimaryKey() {
if (TStringUtil.isNotEmpty(schemaPrefix)) {
return;
}

String tableName = "drop_add_comp_pk";

dropTableIfExists(tableName);

String sql = "CREATE TABLE " + tableName + " (\n"
+ "col1 varchar(255) DEFAULT NULL,\n"
+ "col2 varchar(255) NOT NULL,\n"
+ "id int(11) NOT NULL AUTO_INCREMENT,\n"
+ "PRIMARY KEY (id,col2)\n"
+ ") ENGINE = InnoDB DEFAULT CHARSET = utf8";
JdbcUtil.executeUpdateSuccess(tddlConnection, String.format(sql, tableName));

sql = "ALTER TABLE " + tableName + "\n"
+ "DROP PRIMARY KEY,\n"
+ "DROP COLUMN col2,\n"
+ "ADD PRIMARY KEY (id) USING BTREE";
JdbcUtil.executeUpdateSuccess(tddlConnection, String.format(sql, tableName));

dropTableIfExists(tableName);
}

@Test
public void testAlterTableDropKey() throws SQLException {
if (TStringUtil.isNotEmpty(schemaPrefix)) {
Expand Down