Skip to content

Commit 4c67801

Browse files
feeblefakieKodaiD
andauthored
Backport to branch(3.16) : Fix to avoid deleting non ScalarDB tables when dropping namespaces (#3097)
Co-authored-by: Kodai Doki <52027276+KodaiD@users.noreply.github.com>
1 parent 89b23a2 commit 4c67801

27 files changed

+427
-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
@@ -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.Properties;
67

78
public class CassandraAdminCaseSensitivityIntegrationTest
@@ -18,6 +19,11 @@ protected String getSystemNamespaceName(Properties properties) {
1819
.orElse(DatabaseConfig.DEFAULT_SYSTEM_NAMESPACE_NAME);
1920
}
2021

22+
@Override
23+
protected AdminTestUtils getAdminTestUtils(String testName) {
24+
return new CassandraAdminTestUtils(getProperties(testName));
25+
}
26+
2127
@Override
2228
protected boolean isTimestampTypeSupported() {
2329
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
@@ -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.Properties;
67

78
public class CassandraAdminIntegrationTest extends DistributedStorageAdminIntegrationTestBase {
@@ -17,6 +18,11 @@ protected String getSystemNamespaceName(Properties properties) {
1718
.orElse(DatabaseConfig.DEFAULT_SYSTEM_NAMESPACE_NAME);
1819
}
1920

21+
@Override
22+
protected AdminTestUtils getAdminTestUtils(String testName) {
23+
return new CassandraAdminTestUtils(getProperties(testName));
24+
}
25+
2026
@Override
2127
protected boolean isTimestampTypeSupported() {
2228
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

@@ -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/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

@@ -17,6 +18,11 @@ protected Map<String, String> getCreationOptions() {
1718
return CosmosEnv.getCreationOptions();
1819
}
1920

21+
@Override
22+
protected AdminTestUtils getAdminTestUtils(String testName) {
23+
return new CosmosAdminTestUtils(getProperties(testName));
24+
}
25+
2026
@Override
2127
protected String getSystemNamespaceName(Properties properties) {
2228
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: 12 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;
@@ -25,6 +26,11 @@ protected boolean isIndexOnBooleanColumnSupported() {
2526
return false;
2627
}
2728

29+
@Override
30+
protected AdminTestUtils getAdminTestUtils(String testName) {
31+
return new DynamoAdminTestUtils(getProperties(testName));
32+
}
33+
2834
@Override
2935
protected String getSystemNamespaceName(Properties properties) {
3036
return new DynamoConfig(new DatabaseConfig(properties))
@@ -35,6 +41,12 @@ protected String getSystemNamespaceName(Properties properties) {
3541
// Since DynamoDB doesn't have the namespace concept, some behaviors around the namespace are
3642
// different from the other adapters. So disable several tests that check such behaviors
3743

44+
@Disabled
45+
@Test
46+
@Override
47+
public void
48+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
49+
3850
@Disabled
3951
@Test
4052
@Override

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

Lines changed: 12 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;
@@ -24,6 +25,11 @@ protected boolean isIndexOnBooleanColumnSupported() {
2425
return false;
2526
}
2627

28+
@Override
29+
protected AdminTestUtils getAdminTestUtils(String testName) {
30+
return new DynamoAdminTestUtils(getProperties(testName));
31+
}
32+
2733
@Override
2834
protected String getSystemNamespaceName(Properties properties) {
2935
return new DynamoConfig(new DatabaseConfig(properties))
@@ -34,6 +40,12 @@ protected String getSystemNamespaceName(Properties properties) {
3440
// Since DynamoDB doesn't have the namespace concept, some behaviors around the namespace are
3541
// different from the other adapters. So disable several tests that check such behaviors
3642

43+
@Disabled
44+
@Test
45+
@Override
46+
public void
47+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
48+
3749
@Disabled
3850
@Test
3951
@Override

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
@@ -139,6 +139,31 @@ public void corruptMetadata(String namespace, String table) {
139139
.build());
140140
}
141141

142+
@Override
143+
public void deleteMetadata(String namespace, String table) {
144+
String fullTableName =
145+
getFullTableName(Namespace.of(namespacePrefix, namespace).prefixed(), table);
146+
Map<String, AttributeValue> keyToDelete = new HashMap<>();
147+
keyToDelete.put("table", AttributeValue.builder().s(fullTableName).build());
148+
149+
client.deleteItem(
150+
DeleteItemRequest.builder()
151+
.tableName(getFullTableName(metadataNamespace, DynamoAdmin.METADATA_TABLE))
152+
.key(keyToDelete)
153+
.build());
154+
}
155+
156+
@Override
157+
public void dropTable(String nonPrefixedNamespace, String table) {
158+
String namespace = Namespace.of(namespacePrefix, nonPrefixedNamespace).prefixed();
159+
client.deleteTable(
160+
DeleteTableRequest.builder().tableName(getFullTableName(namespace, table)).build());
161+
if (!waitForTableDeletion(namespace, table)) {
162+
throw new RuntimeException(
163+
String.format("Deleting the %s table timed out", getFullTableName(namespace, table)));
164+
}
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
@@ -3,6 +3,7 @@
33
import com.scalar.db.api.DistributedStorageAdminCaseSensitivityIntegrationTestBase;
44
import com.scalar.db.config.DatabaseConfig;
55
import com.scalar.db.exception.storage.ExecutionException;
6+
import com.scalar.db.util.AdminTestUtils;
67
import java.util.Properties;
78
import org.junit.jupiter.api.Test;
89
import org.junit.jupiter.api.condition.DisabledIf;
@@ -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 JdbcAdminTestUtils(getProperties(testName));
32+
}
33+
2834
// Since SQLite doesn't have persistent namespaces, some behaviors around the namespace are
2935
// different from the other adapters. So disable several tests that check such behaviors.
3036

@@ -33,6 +39,12 @@ private boolean isSqlite() {
3339
return JdbcEnv.isSqlite();
3440
}
3541

42+
@Test
43+
@Override
44+
@DisabledIf("isSqlite")
45+
public void
46+
dropNamespace_ForNamespaceWithNonScalarDBManagedTables_ShouldThrowIllegalArgumentException() {}
47+
3648
@Test
3749
@Override
3850
@DisabledIf("isSqlite")

0 commit comments

Comments
 (0)