/** * Specify that a failed SQL {@code DROP} statement within an executed * script can be ignored. * <p>This is useful for a database whose SQL dialect does not support an * {@code IF EXISTS} clause in a {@code DROP} statement. * <p>The default is {@code false} so that {@link #build building} will fail * fast if a script starts with a {@code DROP} statement. * @param flag {@code true} if failed drop statements should be ignored * @return {@code this}, to facilitate method chaining * @since 4.0.3 */ public EmbeddedDatabaseBuilder ignoreFailedDrops(boolean flag) { this.databasePopulator.setIgnoreFailedDrops(flag); return this; }
@Override public void migrate(Connection connection) throws Exception { if ("sqlserver".equals(type) || "hsqldb".equals(type)) { //we don't have this problem with sqlserver or in memory DB logger.info("Skipping 4.0.4 migration for " + type + ", not affected by 3.9.9 back ports."); return; } ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); SingleConnectionDataSource dataSource = new SingleConnectionDataSource(connection, true); JdbcTemplate template = new JdbcTemplate(dataSource); boolean run = false; for (Map.Entry<String, String> script : getScripts()) { int count = template.queryForObject(checkExistsSql, Integer.class, script.getKey()); if (count == 0) { String path = "org/cloudfoundry/identity/uaa/db/" + type + "/" + script.getValue(); logger.info(String.format("[4.0.4] Adding script for version %s with path %s", script.getKey(), path)); populator.addScript(new ClassPathResource(path)); run = true; } } if (run) { logger.info("Running missing migrations."); populator.setContinueOnError(false); populator.setIgnoreFailedDrops(true); populator.populate(connection); logger.info("Completed missing migrations."); } else { logger.info("Skipping 4.0.4 migrations, no migrations missing."); } }
@Override public void migrate(Connection connection) throws Exception { ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScript(new ClassPathResource("org/cloudfoundry/identity/uaa/db/" + type + "/V1_5_2__initial_db.sql")); populator.setContinueOnError(true); populator.setIgnoreFailedDrops(true); populator.populate(connection); } }
populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS);
@Test public void scriptWithSingleLineCommentsAndFailedDrop() throws Exception { databasePopulator.addScript(resource("db-schema-failed-drop-comments.sql")); databasePopulator.addScript(resource("db-test-data.sql")); databasePopulator.setIgnoreFailedDrops(true); DatabasePopulatorUtils.execute(databasePopulator, db); assertTestDatabaseCreated(); }
/** * Specify that a failed SQL {@code DROP} statement within an executed * script can be ignored. * <p>This is useful for a database whose SQL dialect does not support an * {@code IF EXISTS} clause in a {@code DROP} statement. * <p>The default is {@code false} so that {@link #build building} will fail * fast if a script starts with a {@code DROP} statement. * @param flag {@code true} if failed drop statements should be ignored * @return {@code this}, to facilitate method chaining * @since 4.0.3 */ public EmbeddedDatabaseBuilder ignoreFailedDrops(boolean flag) { this.databasePopulator.setIgnoreFailedDrops(flag); return this; }
private ResourceDatabasePopulator createPopulator(ClassPathResource script) { logger.info("Creating database populator using script '{}'", script.getPath()); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setSeparator(DB_TYPE_ORACLE.equals(dbType) ? "/" : ";"); populator.setIgnoreFailedDrops(true); populator.setSqlScriptEncoding(UTF_8.name()); populator.addScript(script); return populator; }
private ResourceDatabasePopulator createPopulator(ClassPathResource script) { logger.info("Creating database populator using script '{}'", script.getPath()); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.setSeparator(DB_TYPE_ORACLE.equals(dbType) ? "/" : ";"); populator.setIgnoreFailedDrops(true); populator.setSqlScriptEncoding(UTF_8.name()); populator.addScript(script); return populator; }
/** * @param sqlFile the sql file to execute * @param shouldContinueOnError * @throws IOException */ protected void executeSQLResource(final String sqlFile, boolean shouldContinueOnError) throws IOException, SQLException { final Resource sqlResource = getSQLResource(sqlFolder, sqlFile); ResourceDatabasePopulator populate = new ResourceDatabasePopulator(); populate.setContinueOnError(shouldContinueOnError); populate.setIgnoreFailedDrops(true); populate.addScript(sqlResource); populate.setSeparator(getSeparator()); populate.execute(datasource); logger.info("Executed SQL script " + sqlResource.getURL().getFile()); }
/** * @param sqlFile the sql file to execute * @param shouldContinueOnError * @throws IOException */ protected void executeSQLResource(final String sqlFile, boolean shouldContinueOnError) throws IOException, SQLException { final Resource sqlResource = getSQLResource(sqlFolder, sqlFile); ResourceDatabasePopulator populate = new ResourceDatabasePopulator(); populate.setContinueOnError(shouldContinueOnError); populate.setIgnoreFailedDrops(true); populate.addScript(sqlResource); populate.setSeparator(getSeparator()); populate.execute(datasource); logger.info("Executed SQL script " + sqlResource.getURL().getFile()); }
@ConditionalOnProperty("jdbc.initialize") @Bean public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader) { DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); dataSourceInitializer.setDataSource(dataSource); ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.setIgnoreFailedDrops(true); dataSourceInitializer.setDatabasePopulator(databasePopulator); if ("true".equals(properties.getInitialize())) { databasePopulator.addScript(new DefaultInitializationScriptResource(properties)); } else { databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize())); } return dataSourceInitializer; }
@ConditionalOnProperty("jdbc.initialize") @Bean public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader) { DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); dataSourceInitializer.setDataSource(dataSource); ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(); databasePopulator.setIgnoreFailedDrops(true); dataSourceInitializer.setDatabasePopulator(databasePopulator); if ("true".equals(properties.getInitialize())) { databasePopulator.addScript(new DefaultInitializationScriptResource(properties.getTableName(), properties.getColumnsMap().keySet())); } else { databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize())); } return dataSourceInitializer; }
populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS);
populator.setBlockCommentEndDelimiter(mergedSqlConfig.getBlockCommentEndDelimiter()); populator.setContinueOnError(mergedSqlConfig.getErrorMode() == ErrorMode.CONTINUE_ON_ERROR); populator.setIgnoreFailedDrops(mergedSqlConfig.getErrorMode() == ErrorMode.IGNORE_FAILED_DROPS);