Skip to content

Commit

Permalink
Allow platform to be configured in DataSourceInitializers
Browse files Browse the repository at this point in the history
Closes gh-28932
  • Loading branch information
snicoll committed Dec 14, 2021
1 parent d18eae3 commit 157b3aa
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Initialize the Spring Batch schema (ignoring errors, so it should be idempotent).
Expand Down Expand Up @@ -54,6 +55,10 @@ protected String getSchemaLocation() {

@Override
protected String getDatabaseName() {
String platform = this.jdbcProperties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
String databaseName = super.getDatabaseName();
if ("oracle".equals(databaseName)) {
return "oracle10g";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public static class Jdbc {
*/
private String schema = DEFAULT_SCHEMA_LOCATION;

/**
* Platform to use in initialization scripts if the @@platform@@ placeholder is
* used. Auto-detected by default.
*/
private String platform;

/**
* Table prefix for all the batch meta-data tables.
*/
Expand All @@ -140,6 +146,14 @@ public void setSchema(String schema) {
this.schema = schema;
}

public String getPlatform() {
return this.platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public String getTablePrefix() {
return this.tablePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand All @@ -22,6 +22,7 @@
import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Initializer for Spring Integration schema.
Expand Down Expand Up @@ -50,4 +51,13 @@ protected String getSchemaLocation() {
return this.properties.getSchema();
}

@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ public static class Jdbc {
*/
private String schema = DEFAULT_SCHEMA_LOCATION;

/**
* Platform to use in initialization scripts if the @@platform@@ placeholder is
* used. Auto-detected by default.
*/
private String platform;

/**
* Database schema initialization mode.
*/
Expand All @@ -209,6 +215,14 @@ public void setSchema(String schema) {
this.schema = schema;
}

public String getPlatform() {
return this.platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public DataSourceInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Initialize the Quartz Scheduler schema.
Expand Down Expand Up @@ -58,6 +59,10 @@ protected String getSchemaLocation() {

@Override
protected String getDatabaseName() {
String platform = this.properties.getJdbc().getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
String databaseName = super.getDatabaseName();
if ("db2".equals(databaseName)) {
return "db2_v95";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand Down Expand Up @@ -141,6 +141,12 @@ public static class Jdbc {
*/
private String schema = DEFAULT_SCHEMA_LOCATION;

/**
* Platform to use in initialization scripts if the @@platform@@ placeholder is
* used. Auto-detected by default.
*/
private String platform;

/**
* Database schema initialization mode.
*/
Expand All @@ -159,6 +165,14 @@ public void setSchema(String schema) {
this.schema = schema;
}

public String getPlatform() {
return this.platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public DataSourceInitializationMode getInitializeSchema() {
return this.initializeSchema;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand All @@ -22,6 +22,7 @@
import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
* Initializer for Spring Session schema.
Expand Down Expand Up @@ -50,4 +51,13 @@ protected String getSchemaLocation() {
return this.properties.getSchema();
}

@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand Down Expand Up @@ -42,6 +42,12 @@ public class JdbcSessionProperties {
*/
private String schema = DEFAULT_SCHEMA_LOCATION;

/**
* Platform to use in initialization scripts if the @@platform@@ placeholder is used.
* Auto-detected by default.
*/
private String platform;

/**
* Name of the database table used to store sessions.
*/
Expand Down Expand Up @@ -77,6 +83,14 @@ public void setSchema(String schema) {
this.schema = schema;
}

public String getPlatform() {
return this.platform;
}

public void setPlatform(String platform) {
this.platform = platform;
}

public String getTableName() {
return this.tableName;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.autoconfigure.batch;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;

import org.springframework.core.io.DefaultResourceLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

/**
* Tests for {@link BatchDataSourceInitializer}.
*
* @author Stephane Nicoll
*/
class BatchDataSourceInitializerTests {

@Test
void getDatabaseNameWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
BatchProperties properties = new BatchProperties();
properties.getJdbc().setPlatform("test");
BatchDataSourceInitializer initializer = new BatchDataSourceInitializer(dataSource, new DefaultResourceLoader(),
properties);
assertThat(initializer.getDatabaseName()).isEqualTo("test");
verifyNoInteractions(dataSource);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.autoconfigure.integration;

import javax.sql.DataSource;

import org.junit.jupiter.api.Test;

import org.springframework.core.io.DefaultResourceLoader;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

/**
* Tests for {@link IntegrationDataSourceInitializer}.
*
* @author Stephane Nicoll
*/
class IntegrationDataSourceInitializerTests {

@Test
void getDatabaseNameWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
IntegrationProperties properties = new IntegrationProperties();
properties.getJdbc().setPlatform("test");
IntegrationDataSourceInitializer initializer = new IntegrationDataSourceInitializer(dataSource,
new DefaultResourceLoader(), properties);
assertThat(initializer.getDatabaseName()).isEqualTo("test");
verifyNoInteractions(dataSource);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* 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.
Expand Down Expand Up @@ -30,10 +30,13 @@
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.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;

/**
* Tests for {@link QuartzDataSourceInitializer}.
Expand All @@ -48,6 +51,17 @@ class QuartzDataSourceInitializerTests {
.withPropertyValues("spring.datasource.url=" + String.format(
"jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", UUID.randomUUID().toString()));

@Test
void getDatabaseNameWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
QuartzProperties properties = new QuartzProperties();
properties.getJdbc().setPlatform("test");
QuartzDataSourceInitializer initializer = new QuartzDataSourceInitializer(dataSource,
new DefaultResourceLoader(), properties);
assertThat(initializer.getDatabaseName()).isEqualTo("test");
verifyNoInteractions(dataSource);
}

@Test
void hashIsUsedAsACommentPrefixByDefault() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(
Expand Down

0 comments on commit 157b3aa

Please sign in to comment.