Allow platform to be configured in DataSourceInitializers

Closes gh-28932
This commit is contained in:
Stephane Nicoll 2021-12-13 14:42:02 +01:00
parent d18eae35e5
commit 157b3aa25f
12 changed files with 246 additions and 5 deletions

View File

@ -23,6 +23,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Initialize the Spring Batch schema (ignoring errors, so it should be idempotent). * Initialize the Spring Batch schema (ignoring errors, so it should be idempotent).
@ -54,6 +55,10 @@ public class BatchDataSourceInitializer extends AbstractDataSourceInitializer {
@Override @Override
protected String getDatabaseName() { protected String getDatabaseName() {
String platform = this.jdbcProperties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
String databaseName = super.getDatabaseName(); String databaseName = super.getDatabaseName();
if ("oracle".equals(databaseName)) { if ("oracle".equals(databaseName)) {
return "oracle10g"; return "oracle10g";

View File

@ -122,6 +122,12 @@ public class BatchProperties {
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; 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. * Table prefix for all the batch meta-data tables.
*/ */
@ -140,6 +146,14 @@ public class BatchProperties {
this.schema = schema; this.schema = schema;
} }
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getTablePrefix() { public String getTablePrefix() {
return this.tablePrefix; return this.tablePrefix;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Initializer for Spring Integration schema. * Initializer for Spring Integration schema.
@ -50,4 +51,13 @@ public class IntegrationDataSourceInitializer extends AbstractDataSourceInitiali
return this.properties.getSchema(); return this.properties.getSchema();
} }
@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}
} }

View File

@ -196,6 +196,12 @@ public class IntegrationProperties {
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; 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. * Database schema initialization mode.
*/ */
@ -209,6 +215,14 @@ public class IntegrationProperties {
this.schema = schema; this.schema = schema;
} }
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public DataSourceInitializationMode getInitializeSchema() { public DataSourceInitializationMode getInitializeSchema() {
return this.initializeSchema; return this.initializeSchema;
} }

View File

@ -23,6 +23,7 @@ import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Initialize the Quartz Scheduler schema. * Initialize the Quartz Scheduler schema.
@ -58,6 +59,10 @@ public class QuartzDataSourceInitializer extends AbstractDataSourceInitializer {
@Override @Override
protected String getDatabaseName() { protected String getDatabaseName() {
String platform = this.properties.getJdbc().getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
String databaseName = super.getDatabaseName(); String databaseName = super.getDatabaseName();
if ("db2".equals(databaseName)) { if ("db2".equals(databaseName)) {
return "db2_v95"; return "db2_v95";

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -141,6 +141,12 @@ public class QuartzProperties {
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; 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. * Database schema initialization mode.
*/ */
@ -159,6 +165,14 @@ public class QuartzProperties {
this.schema = schema; this.schema = schema;
} }
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public DataSourceInitializationMode getInitializeSchema() { public DataSourceInitializationMode getInitializeSchema() {
return this.initializeSchema; return this.initializeSchema;
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
import org.springframework.boot.jdbc.DataSourceInitializationMode; import org.springframework.boot.jdbc.DataSourceInitializationMode;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/** /**
* Initializer for Spring Session schema. * Initializer for Spring Session schema.
@ -50,4 +51,13 @@ public class JdbcSessionDataSourceInitializer extends AbstractDataSourceInitiali
return this.properties.getSchema(); return this.properties.getSchema();
} }
@Override
protected String getDatabaseName() {
String platform = this.properties.getPlatform();
if (StringUtils.hasText(platform)) {
return platform;
}
return super.getDatabaseName();
}
} }

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -42,6 +42,12 @@ public class JdbcSessionProperties {
*/ */
private String schema = DEFAULT_SCHEMA_LOCATION; 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. * Name of the database table used to store sessions.
*/ */
@ -77,6 +83,14 @@ public class JdbcSessionProperties {
this.schema = schema; this.schema = schema;
} }
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public String getTableName() { public String getTableName() {
return this.tableName; return this.tableName;
} }

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,10 +30,13 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex
import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
/** /**
* Tests for {@link QuartzDataSourceInitializer}. * Tests for {@link QuartzDataSourceInitializer}.
@ -48,6 +51,17 @@ class QuartzDataSourceInitializerTests {
.withPropertyValues("spring.datasource.url=" + String.format( .withPropertyValues("spring.datasource.url=" + String.format(
"jdbc:h2:mem:test-%s;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE", UUID.randomUUID().toString())); "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 @Test
void hashIsUsedAsACommentPrefixByDefault() { void hashIsUsedAsACommentPrefixByDefault() {
this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues( this.contextRunner.withUserConfiguration(TestConfiguration.class).withPropertyValues(

View File

@ -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.session;
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 JdbcSessionDataSourceInitializer}.
*
* @author Stephane Nicoll
*/
class JdbcSessionDataSourceInitializerTests {
@Test
void getDatabaseNameWithPlatformDoesNotTouchDataSource() {
DataSource dataSource = mock(DataSource.class);
JdbcSessionProperties properties = new JdbcSessionProperties();
properties.setPlatform("test");
JdbcSessionDataSourceInitializer initializer = new JdbcSessionDataSourceInitializer(dataSource,
new DefaultResourceLoader(), properties);
assertThat(initializer.getDatabaseName()).isEqualTo("test");
verifyNoInteractions(dataSource);
}
}