Skip to content

Commit d2b4a97

Browse files
committed
Add drop column if exists
1 parent 6a6b4b9 commit d2b4a97

File tree

4 files changed

+72
-20
lines changed

4 files changed

+72
-20
lines changed

core/src/main/java/com/scalar/db/api/Admin.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.scalar.db.common.CoreError;
44
import com.scalar.db.exception.storage.ExecutionException;
55
import com.scalar.db.io.DataType;
6+
import com.scalar.db.util.ScalarDbUtils;
67
import java.util.Collections;
78
import java.util.Map;
89
import java.util.Set;
@@ -453,6 +454,31 @@ default void addNewColumnToTable(
453454
void dropColumnFromTable(String namespace, String table, String columnName)
454455
throws ExecutionException;
455456

457+
/**
458+
* Drops a column from an existing table. The column cannot be a partition or clustering key.
459+
*
460+
* @param namespace the table namespace
461+
* @param table the table name
462+
* @param columnName the name of the column to drop
463+
* @param IfExists if set to true, the column will be dropped only if it exists. If set to false,
464+
* it will throw an exception if it does not exist
465+
* @throws IllegalArgumentException if the table does not exist
466+
* @throws ExecutionException if the operation fails
467+
*/
468+
default void dropColumnFromTable(
469+
String namespace, String table, String columnName, boolean IfExists)
470+
throws ExecutionException {
471+
TableMetadata tableMetadata = getTableMetadata(namespace, table);
472+
if (tableMetadata == null) {
473+
throw new IllegalArgumentException(
474+
CoreError.TABLE_NOT_FOUND.buildMessage(ScalarDbUtils.getFullTableName(namespace, table)));
475+
}
476+
if (IfExists && !tableMetadata.getColumnNames().contains(columnName)) {
477+
return;
478+
}
479+
dropColumnFromTable(namespace, table, columnName);
480+
}
481+
456482
/**
457483
* Imports an existing table that is not managed by ScalarDB.
458484
*

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ public void dropColumnFromTable(String namespace, String table, String columnNam
201201
distributedTransactionAdmin.dropColumnFromTable(namespace, table, columnName);
202202
}
203203

204+
@Override
205+
public void dropColumnFromTable(
206+
String namespace, String table, String columnName, boolean ifExists)
207+
throws ExecutionException {
208+
distributedTransactionAdmin.dropColumnFromTable(namespace, table, columnName, ifExists);
209+
}
210+
204211
@Override
205212
public void importTable(String namespace, String table, Map<String, String> options)
206213
throws ExecutionException {

integration-test/src/main/java/com/scalar/db/api/DistributedStorageAdminIntegrationTestBase.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,16 @@ public void dropColumnFromTable_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArg
10991099
.isInstanceOf(IllegalArgumentException.class);
11001100
}
11011101

1102+
@Test
1103+
public void dropColumnFromTable_IfExists_ForNonExistingColumn_ShouldNotThrowAnyException() {
1104+
// Arrange
1105+
1106+
// Act Assert
1107+
assertThatCode(
1108+
() -> admin.dropColumnFromTable(namespace1, getTable1(), "nonExistingColumn", true))
1109+
.doesNotThrowAnyException();
1110+
}
1111+
11021112
@Test
11031113
public void
11041114
upgrade_WhenMetadataTableExistsButNotNamespacesTable_ShouldCreateNamespacesTableAndImportExistingNamespaces()

integration-test/src/main/java/com/scalar/db/api/DistributedTransactionAdminIntegrationTestBase.java

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -858,6 +858,26 @@ public void addNewColumnToTable_AddColumnForEachExistingDataType_ShouldAddNewCol
858858
}
859859
}
860860

861+
@Test
862+
public void addNewColumnToTable_ForNonExistingTable_ShouldThrowIllegalArgumentException() {
863+
// Arrange
864+
865+
// Act Assert
866+
assertThatThrownBy(
867+
() -> admin.addNewColumnToTable(namespace1, TABLE4, COL_NAME2, DataType.TEXT))
868+
.isInstanceOf(IllegalArgumentException.class);
869+
}
870+
871+
@Test
872+
public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgumentException() {
873+
// Arrange
874+
875+
// Act Assert
876+
assertThatThrownBy(
877+
() -> admin.addNewColumnToTable(namespace1, TABLE1, COL_NAME2, DataType.TEXT))
878+
.isInstanceOf(IllegalArgumentException.class);
879+
}
880+
861881
@Test
862882
public void dropColumnFromTable_DropColumnForEachExistingDataType_ShouldDropColumnsCorrectly()
863883
throws ExecutionException {
@@ -923,26 +943,6 @@ public void dropColumnFromTable_DropColumnForEachExistingDataType_ShouldDropColu
923943
}
924944
}
925945

926-
@Test
927-
public void addNewColumnToTable_ForNonExistingTable_ShouldThrowIllegalArgumentException() {
928-
// Arrange
929-
930-
// Act Assert
931-
assertThatThrownBy(
932-
() -> admin.addNewColumnToTable(namespace1, TABLE4, COL_NAME2, DataType.TEXT))
933-
.isInstanceOf(IllegalArgumentException.class);
934-
}
935-
936-
@Test
937-
public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgumentException() {
938-
// Arrange
939-
940-
// Act Assert
941-
assertThatThrownBy(
942-
() -> admin.addNewColumnToTable(namespace1, TABLE1, COL_NAME2, DataType.TEXT))
943-
.isInstanceOf(IllegalArgumentException.class);
944-
}
945-
946946
@Test
947947
public void dropColumnFromTable_ForNonExistingTable_ShouldThrowIllegalArgumentException() {
948948
// Arrange
@@ -974,6 +974,15 @@ public void dropColumnFromTable_ForPrimaryOrIndexKeyColumn_ShouldThrowIllegalArg
974974
.isInstanceOf(IllegalArgumentException.class);
975975
}
976976

977+
@Test
978+
public void dropColumnFromTable_IfNotExists_ForNonExistingColumn_ShouldNotThrowAnyException() {
979+
// Arrange
980+
981+
// Act Assert
982+
assertThatCode(() -> admin.dropColumnFromTable(namespace1, TABLE1, "nonExistingColumn", true))
983+
.doesNotThrowAnyException();
984+
}
985+
977986
@Test
978987
public void createCoordinatorTables_ShouldCreateCoordinatorTablesCorrectly()
979988
throws ExecutionException {

0 commit comments

Comments
 (0)