Skip to content

Commit b82c7b3

Browse files
committed
[skip ci] fix
1 parent c33661f commit b82c7b3

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

core/src/main/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdmin.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ public void createTable(
129129
String dataTableName = table + TRANSACTION_METADATA_DECOUPLING_DATA_TABLE_SUFFIX;
130130
String txMetadataTableName = table + TRANSACTION_METADATA_DECOUPLING_METADATA_TABLE_SUFFIX;
131131

132+
if (admin.tableExists(namespace, dataTableName)) {
133+
throw new IllegalArgumentException(
134+
CoreError.TABLE_ALREADY_EXISTS.buildMessage(
135+
ScalarDbUtils.getFullTableName(namespace, dataTableName)));
136+
}
137+
if (admin.tableExists(namespace, txMetadataTableName)) {
138+
throw new IllegalArgumentException(
139+
CoreError.TABLE_ALREADY_EXISTS.buildMessage(
140+
ScalarDbUtils.getFullTableName(namespace, txMetadataTableName)));
141+
}
142+
132143
// Create a data table
133144
admin.createTable(namespace, dataTableName, metadata, options);
134145

@@ -406,6 +417,17 @@ public void importTable(
406417
String importedTableName = table + TRANSACTION_METADATA_DECOUPLING_IMPORTED_TABLE_SUFFIX;
407418
String txMetadataTableName = table + TRANSACTION_METADATA_DECOUPLING_METADATA_TABLE_SUFFIX;
408419

420+
if (admin.tableExists(namespace, txMetadataTableName)) {
421+
throw new IllegalArgumentException(
422+
CoreError.TABLE_ALREADY_EXISTS.buildMessage(
423+
ScalarDbUtils.getFullTableName(namespace, txMetadataTableName)));
424+
}
425+
if (admin.tableExists(namespace, importedTableName)) {
426+
throw new IllegalArgumentException(
427+
CoreError.TABLE_ALREADY_EXISTS.buildMessage(
428+
ScalarDbUtils.getFullTableName(namespace, importedTableName)));
429+
}
430+
409431
// import the original table as a data table
410432
admin.importTable(namespace, table, options, overrideColumnsType);
411433

core/src/test/java/com/scalar/db/transaction/consensuscommit/ConsensusCommitAdminTestBase.java

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,57 @@ public void coordinatorNamespaceShouldHandleCorrectly() throws ExecutionExceptio
941941
.isInstanceOf(IllegalArgumentException.class);
942942
}
943943

944+
@Test
945+
public void
946+
createTable_WithTransactionMetadataDecouplingEnabledAndDataTableAlreadyExists_ShouldThrowIllegalArgumentException()
947+
throws ExecutionException {
948+
// Arrange
949+
TableMetadata tableMetadata =
950+
TableMetadata.newBuilder()
951+
.addColumn("col1", DataType.INT)
952+
.addColumn("col2", DataType.INT)
953+
.addPartitionKey("col1")
954+
.build();
955+
956+
Map<String, String> options =
957+
ImmutableMap.of(ConsensusCommitAdmin.TRANSACTION_METADATA_DECOUPLING, "true");
958+
959+
StorageInfo storageInfo =
960+
new StorageInfoImpl("jdbc", MutationAtomicityUnit.STORAGE, Integer.MAX_VALUE, true);
961+
when(distributedStorageAdmin.getStorageInfo(NAMESPACE)).thenReturn(storageInfo);
962+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_data")).thenReturn(true);
963+
964+
// Act Assert
965+
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, tableMetadata, options))
966+
.isInstanceOf(IllegalArgumentException.class);
967+
}
968+
969+
@Test
970+
public void
971+
createTable_WithTransactionMetadataDecouplingEnabledAndMetadataTableAlreadyExists_ShouldThrowIllegalArgumentException()
972+
throws ExecutionException {
973+
// Arrange
974+
TableMetadata tableMetadata =
975+
TableMetadata.newBuilder()
976+
.addColumn("col1", DataType.INT)
977+
.addColumn("col2", DataType.INT)
978+
.addPartitionKey("col1")
979+
.build();
980+
981+
Map<String, String> options =
982+
ImmutableMap.of(ConsensusCommitAdmin.TRANSACTION_METADATA_DECOUPLING, "true");
983+
984+
StorageInfo storageInfo =
985+
new StorageInfoImpl("jdbc", MutationAtomicityUnit.STORAGE, Integer.MAX_VALUE, true);
986+
when(distributedStorageAdmin.getStorageInfo(NAMESPACE)).thenReturn(storageInfo);
987+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_data")).thenReturn(false);
988+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_tx_metadata")).thenReturn(true);
989+
990+
// Act Assert
991+
assertThatThrownBy(() -> admin.createTable(NAMESPACE, TABLE, tableMetadata, options))
992+
.isInstanceOf(IllegalArgumentException.class);
993+
}
994+
944995
@Test
945996
public void dropTable_ForVirtualTable_ShouldDropVirtualTableAndSourceTables()
946997
throws ExecutionException {
@@ -1138,4 +1189,43 @@ public VirtualTableJoinType getJoinType() {
11381189
assertThatThrownBy(() -> admin.importTable(NAMESPACE, TABLE, options, overrideColumnsType))
11391190
.isInstanceOf(IllegalArgumentException.class);
11401191
}
1192+
1193+
@Test
1194+
public void
1195+
importTable_WithTransactionMetadataDecouplingEnabledAndMetadataTableAlreadyExists_ShouldThrowIllegalArgumentException()
1196+
throws ExecutionException {
1197+
// Arrange
1198+
Map<String, String> options =
1199+
ImmutableMap.of(ConsensusCommitAdmin.TRANSACTION_METADATA_DECOUPLING, "true");
1200+
Map<String, DataType> overrideColumnsType = Collections.emptyMap();
1201+
1202+
StorageInfo storageInfo =
1203+
new StorageInfoImpl("jdbc", MutationAtomicityUnit.STORAGE, Integer.MAX_VALUE, true);
1204+
when(distributedStorageAdmin.getStorageInfo(NAMESPACE)).thenReturn(storageInfo);
1205+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_tx_metadata")).thenReturn(true);
1206+
1207+
// Act Assert
1208+
assertThatThrownBy(() -> admin.importTable(NAMESPACE, TABLE, options, overrideColumnsType))
1209+
.isInstanceOf(IllegalArgumentException.class);
1210+
}
1211+
1212+
@Test
1213+
public void
1214+
importTable_WithTransactionMetadataDecouplingEnabledAndImportedTableAlreadyExists_ShouldThrowIllegalArgumentException()
1215+
throws ExecutionException {
1216+
// Arrange
1217+
Map<String, String> options =
1218+
ImmutableMap.of(ConsensusCommitAdmin.TRANSACTION_METADATA_DECOUPLING, "true");
1219+
Map<String, DataType> overrideColumnsType = Collections.emptyMap();
1220+
1221+
StorageInfo storageInfo =
1222+
new StorageInfoImpl("jdbc", MutationAtomicityUnit.STORAGE, Integer.MAX_VALUE, true);
1223+
when(distributedStorageAdmin.getStorageInfo(NAMESPACE)).thenReturn(storageInfo);
1224+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_tx_metadata")).thenReturn(false);
1225+
when(distributedStorageAdmin.tableExists(NAMESPACE, TABLE + "_scalardb")).thenReturn(true);
1226+
1227+
// Act Assert
1228+
assertThatThrownBy(() -> admin.importTable(NAMESPACE, TABLE, options, overrideColumnsType))
1229+
.isInstanceOf(IllegalArgumentException.class);
1230+
}
11411231
}

0 commit comments

Comments
 (0)