Skip to content

Commit 3576c39

Browse files
authored
[#6667]fix(core):can force delete invalid catalog (#6679)
### What changes were proposed in this pull request? #6667 ### Why are the changes needed? Fix: #6667 ### Does this PR introduce _any_ user-facing change? no ### How was this patch tested? add a new test testForceDropCatalog in TestCatalogManager
1 parent e51ac9f commit 3576c39

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

core/src/main/java/org/apache/gravitino/catalog/CatalogManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ public boolean dropCatalog(NameIdentifier ident, boolean force)
674674
store.list(schemaNamespace, SchemaEntity.class, EntityType.SCHEMA);
675675
CatalogEntity catalogEntity = store.get(ident, EntityType.CATALOG, CatalogEntity.class);
676676

677-
if (containsUserCreatedSchemas(schemaEntities, catalogEntity, catalogWrapper) && !force) {
677+
if (!force && containsUserCreatedSchemas(schemaEntities, catalogEntity, catalogWrapper)) {
678678
throw new NonEmptyCatalogException(
679679
"Catalog %s has schemas, please drop them first or use force option", ident);
680680
}

core/src/test/java/org/apache/gravitino/catalog/TestCatalogManager.java

+31
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.gravitino.catalog;
2020

2121
import static org.apache.gravitino.StringIdentifier.ID_KEY;
22+
import static org.mockito.ArgumentMatchers.any;
2223

2324
import com.google.common.collect.ImmutableMap;
2425
import com.google.common.collect.Maps;
@@ -40,10 +41,12 @@
4041
import org.apache.gravitino.exceptions.NoSuchMetalakeException;
4142
import org.apache.gravitino.meta.AuditInfo;
4243
import org.apache.gravitino.meta.BaseMetalake;
44+
import org.apache.gravitino.meta.SchemaEntity;
4345
import org.apache.gravitino.meta.SchemaVersion;
4446
import org.apache.gravitino.storage.RandomIdGenerator;
4547
import org.apache.gravitino.storage.memory.TestMemoryEntityStore;
4648
import org.apache.gravitino.storage.memory.TestMemoryEntityStore.InMemoryEntityStore;
49+
import org.apache.gravitino.utils.PrincipalUtils;
4750
import org.junit.jupiter.api.AfterAll;
4851
import org.junit.jupiter.api.AfterEach;
4952
import org.junit.jupiter.api.Assertions;
@@ -472,6 +475,34 @@ public void testDropCatalog() {
472475
Assertions.assertNull(catalogManager.catalogCache.getIfPresent(ident));
473476
}
474477

478+
@Test
479+
public void testForceDropCatalog() throws Exception {
480+
NameIdentifier ident = NameIdentifier.of("metalake", "test41");
481+
Map<String, String> props =
482+
ImmutableMap.of("provider", "test", "key1", "value1", "key2", "value2");
483+
String comment = "comment";
484+
catalogManager.createCatalog(ident, Catalog.Type.RELATIONAL, provider, comment, props);
485+
SchemaEntity schemaEntity =
486+
SchemaEntity.builder()
487+
.withId(RandomIdGenerator.INSTANCE.nextId())
488+
.withName("test_schema1")
489+
.withNamespace(Namespace.of("metalake", "test41"))
490+
.withAuditInfo(
491+
AuditInfo.builder()
492+
.withCreator(PrincipalUtils.getCurrentPrincipal().getName())
493+
.withCreateTime(Instant.now())
494+
.build())
495+
.build();
496+
entityStore.put(schemaEntity);
497+
CatalogManager.CatalogWrapper catalogWrapper =
498+
Mockito.mock(CatalogManager.CatalogWrapper.class);
499+
Mockito.doReturn(catalogWrapper).when(catalogManager).loadCatalogAndWrap(ident);
500+
Mockito.doThrow(new RuntimeException("Failed connect"))
501+
.when(catalogWrapper)
502+
.doWithSchemaOps(any());
503+
Assertions.assertTrue(catalogManager.dropCatalog(ident, true));
504+
}
505+
475506
@Test
476507
void testAlterMutableProperties() {
477508
NameIdentifier ident = NameIdentifier.of("metalake", "test41");

0 commit comments

Comments
 (0)