mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Make runScripts method of DB initializer more extensible
Closes gh-29137
This commit is contained in:
parent
403803837a
commit
3ab4606584
@ -16,9 +16,6 @@
|
||||
|
||||
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;
|
||||
@ -39,7 +36,6 @@ import org.springframework.boot.test.context.FilteredClassLoader;
|
||||
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 org.springframework.jdbc.datasource.init.DatabasePopulator;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ -162,8 +158,7 @@ class SqlInitializationAutoConfigurationTests {
|
||||
return new SqlDataSourceScriptDatabaseInitializer(null, new DatabaseInitializationSettings()) {
|
||||
|
||||
@Override
|
||||
protected void runScripts(List<Resource> resources, boolean continueOnError, String separator,
|
||||
Charset encoding) {
|
||||
protected void runScripts(Scripts scripts) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
@ -185,8 +180,7 @@ class SqlInitializationAutoConfigurationTests {
|
||||
return new DataSourceScriptDatabaseInitializer(null, new DatabaseInitializationSettings()) {
|
||||
|
||||
@Override
|
||||
protected void runScripts(List<Resource> resources, boolean continueOnError, String separator,
|
||||
Charset encoding) {
|
||||
protected void runScripts(Scripts scripts) {
|
||||
// No-op
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@ -16,9 +16,6 @@
|
||||
|
||||
package org.springframework.boot.jdbc.init;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
@ -76,14 +73,14 @@ public class DataSourceScriptDatabaseInitializer extends AbstractScriptDatabaseI
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runScripts(List<Resource> resources, boolean continueOnError, String separator, Charset encoding) {
|
||||
protected void runScripts(Scripts scripts) {
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.setContinueOnError(continueOnError);
|
||||
populator.setSeparator(separator);
|
||||
if (encoding != null) {
|
||||
populator.setSqlScriptEncoding(encoding.name());
|
||||
populator.setContinueOnError(scripts.isContinueOnError());
|
||||
populator.setSeparator(scripts.getSeparator());
|
||||
if (scripts.getEncoding() != null) {
|
||||
populator.setSqlScriptEncoding(scripts.getEncoding().name());
|
||||
}
|
||||
for (Resource resource : resources) {
|
||||
for (Resource resource : scripts) {
|
||||
populator.addScript(resource);
|
||||
}
|
||||
customize(populator);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@ -16,9 +16,6 @@
|
||||
|
||||
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;
|
||||
@ -58,12 +55,12 @@ public class R2dbcScriptDatabaseInitializer extends AbstractScriptDatabaseInitia
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void runScripts(List<Resource> scripts, boolean continueOnError, String separator, Charset encoding) {
|
||||
protected void runScripts(Scripts scripts) {
|
||||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.setContinueOnError(continueOnError);
|
||||
populator.setSeparator(separator);
|
||||
if (encoding != null) {
|
||||
populator.setSqlScriptEncoding(encoding.name());
|
||||
populator.setContinueOnError(scripts.isContinueOnError());
|
||||
populator.setSeparator(scripts.getSeparator());
|
||||
if (scripts.getEncoding() != null) {
|
||||
populator.setSqlScriptEncoding(scripts.getEncoding().name());
|
||||
}
|
||||
for (Resource script : scripts) {
|
||||
populator.addScript(script);
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2021 the original author or authors.
|
||||
* Copyright 2012-2022 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.
|
||||
@ -21,6 +21,7 @@ import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@ -142,12 +143,16 @@ public abstract class AbstractScriptDatabaseInitializer implements ResourceLoade
|
||||
}
|
||||
|
||||
private void runScripts(List<Resource> resources) {
|
||||
runScripts(resources, this.settings.isContinueOnError(), this.settings.getSeparator(),
|
||||
this.settings.getEncoding());
|
||||
runScripts(new Scripts(resources).continueOnError(this.settings.isContinueOnError())
|
||||
.separator(this.settings.getSeparator()).encoding(this.settings.getEncoding()));
|
||||
}
|
||||
|
||||
protected abstract void runScripts(List<Resource> resources, boolean continueOnError, String separator,
|
||||
Charset encoding);
|
||||
/**
|
||||
* Initialize the database by running the given {@code scripts}.
|
||||
* @param scripts the scripts to run
|
||||
* @since 3.0.0
|
||||
*/
|
||||
protected abstract void runScripts(Scripts scripts);
|
||||
|
||||
private static class ScriptLocationResolver {
|
||||
|
||||
@ -173,4 +178,57 @@ public abstract class AbstractScriptDatabaseInitializer implements ResourceLoade
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Scripts to be used to initialize the database.
|
||||
*
|
||||
* @since 3.0.0
|
||||
*/
|
||||
public static class Scripts implements Iterable<Resource> {
|
||||
|
||||
private final List<Resource> resources;
|
||||
|
||||
private boolean continueOnError = false;
|
||||
|
||||
private String separator = ";";
|
||||
|
||||
private Charset encoding;
|
||||
|
||||
public Scripts(List<Resource> resources) {
|
||||
this.resources = resources;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Resource> iterator() {
|
||||
return this.resources.iterator();
|
||||
}
|
||||
|
||||
public Scripts continueOnError(boolean continueOnError) {
|
||||
this.continueOnError = continueOnError;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isContinueOnError() {
|
||||
return this.continueOnError;
|
||||
}
|
||||
|
||||
public Scripts separator(String separator) {
|
||||
this.separator = separator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getSeparator() {
|
||||
return this.separator;
|
||||
}
|
||||
|
||||
public Scripts encoding(Charset encoding) {
|
||||
this.encoding = encoding;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Charset getEncoding() {
|
||||
return this.encoding;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user