Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#1231] Fix bugs in drop MySQL schemas #1251

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand Down Expand Up @@ -47,11 +48,28 @@ public String generateCreateDatabaseSql(

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,7 @@ protected void testBaseOperation(

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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static void clearTableAndSchema() {
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);
}
Expand Down Expand Up @@ -458,4 +458,40 @@ void testAlterAndDropMysqlTable() {
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)));
}
}