Skip to content

Commit 7fcb197

Browse files
robfletchersnicoll
authored andcommitted
Allow configuration to specify randomly generated database name
See gh-7004
1 parent 0f97ccf commit 7fcb197

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public class DataSourceProperties
5757
*/
5858
private String name = "testdb";
5959

60+
/**
61+
* If <code>true</code> the database name is randomly generated.
62+
*/
63+
private boolean generateName = false;
64+
6065
/**
6166
* Fully qualified name of the connection pool implementation to use. By default, it
6267
* is auto-detected from the classpath.
@@ -183,6 +188,14 @@ public void setName(String name) {
183188
this.name = name;
184189
}
185190

191+
public boolean isGenerateName() {
192+
return this.generateName;
193+
}
194+
195+
public void setGenerateName(boolean generateName) {
196+
this.generateName = generateName;
197+
}
198+
186199
public Class<? extends DataSource> getType() {
187200
return this.type;
188201
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public void setBeanClassLoader(ClassLoader classLoader) {
5555
public EmbeddedDatabase dataSource() {
5656
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
5757
.setType(EmbeddedDatabaseConnection.get(this.classLoader).getType());
58-
this.database = builder.setName(this.properties.getName()).build();
58+
if (this.properties.isGenerateName()) {
59+
builder.generateUniqueName(true);
60+
}
61+
else {
62+
builder.setName(this.properties.getName());
63+
}
64+
this.database = builder.build();
5965
return this.database;
6066
}
6167

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfigurationTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,17 @@
1616

1717
package org.springframework.boot.autoconfigure.jdbc;
1818

19+
import java.sql.Connection;
20+
import java.sql.ResultSet;
21+
import java.sql.SQLException;
22+
import java.util.Properties;
23+
1924
import javax.sql.DataSource;
2025

2126
import org.junit.Test;
2227

2328
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
29+
import org.springframework.core.env.PropertiesPropertySource;
2430

2531
import static org.assertj.core.api.Assertions.assertThat;
2632

@@ -42,4 +48,34 @@ public void testDefaultEmbeddedDatabase() throws Exception {
4248
this.context.close();
4349
}
4450

51+
@Test
52+
public void generatesUniqueDatabaseName() throws Exception {
53+
Properties myProps = new Properties();
54+
myProps.setProperty("spring.datasource.generate-name", "true");
55+
56+
this.context = new AnnotationConfigApplicationContext();
57+
this.context.register(EmbeddedDataSourceConfiguration.class);
58+
this.context.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource("whatever", myProps));
59+
this.context.refresh();
60+
DataSource dataSource = this.context.getBean(DataSource.class);
61+
assertThat(getDatabaseName(dataSource)).isNotEqualToIgnoringCase("testdb");
62+
this.context.close();
63+
}
64+
65+
private String getDatabaseName(DataSource dataSource) throws SQLException {
66+
Connection connection = dataSource.getConnection();
67+
try {
68+
ResultSet catalogs = connection.getMetaData().getCatalogs();
69+
if (catalogs.next()) {
70+
return catalogs.getString(1);
71+
}
72+
else {
73+
throw new IllegalStateException("Unable to get database name");
74+
}
75+
}
76+
finally {
77+
connection.close();
78+
}
79+
}
80+
4581
}

0 commit comments

Comments
 (0)