Java Code Examples for org.springframework.jdbc.datasource.init.ResourceDatabasePopulator#populate()

The following examples show how to use org.springframework.jdbc.datasource.init.ResourceDatabasePopulator#populate() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HermesDataSourceInitializer.java    From hermes with Apache License 2.0 7 votes vote down vote up
/**
 * DriverName: 
 * 1:MySQL Connector Java; 
 * 2:Oracle JDBC driver; 
 * 3:H2 JDBC Driver
 * @throws SQLException
 */
@PostConstruct
public void initData() throws SQLException {
	String dbDriverName = getDBDriveName();
	Logger.info("当前数据库驱动名称:"+dbDriverName);
	Logger.info("检查是否需要初始化脚本");
	Query query = new Query("from User");
	Long userCount = commonRepository.count(query.getCount(), new HashMap<String, Object>());
	if (userCount == 0) {
		Logger.info("开始进行初始化脚本");
		ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
		populator.setSqlScriptEncoding("utf-8");
		populator.addScript(dataScript);
		populator.populate(dataSource.getConnection());
		Logger.info("初始化脚本已完成");
		return;
	}else{
		Logger.info("不需要初始化脚本");
	}
}
 
Example 2
Source File: DataSourceAppConfig.java    From logsniffer with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * @return H2 pooled data source
 * @throws SQLException
 */
@Bean(destroyMethod = "dispose")
public DataSource dataSource() throws SQLException {
	final JdbcConnectionPool pool = JdbcConnectionPool.create(url, user, password);
	pool.setMaxConnections(maxPoolConnections);
	Connection con = null;
	con = pool.getConnection();

	final Flyway flyway = new Flyway();
	flyway.setLocations("classpath:sql/migration");
	flyway.setDataSource(pool);
	flyway.setSqlMigrationPrefix("VOS-");
	flyway.setIgnoreFailedFutureMigration(true);

	final JdbcTemplate tpl = new JdbcTemplate(pool);
	if (tpl.queryForObject("select count(*) from information_schema.tables where table_name = 'LOG_SOURCES'",
			int.class) == 0) {
		logger.info("H2 database not found, creating new schema and populate with default data");
		flyway.setBaselineVersion(MigrationVersion.fromVersion(DB_SETUP_VERSION));
		flyway.setBaselineOnMigrate(true);
		try {
			final ResourceDatabasePopulator dbPopulator = new ResourceDatabasePopulator();
			dbPopulator.addScript(new ClassPathResource("/sql/quartz/tables_h2.sql"));
			dbPopulator.addScript(new ClassPathResource("/sql/model/schema_h2.sql"));
			dbPopulator.populate(con);
			newSchema = true;
			logger.info("Established H2 connection pool with new database");
		} finally {
			if (con != null) {
				con.close();
			}
		}
	} else {
		logger.info("Established H2 connection pool with existing database");
		if (tpl.queryForObject("select count(*) from information_schema.tables where table_name = 'schema_version'",
				int.class) == 0) {
			logger.info("Flyway's DB migration not setup in this version, set baseline version to 0.5.0");
			flyway.setBaselineVersion(MigrationVersion.fromVersion("0.5.0"));
			flyway.setBaselineOnMigrate(true);
		}
	}

	logger.debug("Migrating database, base version is: {}", flyway.getBaselineVersion());
	flyway.migrate();
	logger.debug("Database migrated from base version: {}", flyway.getBaselineVersion());

	return pool;
}