@@ -24,10 +24,7 @@ public class SchemaController implements ResourceController<Schema> {
24
24
@ Override
25
25
public Optional <Schema > createOrUpdateResource (Schema schema ) {
26
26
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 ())) {
31
28
connection .createStatement ().execute (format ("CREATE SCHEMA `%1$s` DEFAULT CHARACTER SET %2$s" ,
32
29
schema .getMetadata ().getName (),
33
30
schema .getSpec ().getEncoding ()));
@@ -40,10 +37,8 @@ public Optional<Schema> createOrUpdateResource(Schema schema) {
40
37
schema .setStatus (status );
41
38
42
39
log .info ("Schema {} created" , schema .getMetadata ().getName ());
43
- return Optional .of (schema );
44
- } else {
45
- return Optional .of (schema );
46
40
}
41
+ return Optional .of (schema );
47
42
} catch (SQLException e ) {
48
43
log .error ("Error while creating Schema" , e );
49
44
@@ -61,9 +56,13 @@ public boolean deleteResource(Schema schema) {
61
56
log .info ("Execution deleteResource for: {}" , schema .getMetadata ().getName ());
62
57
63
58
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
+ }
67
66
return true ;
68
67
} catch (SQLException e ) {
69
68
log .error ("Error while trying to delete Schema" , e );
@@ -79,4 +78,11 @@ private Connection getConnection() throws SQLException {
79
78
System .getenv ("MYSQL_PASSWORD" )));
80
79
}
81
80
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
+
82
88
}
0 commit comments