Skip to content

Commit f34db3c

Browse files
feeblefakieKodaiD
andauthored
Backport to branch(3) : Support ifNotExists in add column (#2991)
Co-authored-by: Kodai Doki <[email protected]>
1 parent 17cf8f1 commit f34db3c

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;
@@ -427,6 +428,43 @@ default void addNewColumnToTable(
427428
}
428429
}
429430

431+
/**
432+
* Adds a new column to an existing table. The new column cannot be a partition or clustering key.
433+
* <br>
434+
* See {@link #addNewColumnToTable(String, String, String, DataType)} for more information.
435+
*
436+
* @param namespace the table namespace
437+
* @param table the table name
438+
* @param columnName the name of the new column
439+
* @param columnType the type of the new column
440+
* @param encrypted whether the new column should be encrypted
441+
* @param ifNotExists if set to true, the column will be added only if it does not exist already.
442+
* If set to false, it will throw an exception if it already exists
443+
* @throws IllegalArgumentException if the table does not exist
444+
* @throws ExecutionException if the operation fails
445+
*/
446+
default void addNewColumnToTable(
447+
String namespace,
448+
String table,
449+
String columnName,
450+
DataType columnType,
451+
boolean encrypted,
452+
boolean ifNotExists)
453+
throws ExecutionException {
454+
if (encrypted) {
455+
throw new UnsupportedOperationException(CoreError.ENCRYPTION_NOT_ENABLED.buildMessage());
456+
}
457+
TableMetadata tableMetadata = getTableMetadata(namespace, table);
458+
if (tableMetadata == null) {
459+
throw new IllegalArgumentException(
460+
CoreError.TABLE_NOT_FOUND.buildMessage(ScalarDbUtils.getFullTableName(namespace, table)));
461+
}
462+
if (ifNotExists && tableMetadata.getColumnNames().contains(columnName)) {
463+
return;
464+
}
465+
addNewColumnToTable(namespace, table, columnName, columnType);
466+
}
467+
430468
/**
431469
* Imports an existing table that is not managed by ScalarDB.
432470
*

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,19 @@ public void addNewColumnToTable(
189189
namespace, table, columnName, columnType, encrypted);
190190
}
191191

192+
@Override
193+
public void addNewColumnToTable(
194+
String namespace,
195+
String table,
196+
String columnName,
197+
DataType columnType,
198+
boolean encrypted,
199+
boolean ifNotExists)
200+
throws ExecutionException {
201+
distributedTransactionAdmin.addNewColumnToTable(
202+
namespace, table, columnName, columnType, encrypted, ifNotExists);
203+
}
204+
192205
@Override
193206
public void importTable(String namespace, String table, Map<String, String> options)
194207
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
@@ -987,6 +987,19 @@ public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgum
987987
.isInstanceOf(IllegalArgumentException.class);
988988
}
989989

990+
@Test
991+
public void
992+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
993+
// Arrange
994+
995+
// Act Assert
996+
assertThatCode(
997+
() ->
998+
admin.addNewColumnToTable(
999+
namespace1, getTable1(), getColumnName7(), DataType.TEXT, false, true))
1000+
.doesNotThrowAnyException();
1001+
}
1002+
9901003
@Test
9911004
public void getNamespaceNames_ShouldReturnCreatedNamespaces() throws ExecutionException {
9921005
// Arrange

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
@@ -907,6 +907,19 @@ protected void extraCheckOnCoordinatorTable() throws ExecutionException {
907907
}
908908
}
909909

910+
@Test
911+
public void
912+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
913+
// Arrange
914+
915+
// Act Assert
916+
assertThatCode(
917+
() ->
918+
admin.addNewColumnToTable(
919+
namespace1, TABLE1, COL_NAME7, DataType.TEXT, false, true))
920+
.doesNotThrowAnyException();
921+
}
922+
910923
@Test
911924
public void createCoordinatorTables_ShouldCreateCoordinatorTablesCorrectly()
912925
throws ExecutionException {

0 commit comments

Comments
 (0)