diff --git a/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/SystemInitializerListener.java b/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/SystemInitializerListener.java index 054a455..e1c66ea 100644 --- a/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/SystemInitializerListener.java +++ b/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/SystemInitializerListener.java @@ -20,8 +20,10 @@ public class SystemInitializerListener extends RestServerLifecycleListenerBase { public static final String PREFIX = SystemInitializerListener.class.getSimpleName() + ": "; public static final String SAFE_CHARS_MESSAGE = " -- this can only contain letters, numbers, spaces, tabs, and these special characters: -._/="; + public static void invalidName(final String msg, String value) { die(PREFIX + msg + ": " + value + SAFE_CHARS_MESSAGE); } @Getter @Setter private boolean checkRedis = true; + @Getter @Setter private String checkTable = null; @Override public void beforeStart(RestServer server) { @@ -31,9 +33,9 @@ public class SystemInitializerListener extends RestServerLifecycleListenerBase { final String password = config.getDatabase().getPassword(); // we're going to use these in shell scripts, so ensure they are safe - if (!checkSafeShellArg(db)) die(PREFIX+"invalid db name: "+db+SAFE_CHARS_MESSAGE); - if (!checkSafeShellArg(user)) die(PREFIX+"invalid db user name: "+user+SAFE_CHARS_MESSAGE); - if (!checkSafeShellArg(password)) die(PREFIX+": invalid password for user '"+user+"': "+password+SAFE_CHARS_MESSAGE); + if (!checkSafeShellArg(db)) invalidName("invalid db name", db); + if (!checkSafeShellArg(user)) invalidName("invalid db user name", user); + if (!checkSafeShellArg(password)) invalidName(": invalid password for user '"+user+"'", password); try { config.execSql("select 1"); @@ -44,12 +46,14 @@ public class SystemInitializerListener extends RestServerLifecycleListenerBase { log.warn(PREFIX+"database not configured, attempting to initialize..."); } - // does a database exist? try { if (!dbExists(db)) { execScript("createdb --encoding=UTF-8 "+db); if (!dbExists(db)) die(PREFIX+"error creating "+db+" database"); + // create the schema, just this time + config.getDatabase().getHibernate().setHbm2ddlAuto("create"); + } else { log.info(db+" DB exists, not creating"); } @@ -62,6 +66,10 @@ public class SystemInitializerListener extends RestServerLifecycleListenerBase { } else { log.info("DB user '"+user+"' exists, not creating"); } + + // create the schema because the database was just created + config.getDatabase().getHibernate().setHbm2ddlAuto("create"); + } catch (Exception e) { die(PREFIX+"Error initializing database: "+shortError(e)); } @@ -76,10 +84,30 @@ public class SystemInitializerListener extends RestServerLifecycleListenerBase { die(PREFIX+"database configuration failed, cannot run test query: "+shortError(e)); } + // does a proper table exist? + if (!empty(checkTable)) { + if (!checkSafeShellArg(checkTable)) invalidName("invalid table name", checkTable); + try { + config.execSql("select * from " + checkTable + " limit 1"); + } catch (Exception e) { + log.warn("table '"+checkTable+"' not found, will create schema: " + shortError(e)); + config.getDatabase().getHibernate().setHbm2ddlAuto("create"); + } + } + super.beforeStart(server); } @Override public void onStart(RestServer server) { + if (!empty(checkTable)) { + final PgRestServerConfiguration config = (PgRestServerConfiguration) server.getConfiguration(); + if (!checkSafeShellArg(checkTable)) invalidName("invalid table name", checkTable); + try { + config.execSql("select * from "+checkTable+" limit 1"); + } catch (Exception e) { + die(PREFIX + "table '"+checkTable+"' not found: " + shortError(e)); + } + } if (isCheckRedis()) { try { final RedisService redis = server.getConfiguration().getBean(RedisService.class);