Skip to content

Commit 34c5abf

Browse files
feeblefakieKodaiD
andauthored
Backport to branch(3) : Fix to avoid deleting non ScalarDB tables when dropping namespaces (#3101)
Co-authored-by: Kodai Doki <[email protected]> Co-authored-by: Kodai Doki <[email protected]>
1 parent f2c8b23 commit 34c5abf

27 files changed

+425
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.scalar.db.config.DatabaseConfig;
88
import com.scalar.db.exception.storage.ExecutionException;
99
import com.scalar.db.io.DataType;
10+
import com.scalar.db.util.AdminTestUtils;
1011
import java.util.Map;
1112
import java.util.Properties;
1213
import org.junit.jupiter.api.Disabled;
@@ -25,6 +26,11 @@ protected String getSystemNamespaceName(Properties properties) {
2526
.orElse(DatabaseConfig.DEFAULT_SYSTEM_NAMESPACE_NAME);
2627
}
2728

29+
@Override
30+
protected AdminTestUtils getAdminTestUtils(String testName) {
31+
return new CassandraAdminTestUtils(getProperties(testName));
32+
}
33+
2834
@Override
2935
protected boolean isTimestampTypeSupported() {
3036
return false;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.scalar.db.config.DatabaseConfig;
88
import com.scalar.db.exception.storage.ExecutionException;
99
import com.scalar.db.io.DataType;
10+
import com.scalar.db.util.AdminTestUtils;
1011
import java.util.Map;
1112
import java.util.Properties;
1213
import org.junit.jupiter.api.Disabled;
@@ -24,6 +25,11 @@ protected String getSystemNamespaceName(Properties properties) {
2425
.orElse(DatabaseConfig.DEFAULT_SYSTEM_NAMESPACE_NAME);
2526
}
2627

28+
@Override
29+
protected AdminTestUtils getAdminTestUtils(String testName) {
30+
return new CassandraAdminTestUtils(getProperties(testName));
31+
}
32+
2733
@Override
2834
protected boolean isTimestampTypeSupported() {
2935
return false;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.scalar.db.storage.cassandra;
22

3+
import static com.datastax.driver.core.Metadata.quoteIfNecessary;
4+
5+
import com.datastax.driver.core.schemabuilder.SchemaBuilder;
36
import com.scalar.db.config.DatabaseConfig;
47
import com.scalar.db.util.AdminTestUtils;
58
import java.util.Properties;
@@ -28,6 +31,11 @@ public void corruptMetadata(String namespace, String table) {
2831
// Do nothing
2932
}
3033

34+
@Override
35+
public void deleteMetadata(String namespace, String table) throws Exception {
36+
// Do nothing
37+
}
38+
3139
@Override
3240
public boolean namespaceExists(String namespace) {
3341
return clusterManager.getSession().getCluster().getMetadata().getKeyspace(namespace) != null;
@@ -38,6 +46,14 @@ public boolean tableExists(String namespace, String table) {
3846
return clusterManager.getMetadata(namespace, table) != null;
3947
}
4048

49+
@Override
50+
public void dropTable(String namespace, String table) {
51+
String dropTableQuery =
52+
SchemaBuilder.dropTable(quoteIfNecessary(namespace), quoteIfNecessary(table))
53+
.getQueryString();
54+
clusterManager.getSession().execute(dropTableQuery);
55+
}
56+
4157
@Override
4258
public void close() {
4359
clusterManager.close();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.scalar.db.api.DistributedStorageAdminCaseSensitivityIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
5+
import com.scalar.db.util.AdminTestUtils;
56
import java.util.Map;
67
import java.util.Properties;
78
import org.junit.jupiter.api.Disabled;
@@ -19,6 +20,11 @@ protected Map<String, String> getCreationOptions() {
1920
return CosmosEnv.getCreationOptions();
2021
}
2122

23+
@Override
24+
protected AdminTestUtils getAdminTestUtils(String testName) {
25+
return new CosmosAdminTestUtils(getProperties(testName));
26+
}
27+
2228
@Override
2329
protected String getSystemNamespaceName(Properties properties) {
2430
return new CosmosConfig(new DatabaseConfig(properties))

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.scalar.db.api.DistributedStorageAdminIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
5+
import com.scalar.db.util.AdminTestUtils;
56
import java.util.Map;
67
import java.util.Properties;
78
import org.junit.jupiter.api.Disabled;
@@ -18,6 +19,11 @@ protected Map<String, String> getCreationOptions() {
1819
return CosmosEnv.getCreationOptions();
1920
}
2021

22+
@Override
23+
protected AdminTestUtils getAdminTestUtils(String testName) {
24+
return new CosmosAdminTestUtils(getProperties(testName));
25+
}
26+
2127
@Override
2228
protected String getSystemNamespaceName(Properties properties) {
2329
return new CosmosConfig(new DatabaseConfig(properties))

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public void corruptMetadata(String namespace, String table) {
7676
container.upsertItem(corruptedMetadata);
7777
}
7878

79+
@Override
80+
public void deleteMetadata(String namespace, String table) {
81+
String fullTableName = getFullTableName(namespace, table);
82+
CosmosContainer container =
83+
client.getDatabase(metadataDatabase).getContainer(CosmosAdmin.METADATA_CONTAINER);
84+
container.deleteItem(
85+
fullTableName, new PartitionKey(fullTableName), new CosmosItemRequestOptions());
86+
}
87+
7988
/**
8089
* Retrieve the stored procedure for the given table
8190
*
@@ -117,6 +126,11 @@ public boolean tableExists(String namespace, String table) {
117126
return true;
118127
}
119128

129+
@Override
130+
public void dropTable(String namespace, String table) {
131+
client.getDatabase(namespace).getContainer(table).delete();
132+
}
133+
120134
@Override
121135
public void close() {
122136
client.close();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.scalar.db.api.DistributedStorageAdminCaseSensitivityIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
5+
import com.scalar.db.util.AdminTestUtils;
56
import java.util.Map;
67
import java.util.Properties;
78
import org.junit.jupiter.api.Disabled;
@@ -20,6 +21,11 @@ protected Map<String, String> getCreationOptions() {
2021
return DynamoEnv.getCreationOptions();
2122
}
2223

24+
@Override
25+
protected AdminTestUtils getAdminTestUtils(String testName) {
26+
return new DynamoAdminTestUtils(getProperties(testName));
27+
}
28+
2329
@Override
2430
protected boolean isIndexOnBooleanColumnSupported() {
2531
return false;
@@ -80,6 +86,11 @@ public void namespaceExists_ShouldReturnCorrectResults() {}
8086
@Override
8187
public void createTable_ForNonExistingNamespace_ShouldThrowIllegalArgumentException() {}
8288

89+
@Override
90+
@Disabled("DynamoDB does not have a concept of namespaces")
91+
public void
92+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
93+
8394
@Override
8495
@Disabled("DynamoDB does not support dropping columns")
8596
public void dropColumnFromTable_DropColumnForEachExistingDataType_ShouldDropColumnsCorrectly() {}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.scalar.db.api.DistributedStorageAdminIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
5+
import com.scalar.db.util.AdminTestUtils;
56
import java.util.Map;
67
import java.util.Properties;
78
import org.junit.jupiter.api.Disabled;
@@ -19,6 +20,11 @@ protected Map<String, String> getCreationOptions() {
1920
return DynamoEnv.getCreationOptions();
2021
}
2122

23+
@Override
24+
protected AdminTestUtils getAdminTestUtils(String testName) {
25+
return new DynamoAdminTestUtils(getProperties(testName));
26+
}
27+
2228
@Override
2329
protected boolean isIndexOnBooleanColumnSupported() {
2430
return false;
@@ -79,6 +85,11 @@ public void namespaceExists_ShouldReturnCorrectResults() {}
7985
@Override
8086
public void createTable_ForNonExistingNamespace_ShouldThrowIllegalArgumentException() {}
8187

88+
@Override
89+
@Disabled("DynamoDB does not have a concept of namespaces")
90+
public void
91+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
92+
8293
@Override
8394
@Disabled("DynamoDB does not support dropping columns")
8495
public void dropColumnFromTable_DropColumnForEachExistingDataType_ShouldDropColumnsCorrectly() {}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ public boolean tableExists(String nonPrefixedNamespace, String table) {
9393
}
9494
}
9595

96+
@Override
97+
public void dropTable(String nonPrefixedNamespace, String table) {
98+
String namespace = Namespace.of(namespacePrefix, nonPrefixedNamespace).prefixed();
99+
client.deleteTable(
100+
DeleteTableRequest.builder().tableName(getFullTableName(namespace, table)).build());
101+
if (!waitForTableDeletion(namespace, table)) {
102+
throw new RuntimeException(
103+
String.format("Deleting the %s table timed out", getFullTableName(namespace, table)));
104+
}
105+
}
106+
96107
@Override
97108
public void truncateMetadataTable() {
98109
Map<String, AttributeValue> lastKeyEvaluated = null;
@@ -139,6 +150,20 @@ public void corruptMetadata(String namespace, String table) {
139150
.build());
140151
}
141152

153+
@Override
154+
public void deleteMetadata(String namespace, String table) {
155+
String fullTableName =
156+
getFullTableName(Namespace.of(namespacePrefix, namespace).prefixed(), table);
157+
Map<String, AttributeValue> keyToDelete = new HashMap<>();
158+
keyToDelete.put("table", AttributeValue.builder().s(fullTableName).build());
159+
160+
client.deleteItem(
161+
DeleteItemRequest.builder()
162+
.tableName(getFullTableName(metadataNamespace, DynamoAdmin.METADATA_TABLE))
163+
.key(keyToDelete)
164+
.build());
165+
}
166+
142167
@Override
143168
public boolean namespaceExists(String namespace) throws Exception {
144169
// Dynamo has no concept of namespace

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.scalar.db.exception.storage.ExecutionException;
1717
import com.scalar.db.io.DataType;
1818
import com.scalar.db.io.Key;
19+
import com.scalar.db.util.AdminTestUtils;
1920
import java.io.IOException;
2021
import java.nio.charset.StandardCharsets;
2122
import java.time.Instant;
@@ -49,6 +50,11 @@ protected String getSystemNamespaceName(Properties properties) {
4950
.orElse(DatabaseConfig.DEFAULT_SYSTEM_NAMESPACE_NAME);
5051
}
5152

53+
@Override
54+
protected AdminTestUtils getAdminTestUtils(String testName) {
55+
return new JdbcAdminTestUtils(getProperties(testName));
56+
}
57+
5258
// Since SQLite doesn't have persistent namespaces, some behaviors around the namespace are
5359
// different from the other adapters. So disable several tests that check such behaviors.
5460

@@ -159,6 +165,12 @@ private boolean isWideningColumnTypeConversionNotFullySupported() {
159165
return JdbcTestUtils.isOracle(rdbEngine) || JdbcTestUtils.isSqlite(rdbEngine);
160166
}
161167

168+
@Test
169+
@Override
170+
@DisabledIf("isSqlite")
171+
public void
172+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
173+
162174
@Test
163175
@Override
164176
@DisabledIf("isDb2")

0 commit comments

Comments
 (0)