Allow configuration to specify randomly generated database name
See gh-7004
This commit is contained in:
parent
0f97ccf2fa
commit
7fcb197092
|
|
@ -57,6 +57,11 @@ public class DataSourceProperties
|
||||||
*/
|
*/
|
||||||
private String name = "testdb";
|
private String name = "testdb";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If <code>true</code> the database name is randomly generated.
|
||||||
|
*/
|
||||||
|
private boolean generateName = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fully qualified name of the connection pool implementation to use. By default, it
|
* Fully qualified name of the connection pool implementation to use. By default, it
|
||||||
* is auto-detected from the classpath.
|
* is auto-detected from the classpath.
|
||||||
|
|
@ -183,6 +188,14 @@ public class DataSourceProperties
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isGenerateName() {
|
||||||
|
return this.generateName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGenerateName(boolean generateName) {
|
||||||
|
this.generateName = generateName;
|
||||||
|
}
|
||||||
|
|
||||||
public Class<? extends DataSource> getType() {
|
public Class<? extends DataSource> getType() {
|
||||||
return this.type;
|
return this.type;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,13 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware {
|
||||||
public EmbeddedDatabase dataSource() {
|
public EmbeddedDatabase dataSource() {
|
||||||
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
|
||||||
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
|
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
|
||||||
this.database = builder.setName(this.properties.getName()).build();
|
if (this.properties.isGenerateName()) {
|
||||||
|
builder.generateUniqueName(true);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
builder.setName(this.properties.getName());
|
||||||
|
}
|
||||||
|
this.database = builder.build();
|
||||||
return this.database;
|
return this.database;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,11 +16,17 @@
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.jdbc;
|
package org.springframework.boot.autoconfigure.jdbc;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
import org.springframework.core.env.PropertiesPropertySource;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
@ -42,4 +48,34 @@ public class EmbeddedDataSourceConfigurationTests {
|
||||||
this.context.close();
|
this.context.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generatesUniqueDatabaseName() throws Exception {
|
||||||
|
Properties myProps = new Properties();
|
||||||
|
myProps.setProperty("spring.datasource.generate-name", "true");
|
||||||
|
|
||||||
|
this.context = new AnnotationConfigApplicationContext();
|
||||||
|
this.context.register(EmbeddedDataSourceConfiguration.class);
|
||||||
|
this.context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("whatever", myProps));
|
||||||
|
this.context.refresh();
|
||||||
|
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||||
|
assertThat(getDatabaseName(dataSource)).isNotEqualToIgnoringCase("testdb");
|
||||||
|
this.context.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getDatabaseName(DataSource dataSource) throws SQLException {
|
||||||
|
Connection connection = dataSource.getConnection();
|
||||||
|
try {
|
||||||
|
ResultSet catalogs = connection.getMetaData().getCatalogs();
|
||||||
|
if (catalogs.next()) {
|
||||||
|
return catalogs.getString(1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw new IllegalStateException("Unable to get database name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue