Skip to content

Commit 086b446

Browse files
committed
Support ifNotExists in add column
1 parent a09a04a commit 086b446

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
@@ -1002,6 +1002,19 @@ public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgum
10021002
.isInstanceOf(IllegalArgumentException.class);
10031003
}
10041004

1005+
@Test
1006+
public void
1007+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
1008+
// Arrange
1009+
1010+
// Act Assert
1011+
assertThatCode(
1012+
() ->
1013+
admin.addNewColumnToTable(
1014+
namespace1, getTable1(), getColumnName7(), DataType.TEXT, false, true))
1015+
.doesNotThrowAnyException();
1016+
}
1017+
10051018
@Test
10061019
public void
10071020
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
@@ -878,6 +878,19 @@ public void addNewColumnToTable_ForAlreadyExistingColumn_ShouldThrowIllegalArgum
878878
.isInstanceOf(IllegalArgumentException.class);
879879
}
880880

881+
@Test
882+
public void
883+
addNewColumnToTable_IfNotExists_ForAlreadyExistingColumn_ShouldNotThrowAnyException() {
884+
// Arrange
885+
886+
// Act Assert
887+
assertThatCode(
888+
() ->
889+
admin.addNewColumnToTable(
890+
namespace1, TABLE1, COL_NAME7, DataType.TEXT, false, true))
891+
.doesNotThrowAnyException();
892+
}
893+
881894
@Test
882895
public void createCoordinatorTables_ShouldCreateCoordinatorTablesCorrectly()
883896
throws ExecutionException {

0 commit comments

Comments
 (0)