Skip to content
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

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

Merged
merged 3 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion tools/upgradetool/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@ public static void main(String[] args) {
|| 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
* 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;
Expand Down Expand Up @@ -234,6 +236,49 @@ public void linkUpgradeJsProfile() {
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;

Expand Down