diff --git a/metacat-connector-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseService.java b/metacat-connector-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseService.java index c94663fd..8b5b5edd 100644 --- a/metacat-connector-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseService.java +++ b/metacat-connector-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseService.java @@ -10,6 +10,7 @@ import com.netflix.metacat.common.server.connectors.exception.ConnectorException; import com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException; import com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException; +import com.netflix.metacat.common.server.connectors.exception.DatabasePreconditionFailedException; import com.netflix.metacat.common.server.connectors.exception.InvalidMetaException; import com.netflix.metacat.common.server.connectors.model.DatabaseInfo; import com.netflix.metacat.connector.polaris.common.PolarisUtils; @@ -20,6 +21,7 @@ import org.springframework.dao.DataIntegrityViolationException; import javax.annotation.Nullable; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; @@ -87,17 +89,17 @@ public void delete(final ConnectorRequestContext context, final QualifiedName na try { this.polarisStoreService.deleteDatabase(name.getDatabaseName()); } catch (DataIntegrityViolationException exception) { -// if (exception.getMessage().contains("violates foreign key constraint") -// || (exception.getCause() instanceof SQLException -// && "23503".equals(((SQLException) exception.getCause()).getSQLState()))) { -// -// final String errorMessage = String.format( -// "Failed to delete database %s due to foreign key constraint violation. " -// + "Ensure all dependent tables are removed first. Error: %s", -// name, exception.getMessage() -// ); -// throw new DatabasePreconditionFailedException(name, errorMessage, exception); -// } + if (exception.getMessage().contains("violates foreign key constraint") + || (exception.getCause() instanceof SQLException + && "23503".equals(((SQLException) exception.getCause()).getSQLState()))) { + + final String errorMessage = String.format( + "Failed to delete database %s due to foreign key constraint violation. " + + "Ensure all dependent tables are removed first. Error: %s", + name, exception.getMessage() + ); + throw new DatabasePreconditionFailedException(name, errorMessage, exception); + } throw new InvalidMetaException(name, exception); } catch (Exception exception) { throw new ConnectorException( diff --git a/metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseServiceTest.java b/metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseServiceTest.java index 163ad3ab..f639a853 100644 --- a/metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseServiceTest.java +++ b/metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseServiceTest.java @@ -81,8 +81,6 @@ public class PolarisConnectorDatabaseServiceTest { */ @BeforeEach public void init() { - final String location = "file://temp2"; - polarisStoreService.createDatabase(DB1_NAME, location, "metacat_user"); connectorContext = new ConnectorContext(CATALOG_NAME, CATALOG_NAME, "polaris", new DefaultConfigImpl( new MetacatProperties(null) @@ -206,6 +204,8 @@ public void testDeleteDb() { @Test public void testDeleteDbNoCascades() { + final DatabaseInfo info = DatabaseInfo.builder().name(DB1_QUALIFIED_NAME).build(); + polarisDBService.create(requestContext, info); Assert.assertTrue(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME)); final QualifiedName qualifiedName = QualifiedName.ofTable(