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
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -44,6 +44,16 @@ public abstract class AbstractScriptDatabaseInitializerTests<T extends AbstractS
|
||||
assertThat(numberOfEmbeddedRows("SELECT COUNT(*) FROM EXAMPLE")).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenDatabaseIsInitializedWithDirectoryLocationsThenFailureIsHelpful() {
|
||||
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||
settings.setSchemaLocations(Arrays.asList("/org/springframework/boot/sql/init"));
|
||||
settings.setDataLocations(Arrays.asList("/org/springframework/boot/sql/init"));
|
||||
T initializer = createEmbeddedDatabaseInitializer(settings);
|
||||
assertThatIllegalStateException().isThrownBy(initializer::initializeDatabase)
|
||||
.withMessage("No schema scripts found at location '/org/springframework/boot/sql/init'");
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenContinueOnErrorIsFalseThenInitializationFailsOnError() {
|
||||
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
|
||||
|
||||
Reference in New Issue
Block a user