@@ -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