use of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator in project spring-security by spring-projects.
the class DataConfig method initDatabase.
@Bean
@DependsOn("entityManagerFactory")
public ResourceDatabasePopulator initDatabase(DataSource dataSource) throws Exception {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("data.sql"));
populator.populate(dataSource.getConnection());
return populator;
}
use of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator in project spring-security by spring-projects.
the class DataConfiguration method initDatabase.
@Bean
@DependsOn("entityManagerFactory")
public ResourceDatabasePopulator initDatabase(DataSource dataSource) throws Exception {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.addScript(new ClassPathResource("data.sql"));
populator.populate(dataSource.getConnection());
return populator;
}
use of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator in project spring-boot by spring-projects.
the class AbstractDatabaseInitializer method initialize.
@PostConstruct
protected void initialize() {
if (!isEnabled()) {
return;
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
String schemaLocation = getSchemaLocation();
if (schemaLocation.contains(PLATFORM_PLACEHOLDER)) {
String platform = getDatabaseName();
schemaLocation = schemaLocation.replace(PLATFORM_PLACEHOLDER, platform);
}
populator.addScript(this.resourceLoader.getResource(schemaLocation));
populator.setContinueOnError(true);
DatabasePopulatorUtils.execute(populator, this.dataSource);
}
use of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator in project spring-boot by spring-projects.
the class DataSourceInitializer method runScripts.
private void runScripts(List<Resource> resources, String username, String password) {
if (resources.isEmpty()) {
return;
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
populator.setContinueOnError(this.properties.isContinueOnError());
populator.setSeparator(this.properties.getSeparator());
if (this.properties.getSqlScriptEncoding() != null) {
populator.setSqlScriptEncoding(this.properties.getSqlScriptEncoding().name());
}
for (Resource resource : resources) {
populator.addScript(resource);
}
DataSource dataSource = this.dataSource;
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
dataSource = DataSourceBuilder.create(this.properties.getClassLoader()).driverClassName(this.properties.determineDriverClassName()).url(this.properties.determineUrl()).username(username).password(password).build();
}
DatabasePopulatorUtils.execute(populator, dataSource);
}
use of org.springframework.jdbc.datasource.init.ResourceDatabasePopulator in project spring-framework by spring-projects.
the class EmbeddedDatabaseFactoryBeanTests method testFactoryBeanLifecycle.
@Test
public void testFactoryBeanLifecycle() throws Exception {
EmbeddedDatabaseFactoryBean bean = new EmbeddedDatabaseFactoryBean();
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(resource("db-schema.sql"), resource("db-test-data.sql"));
bean.setDatabasePopulator(populator);
bean.afterPropertiesSet();
DataSource ds = bean.getObject();
JdbcTemplate template = new JdbcTemplate(ds);
assertEquals("Keith", template.queryForObject("select NAME from T_TEST", String.class));
bean.destroy();
}
Aggregations