Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#1231] Fix bugs in drop MySQL schemas (#1235)

Qi Yu3ece62754647
diff --git a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java
index 33fd2c6f86..b2c68133e1 100644
--- a/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java
+++ b/catalogs/catalog-jdbc-mysql/src/main/java/com/datastrato/gravitino/catalog/mysql/operation/MysqlDatabaseOperations.java
@@ -12,6 +12,7 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections4.MapUtils;
@@ -47,11 +48,28 @@ public class MysqlDatabaseOperations extends JdbcDatabaseOperations {
@Override
public String generateDropDatabaseSql(String databaseName, boolean cascade) {
+ final String dropDatabaseSql = "DROP DATABASE `" + databaseName + "`";
if (cascade) {
- throw new UnsupportedOperationException(
- "MySQL does not support CASCADE option for DROP DATABASE.");
+ return dropDatabaseSql;
}
- return "DROP DATABASE `" + databaseName + "`";
+
+ try (final Connection connection = this.dataSource.getConnection()) {
+ String query = "SHOW TABLES IN " + databaseName;
+ try (Statement statement = connection.createStatement()) {
+ // Execute the query and check if there exists any tables in the database
+ try (ResultSet resultSet = statement.executeQuery(query)) {
+ if (resultSet.next()) {
+ throw new IllegalStateException(
+ String.format(
+ "Database %s is not empty, the value of cascade should be true.",
+ databaseName));
+ }
+ }
+ }
+ } catch (SQLException sqlException) {
+ throw this.exceptionMapper.toGravitinoException(sqlException);
+ }
+ return dropDatabaseSql;
}
@Override
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
index 4fb9961364..9d5c9548e5 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/TestJdbcAbstractIT.java
@@ -62,17 +62,7 @@ public abstract class TestJdbcAbstractIT {
protected static void testDropDatabase(String databaseName) {
List<String> databases;
- // delete database.
- UnsupportedOperationException unsupportedOperationException =
- Assertions.assertThrows(
- UnsupportedOperationException.class,
- () -> DATABASE_OPERATIONS.delete(databaseName, true));
- Assertions.assertTrue(
- unsupportedOperationException
- .getMessage()
- .contains("does not support CASCADE option for DROP DATABASE."));
-
- Assertions.assertDoesNotThrow(() -> DATABASE_OPERATIONS.delete(databaseName, false));
+ DATABASE_OPERATIONS.delete(databaseName, true);
Assertions.assertThrows(
NoSuchSchemaException.class, () -> DATABASE_OPERATIONS.load(databaseName));
diff --git a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
index a59dc2db8c..0677ee8475 100644
--- a/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
+++ b/integration-test/src/test/java/com/datastrato/gravitino/integration/test/catalog/jdbc/mysql/CatalogMysqlIT.java
@@ -120,7 +120,7 @@ public class CatalogMysqlIT extends AbstractIT {
NameIdentifier[] nameIdentifiers =
catalog.asTableCatalog().listTables(Namespace.of(metalakeName, catalogName, schemaName));
for (NameIdentifier nameIdentifier : nameIdentifiers) {
- catalog.asTableCatalog().dropTable(nameIdentifier);
+ catalog.asTableCatalog().purgeTable(nameIdentifier);
}
catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);
}
@@ -456,4 +456,40 @@ public class CatalogMysqlIT extends AbstractIT {
catalog.asTableCatalog().dropTable(tableIdentifier);
});
}
+
+ @Test
+ void testDropMySQLDatabase() {
+ String schemaName = GravitinoITUtils.genRandomName("mysql_schema").toLowerCase();
+ String tableName = GravitinoITUtils.genRandomName("mysql_table").toLowerCase();
+
+ catalog
+ .asSchemas()
+ .createSchema(
+ NameIdentifier.of(metalakeName, catalogName, schemaName),
+ "Created by gravitino client",
+ ImmutableMap.<String, String>builder().build());
+
+ catalog
+ .asTableCatalog()
+ .createTable(
+ NameIdentifier.of(metalakeName, catalogName, schemaName, tableName),
+ createColumns(),
+ "Created by gravitino client",
+ ImmutableMap.<String, String>builder().build());
+
+ // Try to drop a database, and cascade equals to false, it should not be allowed.
+ catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), false);
+ // Check the database still exists
+ catalog.asSchemas().loadSchema(NameIdentifier.of(metalakeName, catalogName, schemaName));
+
+ // Try to drop a database, and cascade equals to true, it should be allowed.
+ catalog.asSchemas().dropSchema(NameIdentifier.of(metalakeName, catalogName, schemaName), true);
+ // Check database has been dropped
+ Assertions.assertThrows(
+ NoSuchSchemaException.class,
+ () ->
+ catalog
+ .asSchemas()
+ .loadSchema(NameIdentifier.of(metalakeName, catalogName, schemaName)));
+ }
}