Skip to content

Commit 96b13d3

Browse files
committed
Add an integration test case for all storages
1 parent 05b8493 commit 96b13d3

File tree

12 files changed

+111
-55
lines changed

12 files changed

+111
-55
lines changed

core/src/integration-test/java/com/scalar/db/storage/cassandra/CassandraAdminTestUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public void corruptMetadata(String namespace, String table) {
5757
// Do nothing
5858
}
5959

60+
@Override
61+
public void deleteMetadata(String namespace, String table) throws Exception {
62+
// Do nothing
63+
}
64+
6065
@Override
6166
public void dropNamespace(String namespace) {
6267
String dropKeyspaceQuery =

core/src/integration-test/java/com/scalar/db/storage/cosmos/CosmosAdminTestUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ public void corruptMetadata(String namespace, String table) {
8686
container.upsertItem(corruptedMetadata);
8787
}
8888

89+
@Override
90+
public void deleteMetadata(String namespace, String table) {
91+
String fullTableName = getFullTableName(namespace, table);
92+
CosmosContainer container =
93+
client.getDatabase(metadataDatabase).getContainer(CosmosAdmin.TABLE_METADATA_CONTAINER);
94+
container.deleteItem(
95+
fullTableName, new PartitionKey(fullTableName), new CosmosItemRequestOptions());
96+
}
97+
8998
/**
9099
* Retrieve the stored procedure for the given table
91100
*

core/src/integration-test/java/com/scalar/db/storage/dynamo/DynamoAdminTestUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,20 @@ public void corruptMetadata(String namespace, String table) {
171171
.build());
172172
}
173173

174+
@Override
175+
public void deleteMetadata(String namespace, String table) {
176+
String fullTableName =
177+
getFullTableName(Namespace.of(namespacePrefix, namespace).prefixed(), table);
178+
Map<String, AttributeValue> keyToDelete = new HashMap<>();
179+
keyToDelete.put("table", AttributeValue.builder().s(fullTableName).build());
180+
181+
client.deleteItem(
182+
DeleteItemRequest.builder()
183+
.tableName(getFullTableName(metadataNamespace, DynamoAdmin.METADATA_TABLE))
184+
.key(keyToDelete)
185+
.build());
186+
}
187+
174188
@Override
175189
public void dropTable(String nonPrefixedNamespace, String table) {
176190
String namespace = Namespace.of(namespacePrefix, nonPrefixedNamespace).prefixed();

core/src/integration-test/java/com/scalar/db/storage/jdbc/ConsensusCommitAdminImportTableIntegrationTestWithJdbcDatabase.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.scalar.db.api.DistributedStorageAdminImportTableIntegrationTestBase.TestData;
44
import com.scalar.db.exception.storage.ExecutionException;
55
import com.scalar.db.transaction.consensuscommit.ConsensusCommitAdminImportTableIntegrationTestBase;
6-
import com.scalar.db.util.AdminTestUtils;
76
import java.sql.SQLException;
87
import java.util.List;
98
import java.util.Properties;
@@ -54,11 +53,6 @@ protected void dropNonImportableTable(String table) throws SQLException {
5453
testUtils.dropTable(getNamespace(), table);
5554
}
5655

57-
@Override
58-
protected AdminTestUtils getAdminTestUtils(String testName) {
59-
return new JdbcAdminTestUtils(getProperties(testName));
60-
}
61-
6256
@SuppressWarnings("unused")
6357
private boolean isSqlite() {
6458
return JdbcEnv.isSqlite();
@@ -78,9 +72,4 @@ public void importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationEx
7872
throws ExecutionException {
7973
super.importTable_ForUnsupportedDatabase_ShouldThrowUnsupportedOperationException();
8074
}
81-
82-
@Test
83-
@Override
84-
@DisabledIf("isSqlite")
85-
public void dropNamespace_ShouldNotDropNonScalarDBTables() {}
8675
}

core/src/integration-test/java/com/scalar/db/storage/jdbc/JdbcAdminTestUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ public void corruptMetadata(String namespace, String table) throws Exception {
6464
execute(insertCorruptedMetadataStatement);
6565
}
6666

67+
@Override
68+
public void deleteMetadata(String namespace, String table) throws Exception {
69+
String deleteMetadataStatement =
70+
"DELETE FROM "
71+
+ rdbEngine.encloseFullTableName(metadataSchema, JdbcAdmin.METADATA_TABLE)
72+
+ " WHERE "
73+
+ rdbEngine.enclose(JdbcAdmin.METADATA_COL_FULL_TABLE_NAME)
74+
+ " = '"
75+
+ getFullTableName(namespace, table)
76+
+ "'";
77+
execute(deleteMetadataStatement);
78+
}
79+
6780
private void execute(String sql) throws SQLException {
6881
try (Connection connection = dataSource.getConnection()) {
6982
JdbcAdmin.execute(connection, sql);

core/src/integration-test/java/com/scalar/db/storage/multistorage/MultiStorageAdminTestUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,22 @@ public void corruptMetadata(String namespace, String table) throws Exception {
116116
execute(insertCorruptedMetadataStatement);
117117
}
118118

119+
@Override
120+
public void deleteMetadata(String namespace, String table) throws Exception {
121+
// Do nothing for Cassandra
122+
123+
// for JDBC
124+
String deleteMetadataStatement =
125+
"DELETE FROM "
126+
+ rdbEngine.encloseFullTableName(jdbcMetadataSchema, JdbcAdmin.METADATA_TABLE)
127+
+ " WHERE "
128+
+ rdbEngine.enclose(JdbcAdmin.METADATA_COL_FULL_TABLE_NAME)
129+
+ " = '"
130+
+ getFullTableName(namespace, table)
131+
+ "'";
132+
execute(deleteMetadataStatement);
133+
}
134+
119135
@Override
120136
public void dropNamespace(String namespace) throws SQLException {
121137
boolean existsOnCassandra = namespaceExistsOnCassandra(namespace);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,12 @@ public enum CoreError implements ScalarDbError {
808808
"Db2 does not support column type conversion from %s to %s",
809809
"",
810810
""),
811+
NAMESPACE_WITH_NON_SCALARDB_TABLES_CANNOT_BE_DROPPED(
812+
Category.USER_ERROR,
813+
"0241",
814+
"The namespace has non-ScalarDB tables and cannot be dropped. Namespace: %s; Tables in the namespace: %s",
815+
"",
816+
""),
811817

812818
//
813819
// Errors for the concurrency error category

core/src/main/java/com/scalar/db/storage/cosmos/CosmosAdmin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,12 @@ private void deleteTableMetadata(String namespace, String table) throws Executio
344344
@Override
345345
public void dropNamespace(String namespace) throws ExecutionException {
346346
try {
347+
Set<String> remainingTables = getNamespaceTableNamesInternal(namespace);
348+
if (!remainingTables.isEmpty()) {
349+
throw new IllegalArgumentException(
350+
CoreError.NAMESPACE_WITH_NON_SCALARDB_TABLES_CANNOT_BE_DROPPED.buildMessage(
351+
namespace, remainingTables));
352+
}
347353
client.getDatabase(namespace).delete();
348354
getNamespacesContainer()
349355
.deleteItem(new CosmosNamespace(namespace), new CosmosItemRequestOptions());
@@ -785,4 +791,10 @@ private boolean containerExists(String databaseId, String containerId) throws Co
785791
public StorageInfo getStorageInfo(String namespace) {
786792
return STORAGE_INFO;
787793
}
794+
795+
private Set<String> getNamespaceTableNamesInternal(String namespace) {
796+
return client.getDatabase(namespace).readAllContainers().stream()
797+
.map(CosmosContainerProperties::getId)
798+
.collect(Collectors.toSet());
799+
}
788800
}

core/src/main/java/com/scalar/db/storage/jdbc/JdbcAdmin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
public class JdbcAdmin implements DistributedStorageAdmin {
4646
public static final String METADATA_TABLE = "metadata";
4747
public static final String NAMESPACES_TABLE = "namespaces";
48-
@VisibleForTesting static final String METADATA_COL_FULL_TABLE_NAME = "full_table_name";
48+
@VisibleForTesting public static final String METADATA_COL_FULL_TABLE_NAME = "full_table_name";
4949
@VisibleForTesting static final String METADATA_COL_COLUMN_NAME = "column_name";
5050
@VisibleForTesting static final String METADATA_COL_DATA_TYPE = "data_type";
5151
@VisibleForTesting static final String METADATA_COL_KEY_TYPE = "key_type";
@@ -483,8 +483,9 @@ public void dropNamespace(String namespace) throws ExecutionException {
483483
try (Connection connection = dataSource.getConnection()) {
484484
Set<String> remainingTables = getNamespaceTableNamesInternal(connection, namespace);
485485
if (!remainingTables.isEmpty()) {
486-
throw new IllegalStateException(
487-
CoreError.NAMESPACE_NOT_EMPTY.buildMessage(namespace, remainingTables));
486+
throw new IllegalArgumentException(
487+
CoreError.NAMESPACE_WITH_NON_SCALARDB_TABLES_CANNOT_BE_DROPPED.buildMessage(
488+
namespace, remainingTables));
488489
}
489490
execute(connection, rdbEngine.dropNamespaceSql(namespace));
490491
deleteFromNamespacesTable(connection, namespace);

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,29 @@ public void dropNamespace_ForNonEmptyNamespace_ShouldThrowIllegalArgumentExcepti
423423
}
424424
}
425425

426+
@Test
427+
public void
428+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException()
429+
throws Exception {
430+
AdminTestUtils adminTestUtils = getAdminTestUtils(getTestName());
431+
String nonManagedTable = "non_managed_table";
432+
try {
433+
// Arrange
434+
admin.createNamespace(namespace3, getCreationOptions());
435+
admin.createTable(namespace3, nonManagedTable, getTableMetadata(), getCreationOptions());
436+
adminTestUtils.deleteMetadata(namespace3, nonManagedTable);
437+
438+
// Act Assert
439+
assertThatThrownBy(() -> admin.dropNamespace(namespace3))
440+
.isInstanceOf(IllegalArgumentException.class);
441+
} finally {
442+
adminTestUtils.dropTable(namespace3, nonManagedTable);
443+
admin.dropNamespace(namespace3, true);
444+
445+
adminTestUtils.close();
446+
}
447+
}
448+
426449
@Test
427450
public void dropNamespace_IfExists_ForNonExistingNamespace_ShouldNotThrowAnyException() {
428451
// Arrange

0 commit comments

Comments
 (0)