commit
ee41dbdd8f
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.sql.init;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.jdbc.init.dependency.DataSourceInitializationDependencyConfigurer;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnMissingBean(AbstractScriptDatabaseInitializer.class)
|
||||||
|
@ConditionalOnSingleCandidate(DataSource.class)
|
||||||
|
@Import(DataSourceInitializationDependencyConfigurer.class)
|
||||||
|
class DataSourceInitializationConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
DataSourceScriptDatabaseInitializer dataSourceScriptDatabaseInitializer(DataSource dataSource,
|
||||||
|
SqlInitializationProperties initializationProperties) {
|
||||||
|
DatabaseInitializationSettings settings = SettingsCreator.createFrom(initializationProperties);
|
||||||
|
return new DataSourceScriptDatabaseInitializer(determineDataSource(dataSource,
|
||||||
|
initializationProperties.getUsername(), initializationProperties.getPassword()), settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static DataSource determineDataSource(DataSource dataSource, String username, String password) {
|
||||||
|
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
|
||||||
|
DataSourceBuilder.derivedFrom(dataSource).username(username).password(password)
|
||||||
|
.type(SimpleDriverDataSource.class).build();
|
||||||
|
}
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.sql.init;
|
||||||
|
|
||||||
|
import io.r2dbc.spi.ConnectionFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||||
|
import org.springframework.boot.r2dbc.ConnectionFactoryBuilder;
|
||||||
|
import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration for initializing an SQL database accessed via an R2DBC
|
||||||
|
* {@link ConnectionFactory}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
@ConditionalOnClass(ConnectionFactory.class)
|
||||||
|
@ConditionalOnSingleCandidate(ConnectionFactory.class)
|
||||||
|
class R2dbcInitializationConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
R2dbcScriptDatabaseInitializer r2dbcScriptDatabaseInitializer(ConnectionFactory connectionFactory,
|
||||||
|
SqlInitializationProperties properties) {
|
||||||
|
DatabaseInitializationSettings settings = SettingsCreator.createFrom(properties);
|
||||||
|
return new R2dbcScriptDatabaseInitializer(
|
||||||
|
determineConnectionFactory(connectionFactory, properties.getUsername(), properties.getPassword()),
|
||||||
|
settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static ConnectionFactory determineConnectionFactory(ConnectionFactory connectionFactory, String username,
|
||||||
|
String password) {
|
||||||
|
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
|
||||||
|
ConnectionFactoryBuilder.derivefrom(connectionFactory).username(username).password(password).build();
|
||||||
|
}
|
||||||
|
return connectionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.sql.init;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helpers class for creating {@link DatabaseInitializationSettings} from
|
||||||
|
* {@link SqlInitializationProperties}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
final class SettingsCreator {
|
||||||
|
|
||||||
|
private SettingsCreator() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static DatabaseInitializationSettings createFrom(SqlInitializationProperties properties) {
|
||||||
|
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||||
|
settings.setSchemaLocations(
|
||||||
|
scriptLocations(properties.getSchemaLocations(), "schema", properties.getPlatform()));
|
||||||
|
settings.setDataLocations(scriptLocations(properties.getDataLocations(), "data", properties.getPlatform()));
|
||||||
|
settings.setContinueOnError(properties.isContinueOnError());
|
||||||
|
settings.setSeparator(properties.getSeparator());
|
||||||
|
settings.setEncoding(properties.getEncoding());
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> scriptLocations(List<String> locations, String fallback, String platform) {
|
||||||
|
if (locations != null) {
|
||||||
|
return locations;
|
||||||
|
}
|
||||||
|
List<String> fallbackLocations = new ArrayList<>();
|
||||||
|
fallbackLocations.add("optional:classpath*:" + fallback + "-" + platform + ".sql");
|
||||||
|
fallbackLocations.add("optional:classpath*:" + fallback + ".sql");
|
||||||
|
return fallbackLocations;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,119 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.autoconfigure.sql.init;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import io.r2dbc.spi.ConnectionFactory;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration;
|
||||||
|
import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.r2dbc.init.R2dbcScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link SqlInitializationAutoConfiguration}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
public class SqlInitializationAutoConfigurationTests {
|
||||||
|
|
||||||
|
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(SqlInitializationAutoConfiguration.class)).withPropertyValues(
|
||||||
|
"spring.datasource.generate-unique-name:true", "spring.r2dbc.generate-unique-name:true");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenNoDataSourceOrConnectionFactoryIsAvailableThenAutoConfigurationBacksOff() {
|
||||||
|
this.contextRunner
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(AbstractScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenConnectionFactoryIsAvailableThenR2dbcInitializerIsAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(R2dbcAutoConfiguration.class))
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(R2dbcScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenConnectionFactoryIsAvailableAndInitializationIsDisabledThenInitializerIsNotAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(R2dbcAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.sql.init.enabled:false")
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(AbstractScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenDataSourceIsAvailableThenDataSourceInitializerIsAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(DataSourceScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenDataSourceIsAvailableAndInitializationIsDisabledThenInitializerIsNotAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.sql.init.enabled:false")
|
||||||
|
.run((context) -> assertThat(context).doesNotHaveBean(AbstractScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenDataSourceAndConnectionFactoryAreAvailableThenOnlyR2dbcInitializerIsAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(R2dbcAutoConfiguration.class))
|
||||||
|
.withUserConfiguration(DataSourceAutoConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(ConnectionFactory.class)
|
||||||
|
.hasSingleBean(DataSource.class).hasSingleBean(R2dbcScriptDatabaseInitializer.class)
|
||||||
|
.doesNotHaveBean(DataSourceScriptDatabaseInitializer.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenAnInitializerIsDefinedThenInitializerIsNotAutoConfigured() {
|
||||||
|
this.contextRunner.withConfiguration(AutoConfigurations.of(R2dbcAutoConfiguration.class))
|
||||||
|
.withUserConfiguration(DataSourceAutoConfiguration.class, DatabaseInitializerConfiguration.class)
|
||||||
|
.run((context) -> assertThat(context).hasSingleBean(AbstractScriptDatabaseInitializer.class)
|
||||||
|
.hasBean("customInitializer"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration(proxyBeanMethods = false)
|
||||||
|
static class DatabaseInitializerConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
AbstractScriptDatabaseInitializer customInitializer() {
|
||||||
|
return new AbstractScriptDatabaseInitializer(new DatabaseInitializationSettings()) {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void runScripts(List<Resource> resources, boolean continueOnError, String separator,
|
||||||
|
Charset encoding) {
|
||||||
|
// No-op
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.jdbc.init;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
|
||||||
|
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link InitializingBean} that performs {@link DataSource} initialization using schema
|
||||||
|
* (DDL) and data (DML) scripts.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public class DataSourceScriptDatabaseInitializer extends AbstractScriptDatabaseInitializer {
|
||||||
|
|
||||||
|
private final DataSource dataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@link DataSourceScriptDatabaseInitializer} that will initialize the
|
||||||
|
* given {@code DataSource} using the given settings.
|
||||||
|
* @param dataSource data source to initialize
|
||||||
|
* @param settings initialization settings
|
||||||
|
*/
|
||||||
|
public DataSourceScriptDatabaseInitializer(DataSource dataSource, DatabaseInitializationSettings settings) {
|
||||||
|
super(settings);
|
||||||
|
this.dataSource = dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the {@code DataSource} that will be initialized.
|
||||||
|
* @return the initialization data source
|
||||||
|
*/
|
||||||
|
protected final DataSource getDataSource() {
|
||||||
|
return this.dataSource;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void runScripts(List<Resource> resources, boolean continueOnError, String separator, Charset encoding) {
|
||||||
|
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||||
|
populator.setContinueOnError(continueOnError);
|
||||||
|
populator.setSeparator(separator);
|
||||||
|
if (encoding != null) {
|
||||||
|
populator.setSqlScriptEncoding(encoding.name());
|
||||||
|
}
|
||||||
|
for (Resource resource : resources) {
|
||||||
|
populator.addScript(resource);
|
||||||
|
}
|
||||||
|
DatabasePopulatorUtils.execute(populator, this.dataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.r2dbc.init;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.r2dbc.spi.ConnectionFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An {@link InitializingBean} that initializes a database represented by an R2DBC
|
||||||
|
* {@link ConnectionFactory}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
* @since 2.5.0
|
||||||
|
*/
|
||||||
|
public class R2dbcScriptDatabaseInitializer extends AbstractScriptDatabaseInitializer {
|
||||||
|
|
||||||
|
private final ConnectionFactory connectionFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new {@code R2dbcScriptDatabaseInitializer} that will initialize the
|
||||||
|
* database recognized by the given {@code connectionFactory} using the given
|
||||||
|
* {@code settings}.
|
||||||
|
* @param connectionFactory connectionFactory for the database
|
||||||
|
* @param settings initialization settings
|
||||||
|
*/
|
||||||
|
public R2dbcScriptDatabaseInitializer(ConnectionFactory connectionFactory,
|
||||||
|
DatabaseInitializationSettings settings) {
|
||||||
|
super(settings);
|
||||||
|
this.connectionFactory = connectionFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void runScripts(List<Resource> scripts, boolean continueOnError, String separator, Charset encoding) {
|
||||||
|
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||||
|
populator.setContinueOnError(continueOnError);
|
||||||
|
populator.setSeparator(separator);
|
||||||
|
if (encoding != null) {
|
||||||
|
populator.setSqlScriptEncoding(encoding.name());
|
||||||
|
}
|
||||||
|
for (Resource script : scripts) {
|
||||||
|
populator.addScript(script);
|
||||||
|
}
|
||||||
|
populator.populate(this.connectionFactory).block();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Support for initializaton of an SQL database using an R2DBC
|
||||||
|
* {@link io.r2dbc.spi.ConnectionFactory ConnectionFactory}.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.r2dbc.init;
|
@ -0,0 +1,20 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Support for initializaton of an SQL database.
|
||||||
|
*/
|
||||||
|
package org.springframework.boot.sql.init;
|
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.jdbc.init;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
|
||||||
|
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializerTests;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link DataSourceScriptDatabaseInitializer}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
class DataSourceScriptDatabaseInitializerTests extends AbstractScriptDatabaseInitializerTests {
|
||||||
|
|
||||||
|
private final HikariDataSource dataSource = DataSourceBuilder.create().type(HikariDataSource.class)
|
||||||
|
.url("jdbc:h2:mem:" + UUID.randomUUID()).build();
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void closeDataSource() {
|
||||||
|
this.dataSource.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AbstractScriptDatabaseInitializer createInitializer(DatabaseInitializationSettings settings) {
|
||||||
|
return new DataSourceScriptDatabaseInitializer(this.dataSource, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int numberOfRows(String sql) {
|
||||||
|
return new JdbcTemplate(this.dataSource).queryForObject(sql, Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012-2021 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.springframework.boot.r2dbc.init;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import io.r2dbc.spi.ConnectionFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.r2dbc.ConnectionFactoryBuilder;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializer;
|
||||||
|
import org.springframework.boot.sql.init.AbstractScriptDatabaseInitializerTests;
|
||||||
|
import org.springframework.boot.sql.init.DatabaseInitializationSettings;
|
||||||
|
import org.springframework.r2dbc.core.DatabaseClient;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests for {@link R2dbcScriptDatabaseInitializer}.
|
||||||
|
*
|
||||||
|
* @author Andy Wilkinson
|
||||||
|
*/
|
||||||
|
class R2dbcScriptDatabaseInitializerTests extends AbstractScriptDatabaseInitializerTests {
|
||||||
|
|
||||||
|
private final ConnectionFactory connectionFactory = ConnectionFactoryBuilder
|
||||||
|
.withUrl("r2dbc:h2:mem:///" + UUID.randomUUID() + "?options=DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE")
|
||||||
|
.build();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AbstractScriptDatabaseInitializer createInitializer(DatabaseInitializationSettings settings) {
|
||||||
|
return new R2dbcScriptDatabaseInitializer(this.connectionFactory, settings);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected int numberOfRows(String sql) {
|
||||||
|
return DatabaseClient.create(this.connectionFactory).sql(sql).map((row, metadata) -> row.get(0)).first()
|
||||||
|
.map((number) -> ((Number) number).intValue()).block();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,10 +1,2 @@
|
|||||||
CREATE TABLE CITY (
|
|
||||||
id INTEGER IDENTITY PRIMARY KEY,
|
|
||||||
name VARCHAR(30),
|
|
||||||
state VARCHAR(30),
|
|
||||||
country VARCHAR(30)
|
|
||||||
);
|
|
||||||
|
|
||||||
INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2000, 'Washington', 'DC', 'US');
|
INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2000, 'Washington', 'DC', 'US');
|
||||||
INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2001, 'San Francisco', 'CA', 'US');
|
INSERT INTO CITY (ID, NAME, STATE, COUNTRY) values (2001, 'San Francisco', 'CA', 'US');
|
||||||
|
|
@ -0,0 +1,6 @@
|
|||||||
|
CREATE TABLE CITY (
|
||||||
|
id INTEGER IDENTITY PRIMARY KEY,
|
||||||
|
name VARCHAR(30),
|
||||||
|
state VARCHAR(30),
|
||||||
|
country VARCHAR(30)
|
||||||
|
);
|
Loading…
Reference in New Issue