Generalize script-based DB initialization and add R2DBC initializer
See gh-24741pull/25829/head
parent
eb1200415d
commit
9cc7f0b54d
@ -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();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue