Skip to content

Support initialization on different table names in the Spring Sesssion database initializer #33748

Open
@alexanderankin

Description

@alexanderankin

I was reading #6649 and thought that since the fix got lost in the 2.1 merge (its here but not here) - why not support it fully. I can submit the original fix if that is preferable, but at work we use Mongo and i thought it was nice that you could customize the collection name, so thought it would work the same way on jdbc.

I don't mind submitting a PR to restore the intended functionality either, but making it fully functional seems preferable if possible.

i figure the fix could look something like this
diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceScriptDatabaseInitializer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceScriptDatabaseInitializer.java
index a5279ecb45..4669897a74 100644
--- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceScriptDatabaseInitializer.java
+++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/JdbcSessionDataSourceScriptDatabaseInitializer.java
@@ -16,6 +16,9 @@
 
 package org.springframework.boot.autoconfigure.session;
 
+import java.io.IOException;
+import java.nio.charset.Charset;
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.sql.DataSource;
@@ -24,6 +27,8 @@ import org.springframework.boot.jdbc.DatabaseDriver;
 import org.springframework.boot.jdbc.init.DataSourceScriptDatabaseInitializer;
 import org.springframework.boot.jdbc.init.PlatformPlaceholderDatabaseDriverResolver;
 import org.springframework.boot.sql.init.DatabaseInitializationSettings;
+import org.springframework.core.io.ByteArrayResource;
+import org.springframework.core.io.Resource;
 import org.springframework.util.StringUtils;
 
 /**
@@ -38,6 +43,9 @@ import org.springframework.util.StringUtils;
  */
 public class JdbcSessionDataSourceScriptDatabaseInitializer extends DataSourceScriptDatabaseInitializer {
 
+	private final DatabaseInitializationSettings settings;
+	private JdbcSessionProperties properties;
+
 	/**
 	 * Create a new {@link JdbcSessionDataSourceScriptDatabaseInitializer} instance.
 	 * @param dataSource the Spring Session JDBC data source
@@ -46,6 +54,7 @@ public class JdbcSessionDataSourceScriptDatabaseInitializer extends DataSourceSc
 	 */
 	public JdbcSessionDataSourceScriptDatabaseInitializer(DataSource dataSource, JdbcSessionProperties properties) {
 		this(dataSource, getSettings(dataSource, properties));
+		this.properties = properties;
 	}
 
 	/**
@@ -57,6 +66,32 @@ public class JdbcSessionDataSourceScriptDatabaseInitializer extends DataSourceSc
 	public JdbcSessionDataSourceScriptDatabaseInitializer(DataSource dataSource,
 			DatabaseInitializationSettings settings) {
 		super(dataSource, settings);
+		this.settings = settings;
+	}
+
+	@Override
+	protected void runScripts(Scripts scripts) {
+		// if the table name is not the default table name, replace table name in scripts
+		if (!properties.getTableName().equals(new JdbcSessionProperties().getTableName())) {
+			scripts = fixSchemaWithCorrectTableName(scripts);
+		}
+
+		super.runScripts(scripts);
+	}
+
+	private Scripts fixSchemaWithCorrectTableName(Scripts scripts) {
+		Charset cs = settings.getEncoding() != null ? scripts.getEncoding() : Charset.defaultCharset();
+		ArrayList<Resource> resources = new ArrayList<>();
+		for (Resource resource : scripts) {
+			try {
+				resources.add(new ByteArrayResource(new String(resource.getInputStream().readAllBytes(), cs)
+						.replaceAll(new JdbcSessionProperties().getTableName(), properties.getTableName())
+						.getBytes(cs)));
+			} catch (IOException e) {
+				resources.add(resource);
+			}
+		}
+		return new Scripts(resources);
 	}
 
 	/**

as the core implementation, with tests and such to follow, if the approach seems generally acceptable.

thanks for reading!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions