Skip to content

Commit a63f5d8

Browse files
committed
Fix
1 parent 730a763 commit a63f5d8

20 files changed

+308
-69
lines changed

core/src/integration-test/java/com/scalar/db/storage/cosmos/ConsensusCommitAdminIntegrationTestWithCosmos.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6262

6363
@Override
6464
@Disabled("Cosmos DB does not support renaming columns")
65-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
65+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
66+
67+
@Override
68+
@Disabled("Cosmos DB does not support renaming columns")
69+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
6670
}

core/src/integration-test/java/com/scalar/db/storage/cosmos/CosmosAdminCaseSensitivityIntegrationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6262

6363
@Override
6464
@Disabled("Cosmos DB does not support renaming columns")
65-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
65+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
66+
67+
@Override
68+
@Disabled("Cosmos DB does not support renaming columns")
69+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
6670
}

core/src/integration-test/java/com/scalar/db/storage/cosmos/CosmosAdminIntegrationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6161

6262
@Override
6363
@Disabled("Cosmos DB does not support renaming columns")
64-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
64+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
65+
66+
@Override
67+
@Disabled("Cosmos DB does not support renaming columns")
68+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
6569
}

core/src/integration-test/java/com/scalar/db/storage/cosmos/SingleCrudOperationTransactionAdminIntegrationTestWithCosmos.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
5656

5757
@Override
5858
@Disabled("Cosmos DB does not support renaming columns")
59-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
59+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
60+
61+
@Override
62+
@Disabled("Cosmos DB does not support renaming columns")
63+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
6064
}

core/src/integration-test/java/com/scalar/db/storage/dynamo/ConsensusCommitAdminIntegrationTestWithDynamo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6767

6868
@Override
6969
@Disabled("DynamoDB does not support renaming columns")
70-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
70+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
71+
72+
@Override
73+
@Disabled("DynamoDB does not support renaming columns")
74+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
7175
}

core/src/integration-test/java/com/scalar/db/storage/dynamo/DynamoAdminCaseSensitivityIntegrationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6767

6868
@Override
6969
@Disabled("DynamoDB does not support renaming columns")
70-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
70+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
71+
72+
@Override
73+
@Disabled("DynamoDB does not support renaming columns")
74+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
7175
}

core/src/integration-test/java/com/scalar/db/storage/dynamo/DynamoAdminIntegrationTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6666

6767
@Override
6868
@Disabled("DynamoDB does not support renaming columns")
69-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
69+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
70+
71+
@Override
72+
@Disabled("DynamoDB does not support renaming columns")
73+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
7074
}

core/src/integration-test/java/com/scalar/db/storage/dynamo/SingleCrudOperationTransactionAdminIntegrationTestWithDynamo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,9 @@ public void renameColumn_ForNonExistingColumn_ShouldThrowIllegalArgumentExceptio
6161

6262
@Override
6363
@Disabled("DynamoDB does not support renaming columns")
64-
public void renameColumn_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArgumentException() {}
64+
public void renameColumn_ForPrimaryKeyColumn_ShouldRenameColumnCorrectly() {}
65+
66+
@Override
67+
@Disabled("DynamoDB does not support renaming columns")
68+
public void renameColumn_ForIndexKeyColumn_ShouldRenameColumnAndIndexCorrectly() {}
6569
}

core/src/main/java/com/scalar/db/common/CheckedDistributedStorageAdmin.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,14 +353,6 @@ public void renameColumn(
353353
ScalarDbUtils.getFullTableName(namespace, table), newColumnName));
354354
}
355355

356-
if (tableMetadata.getPartitionKeyNames().contains(oldColumnName)
357-
|| tableMetadata.getClusteringKeyNames().contains(oldColumnName)
358-
|| tableMetadata.getSecondaryIndexNames().contains(oldColumnName)) {
359-
throw new IllegalArgumentException(
360-
CoreError.RENAME_PRIMARY_KEY_COLUMN_NOT_SUPPORTED.buildMessage(
361-
ScalarDbUtils.getFullTableName(namespace, table), oldColumnName));
362-
}
363-
364356
try {
365357
admin.renameColumn(namespace, table, oldColumnName, newColumnName);
366358
} catch (ExecutionException e) {

core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -891,15 +891,34 @@ public void renameColumn(
891891
try {
892892
TableMetadata currentTableMetadata = getTableMetadata(namespace, table);
893893
DataType columnType = currentTableMetadata.getColumnDataType(oldColumnName);
894-
TableMetadata updatedTableMetadata =
894+
TableMetadata.Builder tableMetadataBuilder =
895895
TableMetadata.newBuilder(currentTableMetadata)
896896
.removeColumn(oldColumnName)
897-
.addColumn(newColumnName, columnType)
898-
.build();
897+
.addColumn(newColumnName, columnType);
898+
if (currentTableMetadata.getPartitionKeyNames().contains(oldColumnName)) {
899+
tableMetadataBuilder.removePartitionKey(oldColumnName);
900+
tableMetadataBuilder.addPartitionKey(newColumnName);
901+
} else if (currentTableMetadata.getClusteringKeyNames().contains(oldColumnName)) {
902+
Ordering.Order order = currentTableMetadata.getClusteringOrder(oldColumnName);
903+
tableMetadataBuilder.removeClusteringKey(oldColumnName);
904+
tableMetadataBuilder.addClusteringKey(newColumnName, order);
905+
} else if (currentTableMetadata.getSecondaryIndexNames().contains(oldColumnName)) {
906+
tableMetadataBuilder.removeSecondaryIndex(oldColumnName);
907+
tableMetadataBuilder.addSecondaryIndex(newColumnName);
908+
}
909+
TableMetadata updatedTableMetadata = tableMetadataBuilder.build();
899910
String renameColumnStatement =
900911
rdbEngine.renameColumnSql(namespace, table, oldColumnName, newColumnName);
901912
try (Connection connection = dataSource.getConnection()) {
902913
execute(connection, renameColumnStatement);
914+
if (currentTableMetadata.getSecondaryIndexNames().contains(oldColumnName)) {
915+
if (rdbEngine instanceof RdbEngineSqlite) {
916+
dropIndex(namespace, table, oldColumnName);
917+
createIndex(connection, namespace, table, newColumnName, false);
918+
} else {
919+
renameIndex(connection, namespace, table, oldColumnName, newColumnName);
920+
}
921+
}
903922
addTableMetadata(connection, namespace, table, updatedTableMetadata, false, true);
904923
}
905924
} catch (SQLException e) {
@@ -968,6 +987,19 @@ private void dropIndex(Connection connection, String schema, String table, Strin
968987
execute(connection, sql);
969988
}
970989

990+
private void renameIndex(
991+
Connection connection,
992+
String schema,
993+
String table,
994+
String oldIndexedColumn,
995+
String newIndexedColumn)
996+
throws SQLException {
997+
String oldIndexName = getIndexName(schema, table, oldIndexedColumn);
998+
String newIndexName = getIndexName(schema, table, newIndexedColumn);
999+
String sql = rdbEngine.renameIndexSql(schema, table, oldIndexName, newIndexName);
1000+
execute(connection, sql);
1001+
}
1002+
9711003
private String getIndexName(String schema, String table, String indexedColumn) {
9721004
return String.join("_", INDEX_NAME_PREFIX, schema, table, indexedColumn);
9731005
}

0 commit comments

Comments
 (0)