Upgrade tool: Add upgrade task for script profile changes (#4117)

* Upgrade tool: Add upgrade task for script profile changes

Refs #4058.

Signed-off-by: Florian Hotze <florianh_dev@icloud.com>
This commit is contained in:
Florian Hotze 2024-03-02 10:41:34 +01:00 committed by GitHub
parent 3f27104e76
commit 45f8bff876
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 2 deletions

View File

@ -15,7 +15,7 @@
<packaging>jar</packaging>
<name>openHAB Core :: Tools :: Upgrade tool</name>
<description>A tool for upgrading openHAB from 3.4 to 4.0</description>
<description>A tool for upgrading openHAB</description>
<dependencies>
<dependency>

View File

@ -82,10 +82,15 @@ public class UpgradeTool {
|| LINK_UPGRADE_JS_PROFILE.equals(commandLine.getOptionValue(OPT_COMMAND))) {
upgrader.linkUpgradeJsProfile();
}
if (!commandLine.hasOption(OPT_COMMAND)
|| LINK_UPGRADE_SCRIPT_PROFILE.equals(commandLine.getOptionValue(OPT_COMMAND))) {
upgrader.linkUpgradeScriptProfile();
}
}
} catch (ParseException e) {
HelpFormatter formatter = new HelpFormatter();
String commands = Set.of(ITEM_COPY_UNIT_TO_METADATA, LINK_UPGRADE_JS_PROFILE).toString();
String commands = Set.of(ITEM_COPY_UNIT_TO_METADATA, LINK_UPGRADE_JS_PROFILE, LINK_UPGRADE_SCRIPT_PROFILE)
.toString();
formatter.printHelp("upgradetool", "", options, "Available commands: " + commands, true);
}

View File

@ -42,11 +42,13 @@ import org.slf4j.LoggerFactory;
* The {@link Upgrader} contains the implementation of the upgrade methods
*
* @author Jan N. Klug - Initial contribution
* @author Florian Hotze - Add script profile upgrade
*/
@NonNullByDefault
public class Upgrader {
public static final String ITEM_COPY_UNIT_TO_METADATA = "itemCopyUnitToMetadata";
public static final String LINK_UPGRADE_JS_PROFILE = "linkUpgradeJsProfile";
public static final String LINK_UPGRADE_SCRIPT_PROFILE = "linkUpgradeScriptProfile";
private final Logger logger = LoggerFactory.getLogger(Upgrader.class);
private final String baseDir;
@ -234,6 +236,49 @@ public class Upgrader {
upgradeRecords.flush();
}
/**
* Upgrades the ItemChannelLink database for the separation of {@code toHandlerScript} into
* {@code commandFromItemScript} and {@code stateFromItemScript}.
* See <a href="https://github.com/openhab/openhab-core/pull/4058">openhab/openhab-core#4058</a>.
*/
public void linkUpgradeScriptProfile() {
if (!checkUpgradeRecord(LINK_UPGRADE_SCRIPT_PROFILE)) {
return;
}
Path linkJsonDatabasePath = Path.of(baseDir, "jsondb", "org.openhab.core.thing.link.ItemChannelLink.json");
logger.info("Upgrading script profile configuration in database '{}'", linkJsonDatabasePath);
if (!Files.isWritable(linkJsonDatabasePath)) {
logger.error("Cannot access link database '{}', check path and access rights.", linkJsonDatabasePath);
return;
}
JsonStorage<ItemChannelLink> linkStorage = new JsonStorage<>(linkJsonDatabasePath.toFile(), null, 5, 0, 0,
List.of());
List.copyOf(linkStorage.getKeys()).forEach(linkUid -> {
ItemChannelLink link = Objects.requireNonNull(linkStorage.get(linkUid));
Configuration configuration = link.getConfiguration();
String profileName = (String) configuration.get(ItemChannelLinkConfigDescriptionProvider.PARAM_PROFILE);
if (profileName.matches("^transform:*")) {
String toHandlerScript = (String) configuration.get("toHandlerScript");
if (toHandlerScript != null) {
configuration.put("commandFromItemScript", toHandlerScript);
configuration.remove("toHandlerScript");
linkStorage.put(linkUid, link);
logger.info("{}: rewrote script profile link to new format", linkUid);
} else {
logger.info("{}: link already has correct configuration", linkUid);
}
}
});
linkStorage.flush();
upgradeRecords.put(LINK_UPGRADE_SCRIPT_PROFILE, new UpgradeRecord(ZonedDateTime.now()));
upgradeRecords.flush();
}
private static class UpgradeRecord {
public final String executionDate;