From 35d3cdbe74afc807dcd1c41b200e9bcee428020a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 8 Aug 2023 11:20:07 +0100 Subject: [PATCH] Fail more helpfully when schema or data location is a directory Previously a schema or data locatio that was a directory would result in an attempt being made to apply the directory listing as SQL scripts. This would typically result in a hard to diagnose failure due to the directory listing not being valid SQL. This commit updates the initializer to ignore locations for which the Resources is not readable. This works as Framework's Resource abstraction does not consider directory resources to be readable. Closes gh-36386 --- .../sql/init/AbstractScriptDatabaseInitializer.java | 4 ++-- .../init/AbstractScriptDatabaseInitializerTests.java | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java index 4ab0661905..782a238f07 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2023 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. @@ -121,7 +121,7 @@ public abstract class AbstractScriptDatabaseInitializer implements ResourceLoade location = location.substring(OPTIONAL_LOCATION_PREFIX.length()); } for (Resource resource : doGetResources(location, locationResolver)) { - if (resource.exists()) { + if (resource.isReadable()) { resources.add(resource); } else if (!optional) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java index 3689bca1dd..1c091bf8a5 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java @@ -44,6 +44,16 @@ public abstract class AbstractScriptDatabaseInitializerTests