Skip to content

Commit 10eab6d

Browse files
committed
only attempt delete of schema of it exists
1 parent f215523 commit 10eab6d

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

samples/mysql-schema/src/main/java/com/github/containersolutions/operator/sample/SchemaController.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ public class SchemaController implements ResourceController<Schema> {
2424
@Override
2525
public Optional<Schema> createOrUpdateResource(Schema schema) {
2626
try (Connection connection = getConnection()) {
27-
ResultSet resultSet = connection.createStatement().executeQuery(
28-
format("SELECT schema_name FROM information_schema.schemata WHERE schema_name = \"%1$s\"",
29-
schema.getMetadata().getName()));
30-
if (!resultSet.first()) {
27+
if (schemaExists(connection, schema.getMetadata().getName())) {
3128
connection.createStatement().execute(format("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s",
3229
schema.getMetadata().getName(),
3330
schema.getSpec().getEncoding()));
@@ -40,10 +37,8 @@ public Optional<Schema> createOrUpdateResource(Schema schema) {
4037
schema.setStatus(status);
4138

4239
log.info("Schema {} created", schema.getMetadata().getName());
43-
return Optional.of(schema);
44-
} else {
45-
return Optional.of(schema);
4640
}
41+
return Optional.of(schema);
4742
} catch (SQLException e) {
4843
log.error("Error while creating Schema", e);
4944

@@ -61,9 +56,13 @@ public boolean deleteResource(Schema schema) {
6156
log.info("Execution deleteResource for: {}", schema.getMetadata().getName());
6257

6358
try (Connection connection = getConnection()) {
64-
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
65-
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
66-
59+
if (schemaExists(connection, schema.getMetadata().getName())) {
60+
connection.createStatement().execute("DROP DATABASE `" + schema.getMetadata().getName() + "`");
61+
log.info("Deleted Schema '{}'", schema.getMetadata().getName());
62+
} else {
63+
log.info("Delete event ignored for schema '{}', real schema doesn't exist",
64+
schema.getMetadata().getName());
65+
}
6766
return true;
6867
} catch (SQLException e) {
6968
log.error("Error while trying to delete Schema", e);
@@ -79,4 +78,11 @@ private Connection getConnection() throws SQLException {
7978
System.getenv("MYSQL_PASSWORD")));
8079
}
8180

81+
private boolean schemaExists(Connection connection, String schemaName) throws SQLException {
82+
ResultSet resultSet = connection.createStatement().executeQuery(
83+
format("SELECT schema_name FROM information_schema.schemata WHERE schema_name = \"%1$s\"",
84+
schemaName));
85+
return resultSet.first();
86+
}
87+
8288
}

0 commit comments

Comments
 (0)