Skip to content

Allow configuration to specify randomly generated database name #7004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public class DataSourceProperties
*/
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
* is auto-detected from the classpath.
Expand Down Expand Up @@ -183,6 +188,14 @@ public void setName(String name) {
this.name = name;
}

public boolean isGenerateName() {
return this.generateName;
}

public void setGenerateName(boolean generateName) {
this.generateName = generateName;
}

public Class<? extends DataSource> getType() {
return this.type;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ public void setBeanClassLoader(ClassLoader classLoader) {
public EmbeddedDatabase dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder()
.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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@

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 org.junit.Test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.PropertiesPropertySource;

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

Expand All @@ -42,4 +48,34 @@ public void testDefaultEmbeddedDatabase() throws Exception {
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();
}
}

}