Skip to content

Commit 35d3cdb

Browse files
committed
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
1 parent db0ab9f commit 35d3cdb

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,7 +121,7 @@ private List<Resource> getScripts(List<String> locations, String type, ScriptLoc
121121
location = location.substring(OPTIONAL_LOCATION_PREFIX.length());
122122
}
123123
for (Resource resource : doGetResources(location, locationResolver)) {
124-
if (resource.exists()) {
124+
if (resource.isReadable()) {
125125
resources.add(resource);
126126
}
127127
else if (!optional) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ void whenDatabaseIsInitializedThenSchemaAndDataScriptsAreApplied() {
4444
assertThat(numberOfEmbeddedRows("SELECT COUNT(*) FROM EXAMPLE")).isEqualTo(1);
4545
}
4646

47+
@Test
48+
void whenDatabaseIsInitializedWithDirectoryLocationsThenFailureIsHelpful() {
49+
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
50+
settings.setSchemaLocations(Arrays.asList("/org/springframework/boot/sql/init"));
51+
settings.setDataLocations(Arrays.asList("/org/springframework/boot/sql/init"));
52+
T initializer = createEmbeddedDatabaseInitializer(settings);
53+
assertThatIllegalStateException().isThrownBy(initializer::initializeDatabase)
54+
.withMessage("No schema scripts found at location '/org/springframework/boot/sql/init'");
55+
}
56+
4757
@Test
4858
void whenContinueOnErrorIsFalseThenInitializationFailsOnError() {
4959
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();

0 commit comments

Comments
 (0)