-
-
Notifications
You must be signed in to change notification settings - Fork 471
Fix persistence upgrader #5211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix persistence upgrader #5211
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,6 +12,7 @@ | |||||||||||||||||||
| */ | ||||||||||||||||||||
| package org.openhab.core.tools.internal; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||
| import java.nio.file.Files; | ||||||||||||||||||||
| import java.nio.file.Path; | ||||||||||||||||||||
| import java.util.Collection; | ||||||||||||||||||||
|
|
@@ -20,8 +21,10 @@ | |||||||||||||||||||
|
|
||||||||||||||||||||
| import org.eclipse.jdt.annotation.NonNullByDefault; | ||||||||||||||||||||
| import org.eclipse.jdt.annotation.Nullable; | ||||||||||||||||||||
| import org.openhab.core.persistence.dto.PersistenceCronStrategyDTO; | ||||||||||||||||||||
| import org.openhab.core.persistence.dto.PersistenceItemConfigurationDTO; | ||||||||||||||||||||
| import org.openhab.core.persistence.dto.PersistenceServiceConfigurationDTO; | ||||||||||||||||||||
| import org.openhab.core.persistence.strategy.PersistenceStrategy; | ||||||||||||||||||||
| import org.openhab.core.storage.json.internal.JsonStorage; | ||||||||||||||||||||
| import org.openhab.core.tools.Upgrader; | ||||||||||||||||||||
| import org.slf4j.Logger; | ||||||||||||||||||||
|
|
@@ -57,11 +60,31 @@ | |||||||||||||||||||
| return false; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| List<String> managedConfigs; | ||||||||||||||||||||
| try { | ||||||||||||||||||||
| managedConfigs = managedPersistenceConfigs(installedPersistenceAddons(userdataPath), | ||||||||||||||||||||
| unmanagedPersistenceConfigs(confPath)); | ||||||||||||||||||||
|
Check failure on line 66 in tools/upgradetool/src/main/java/org/openhab/core/tools/internal/PersistenceUpgrader.java
|
||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||
| logger.error(e.getMessage()); | ||||||||||||||||||||
| return false; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| if (managedConfigs.isEmpty()) { | ||||||||||||||||||||
| // No managed persistence configurations, so no need to upgrade | ||||||||||||||||||||
| return true; | ||||||||||||||||||||
| } | ||||||||||||||||||||
|
|
||||||||||||||||||||
| Path persistenceJsonDatabasePath = userdataPath | ||||||||||||||||||||
| .resolve(Path.of("jsondb", "org.openhab.core.persistence.PersistenceServiceConfiguration.json")); | ||||||||||||||||||||
| if (Files.notExists(persistenceJsonDatabasePath)) { | ||||||||||||||||||||
| // No managed persistence configurations, so no need to upgrade | ||||||||||||||||||||
| return true; | ||||||||||||||||||||
| // No configuration, but persistence addons are installed and there is no unmanaged configuration for it, so | ||||||||||||||||||||
| // it needs to be created | ||||||||||||||||||||
| try { | ||||||||||||||||||||
|
||||||||||||||||||||
| try { | |
| try { | |
| Path parentDir = persistenceJsonDatabasePath.getParent(); | |
| if (parentDir != null) { | |
| Files.createDirectories(parentDir); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parent directory will be present => jsondb directory is created by openHAB.
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Objects.requireNonNull() call is unnecessary since configLines.get(i) will never return null for a valid list index. The call also causes confusion about the actual nullability contract. Remove this call to improve code clarity.
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The loop variable i is declared but never used except to iterate through the list. Replace the traditional for-loop with an enhanced for-loop to improve readability and eliminate the unused loop variable.
| for (int i = 0; i < configLines.size(); i++) { | |
| String line = Objects.requireNonNull(configLines.get(i)); | |
| for (String lineRaw : configLines) { | |
| String line = Objects.requireNonNull(lineRaw); |
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Path.endsWith() method expects a Path argument, not a String. This call should use getFileName().toString().endsWith(".persist") to properly check the file extension. The current code will cause a compilation error or unexpected behavior.
| return Files.list(persistenceConfigPath).filter(configFile -> configFile.endsWith(".persist")) | |
| return Files.list(persistenceConfigPath) | |
| .filter(configFile -> configFile.getFileName().toString().endsWith(".persist")) |
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method does not handle the case where the persistence directory does not exist, which will cause an IOException when calling Files.list(). Consider checking if the directory exists first and returning an empty list if it doesn't, rather than propagating the exception.
| return Files.list(persistenceConfigPath).filter(configFile -> configFile.endsWith(".persist")) | |
| .map(configFile -> configFile.getFileName().toString().replace(".persist", "")).toList(); | |
| if (Files.notExists(persistenceConfigPath)) { | |
| return List.of(); | |
| } | |
| try (var stream = Files.list(persistenceConfigPath)) { | |
| return stream.filter(configFile -> configFile.endsWith(".persist")) | |
| .map(configFile -> configFile.getFileName().toString().replace(".persist", "")).toList(); | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The $OPENHAB_CONF/persistence path will always exist. (If it doesn't there serious things went wrong.)
Copilot
AI
Dec 19, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Files.list() operation returns a Stream that must be closed to avoid resource leaks. Wrap this operation in a try-with-resources block to ensure the stream is properly closed after use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
confPathparameter is not null-checked before being passed tounmanagedPersistenceConfigs(). IfconfPathis null, this will cause a NullPointerException when the method tries to resolve the persistence path. Add a null check forconfPathsimilar to the check foruserdataPath.