Skip to content

Commit 1064cf8

Browse files
KodaiDbrfrn169
andauthored
Support ifNotExists in add column (#2960)
Co-authored-by: Toshihiro Suzuki <[email protected]>
1 parent 7c80d80 commit 1064cf8

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

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

Lines changed: 38 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;
@@ -441,6 +442,43 @@ default void addNewColumnToTable(
441442
}
442443
}
443444

445+
/**
446+
* Adds a new column to an existing table. The new column cannot be a partition or clustering key.
447+
* <br>
448+
* See {@link #addNewColumnToTable(String, String, String, DataType)} for more information.
449+
*
450+
* @param namespace the table namespace
451+
* @param table the table name
452+
* @param columnName the name of the new column
453+
* @param columnType the type of the new column
454+
* @param encrypted whether the new column should be encrypted
455+
* @param ifNotExists if set to true, the column will be added only if it does not exist already.
456+
* If set to false, it will throw an exception if it already exists
457+
* @throws IllegalArgumentException if the table does not exist
458+
* @throws ExecutionException if the operation fails
459+
*/
460+
default void addNewColumnToTable(
461+
String namespace,
462+
String table,
463+
String columnName,
464+
DataType columnType,
465+
boolean encrypted,
466+
boolean ifNotExists)
467+
throws ExecutionException {
468+
if (encrypted) {
469+
throw new UnsupportedOperationException(CoreError.ENCRYPTION_NOT_ENABLED.buildMessage());
470+
}
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 (ifNotExists && tableMetadata.getColumnNames().contains(columnName)) {
477+
return;
478+
}
479+
addNewColumnToTable(namespace, table, columnName, columnType);
480+
}
481+
444482
/**
445483
* Imports an existing table that is not managed by ScalarDB.
446484
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,19 @@ public void addNewColumnToTable(
195195
namespace, table, columnName, columnType, encrypted);
196196
}
197197

198+
@Override
199+
public void addNewColumnToTable(
200+
String namespace,
201+
String table,
202+
String columnName,
203+
DataType columnType,
204+
boolean encrypted,
205+
boolean ifNotExists)
206+
throws ExecutionException {
207+
distributedTransactionAdmin.addNewColumnToTable(
208+
namespace, table, columnName, columnType, encrypted, ifNotExists);
209+
}
210+
198211
@Override
199212
public void importTable(String namespace, String table, Map<String, String> options)
200213
throws ExecutionException {

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,19 @@ public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgum
10101010
.isInstanceOf(IllegalArgumentException.class);
10111011
}
10121012

1013+
@Test
1014+
public void
1015+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
1016+
// Arrange
1017+
1018+
// Act Assert
1019+
assertThatCode(
1020+
() ->
1021+
admin.addNewColumnToTable(
1022+
namespace1, getTable1(), getColumnName7(), DataType.TEXT, false, true))
1023+
.doesNotThrowAnyException();
1024+
}
1025+
10131026
@Test
10141027
public void
10151028
upgrade_WhenMetadataTableExistsButNotNamespacesTable_ShouldCreateNamespacesTableAndImportExistingNamespaces()

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,19 @@ public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgum
886886
.isInstanceOf(IllegalArgumentException.class);
887887
}
888888

889+
@Test
890+
public void
891+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
892+
// Arrange
893+
894+
// Act Assert
895+
assertThatCode(
896+
() ->
897+
admin.addNewColumnToTable(
898+
namespace1, TABLE1, COL_NAME7, DataType.TEXT, false, true))
899+
.doesNotThrowAnyException();
900+
}
901+
889902
@Test
890903
public void createCoordinatorTables_ShouldCreateCoordinatorTablesCorrectly()
891904
throws ExecutionException {

0 commit comments

Comments
 (0)