private static String getUrlUncached(String dbName, boolean persistToFile) { if (persistToFile) { try { File tmpPlaceholder = File.createTempFile("hsqldb" + dbName, HSQL_FILE_PLACEHOLDER); File tmpDbFile = new File(tmpPlaceholder.getParentFile(), tmpPlaceholder.getName().substring(0, tmpPlaceholder.getName().length() - HSQL_FILE_PLACEHOLDER.length())); return String.format("jdbc:hsqldb:file:%1$s", tmpDbFile.getAbsolutePath()); } catch (IOException exc) { throw new DeployerRuntimeException(exc); } } else { return String.format("jdbc:hsqldb:mem:%1$s", dbName); } } }
@Test public void testExceptionDetectionAndSkips() { try { DbEnvironmentFactory.getInstance().readOneFromSourcePath("./src/test/resources/DbDeployer/error-run-test") .buildAppContext() .setupAndCleanAndDeploy(); } catch (DeployerRuntimeException exc) { assertThat(exc.getMessage(), containsString("Object [TABLE_A]; ChangeName [invalidChange]")); assertThat(exc.getMessage(), containsString("Object [TABLE_B]; ChangeName [n/a]")); assertThat(exc.getMessage(), not(containsString("Object [TABLE_A]; ChangeName [failed_fk_not_to_be_attempted]"))); } } }
private static String getUrlUncached(String dbName, boolean persistToFile) { if (persistToFile) { try { File tmpPlaceholder = File.createTempFile("hsqldb" + dbName, HSQL_FILE_PLACEHOLDER); File tmpDbFile = new File(tmpPlaceholder.getParentFile(), tmpPlaceholder.getName().substring(0, tmpPlaceholder.getName().length() - HSQL_FILE_PLACEHOLDER.length())); return String.format("jdbc:hsqldb:file:%1$s", tmpDbFile.getAbsolutePath()); } catch (IOException exc) { throw new DeployerRuntimeException(exc); } } else { return String.format("jdbc:hsqldb:mem:%1$s", dbName); } } }
public Platform valueOf(String dbPlatformStr) { try { ImmutableHierarchicalConfiguration platformConfig = platformConfigs.get(dbPlatformStr); String resolvedDbPlatformClass = null; if (platformConfig != null) { resolvedDbPlatformClass = platformConfig.getString("class"); } if (resolvedDbPlatformClass == null) { resolvedDbPlatformClass = dbPlatformStr; } return (Platform) Class.forName(resolvedDbPlatformClass).newInstance(); } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } catch (ClassNotFoundException e) { throw new DeployerRuntimeException("Could not find platform named " + dbPlatformStr + "; no class found and name was not in the list of default platform strings: " + platformConfigs.keysView()); } }
public Platform valueOf(String dbPlatformStr) { try { ImmutableHierarchicalConfiguration platformConfig = platformConfigs.get(dbPlatformStr); String resolvedDbPlatformClass = null; if (platformConfig != null) { resolvedDbPlatformClass = platformConfig.getString("class"); } if (resolvedDbPlatformClass == null) { resolvedDbPlatformClass = dbPlatformStr; } return (Platform) Class.forName(resolvedDbPlatformClass).newInstance(); } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } catch (ClassNotFoundException e) { throw new DeployerRuntimeException("Could not find platform named " + dbPlatformStr + "; no class found and name was not in the list of default platform strings: " + platformConfigs.keysView()); } }
private static DataSource ds(String driverClassName, String url, String username, String password) { Class<? extends Driver> driverClass; try { driverClass = (Class<? extends Driver>) Class.forName(driverClassName); return JdbcDataSourceFactory.createFromJdbcUrl(driverClass, url, new Credential(username, password)); } catch (ClassNotFoundException e) { throw new DeployerRuntimeException(e); } }
private static DataSource ds(String driverClassName, String url, String username, String password) { Class<? extends Driver> driverClass; try { driverClass = (Class<? extends Driver>) Class.forName(driverClassName); return JdbcDataSourceFactory.createFromJdbcUrl(driverClass, url, new Credential(username, password)); } catch (ClassNotFoundException e) { throw new DeployerRuntimeException(e); } }
public DeployerAppContext getAppContextBuilder() { DeployerAppContext deployerAppContext; try { if (appContextBuilderClass != null) { deployerAppContext = this.appContextBuilderClass.newInstance(); } else { deployerAppContext = this.platform.getAppContextBuilderClass().newInstance(); } } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } deployerAppContext.setEnvironment(this); if (defaultUserId != null && defaultPassword != null) { deployerAppContext.setCredential(new Credential(defaultUserId, defaultPassword)); } return deployerAppContext; }
public DeployerAppContext getAppContextBuilder() { DeployerAppContext deployerAppContext; try { if (appContextBuilderClass != null) { deployerAppContext = this.appContextBuilderClass.newInstance(); } else { deployerAppContext = this.platform.getAppContextBuilderClass().newInstance(); } } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } deployerAppContext.setEnvironment(this); if (defaultUserId != null && defaultPassword != null) { deployerAppContext.setCredential(new Credential(defaultUserId, defaultPassword)); } return deployerAppContext; }
private static DeployerAppContext getAppContextBuilder(Environment env, Credential credential) { DeployerAppContext deployerAppContext; try { deployerAppContext = env.getPlatform().getAppContextBuilderClass().newInstance(); } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } deployerAppContext.setEnvironment(env); if (credential == null) { credential = getDefaultCredential(env); } deployerAppContext.setCredential(credential); return deployerAppContext; }
@Override public <T> T executeWithinContext(PhysicalSchema schema, ThrowingFunction<Connection, T> callable) { Connection conn = null; try { conn = ds.getConnection(); setDataSourceSchema(conn, schema); return callable.safeValueOf(conn); } catch (DeployerRuntimeException e) { throw e; // rethrowing DeployerRuntimeException to avoid excessive stack trace outputs } catch (Exception e) { throw new DeployerRuntimeException(e); } finally { DbUtils.closeQuietly(conn); } }
private static DeployerAppContext getAppContextBuilder(Environment env, Credential credential) { DeployerAppContext deployerAppContext; try { deployerAppContext = env.getPlatform().getAppContextBuilderClass().newInstance(); } catch (InstantiationException e) { throw new DeployerRuntimeException(e); } catch (IllegalAccessException e) { throw new DeployerRuntimeException(e); } deployerAppContext.setEnvironment(env); if (credential == null) { credential = getDefaultCredential(env); } deployerAppContext.setCredential(credential); return deployerAppContext; }
@Override public void executeWithinContext(PhysicalSchema schema, Procedure<Connection> runnable) { Connection conn = null; try { conn = ds.getConnection(); setDataSourceSchema(conn, schema); runnable.value(conn); } catch (RuntimeException e) { throw e; // rethrowing DeployerRuntimeException to avoid excessive stack trace outputs } catch (Exception e) { throw new DeployerRuntimeException(e); } finally { DbUtils.closeQuietly(conn); } }
@Override public void executeWithinContext(PhysicalSchema schema, Procedure<Connection> runnable) { Connection conn = null; try { conn = ds.getConnection(); setDataSourceSchema(conn, schema); runnable.value(conn); } catch (RuntimeException e) { throw e; // rethrowing DeployerRuntimeException to avoid excessive stack trace outputs } catch (Exception e) { throw new DeployerRuntimeException(e); } finally { DbUtils.closeQuietly(conn); } }
@Override public <T> T executeWithinContext(PhysicalSchema schema, ThrowingFunction<Connection, T> callable) { Connection conn = null; try { conn = ds.getConnection(); setDataSourceSchema(conn, schema); return callable.safeValueOf(conn); } catch (DeployerRuntimeException e) { throw e; // rethrowing DeployerRuntimeException to avoid excessive stack trace outputs } catch (Exception e) { throw new DeployerRuntimeException(e); } finally { DbUtils.closeQuietly(conn); } }
Thread.sleep(seconds.getSeconds() * 1000); } catch (InterruptedException e) { throw new DeployerRuntimeException(e);
Thread.sleep(seconds.getSeconds() * 1000); } catch (InterruptedException e) { throw new DeployerRuntimeException(e);
@Override public void cleanEnvironment(final boolean noPrompt) { Validate.isTrue(env.isCleanBuildAllowed(), "Clean build not allowed for this environment [" + env.getName() + "] ! Exiting..."); // some schemas have complex dependencies that we currently aren't handling w/ the drop code. To work // around it, we just retry the drop if we have progress in dropping objects. // Note that regular forward deploys can handle dependencies properly; we just need the logic to extract // the object definitions out for all object types to enable this. int tryCount = 0; while (true) { tryCount++; LOG.info("Attempting to clean objects from environment"); final Pair<Boolean, MutableList<Exception>> clearResults = clearEnvironmentInternal(noPrompt); if (!clearResults.getOne()) { throw new DeployerRuntimeException("Could not clean schema; remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE)); } else if (clearResults.getTwo().isEmpty()) { return; } else if (tryCount <= 10) { LOG.info("Failed to clean up schema on try #" + tryCount + " but able to make progress, will continue to try"); } else { throw new DeployerRuntimeException("Could not clean schema after max " + tryCount + " tries; will exit with remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE)); } } }
@Override public void cleanEnvironment(final boolean noPrompt) { Validate.isTrue(env.isCleanBuildAllowed(), "Clean build not allowed for this environment [" + env.getName() + "] ! Exiting..."); // some schemas have complex dependencies that we currently aren't handling w/ the drop code. To work // around it, we just retry the drop if we have progress in dropping objects. // Note that regular forward deploys can handle dependencies properly; we just need the logic to extract // the object definitions out for all object types to enable this. int tryCount = 0; while (true) { tryCount++; LOG.info("Attempting to clean objects from environment"); final Pair<Boolean, MutableList<Exception>> clearResults = clearEnvironmentInternal(noPrompt); if (!clearResults.getOne()) { throw new DeployerRuntimeException("Could not clean schema; remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE)); } else if (clearResults.getTwo().isEmpty()) { return; } else if (tryCount <= 10) { LOG.info("Failed to clean up schema on try #" + tryCount + " but able to make progress, will continue to try"); } else { throw new DeployerRuntimeException("Could not clean schema after max " + tryCount + " tries; will exit with remaining exceptions: " + clearResults.getTwo().collect(TO_EXCEPTION_STACK_TRACE)); } } }
void deployArtifact(Connection conn, Change artifact) { MutableList<String> sqls = MultiLineStringSplitter.createSplitterOnSpaceAndLine("GO").valueOf(artifact.getConvertedContent()); int index = 0; for (String sql : sqls) { index++; if (StringUtils.isBlank(sql)) { LOG.debug("Skipping blank sql"); } else { LOG.debug("Executing change #{} in the artifact", index); try { dialect.doTryBlockForArtifact(conn, this.sqlExecutor, artifact); this.sqlExecutor.getJdbcTemplate().update(conn, sql); } catch (DataAccessException e) { throw new DeployerRuntimeException("Could not execute DDL:\nfor artifact [[[" + artifact.getDisplayString() + "]]] while executing SQL: [[[\n" + sql + "\n]]]", e); } finally { dialect.doFinallyBlockForArtifact(conn, this.sqlExecutor, artifact); } } } } }