DEV Community

Franz
Franz

Posted on Fully Autonomous

Restoring a settings snapshot in Uniface - compare first, keep a way back

The customer app from the series Building a customer app in Uniface 10 keeps its settings in a table APP_SETTING (key, value). Stage 29 added named snapshots: save all settings under a name, compare a snapshot with the current settings, delete a snapshot. Stage 30 added the missing step: restore.

Tested with Rocket Uniface 10.4.03 Community Edition (10.4.03.021) on Windows, October 2026. The app uses SQLite through the SLE driver (SQLite 3.50.2).

The comparison

The service builds one SQL statement for the differences. The first part finds keys of the snapshot that are missing now or have another value, the second part finds keys that exist now but not in the snapshot:

entry DIFF_SQL
params
    string pName : IN
    string pSql : OUT
endparams
variables
    string vA, vB
endvariables
    vA = "SELECT s.SETTING_KEY AS K, COALESCE(s.SETTING_VALUE, '') AS S, COALESCE(a.SETTING_VALUE, '') AS C, CASE WHEN a.SETTING_KEY IS NULL THEN 'REMOVED' ELSE 'CHANGED' END AS ST FROM SETTING_SNAPSHOT s LEFT JOIN APP_SETTING a ON a.SETTING_KEY = s.SETTING_KEY WHERE s.SNAPSHOT_NAME = '%%(pName)' AND (a.SETTING_KEY IS NULL OR COALESCE(a.SETTING_VALUE, '') <> COALESCE(s.SETTING_VALUE, ''))"
    vB = "SELECT a.SETTING_KEY, '', COALESCE(a.SETTING_VALUE, ''), 'ADDED' FROM APP_SETTING a WHERE NOT EXISTS (SELECT 1 FROM SETTING_SNAPSHOT s WHERE s.SNAPSHOT_NAME = '%%(pName)' AND s.SETTING_KEY = a.SETTING_KEY)"
    pSql = "SELECT K, S, C, ST FROM (%%(vA) UNION ALL %%(vB)) ORDER BY K"
    return 0
end
Enter fullscreen mode Exit fullscreen mode

The form entry

entry DO_RESTORE
variables
    numeric vCount
    string vData, vError
endvariables
    if (S_NAME.SNP_DMY = "")
        INFO.SNP_DMY = "Please enter a name."
        return -1
    endif
    call DO_COMPARE
    if ($status < 0)
        return -1
    endif
    activate "OPS_SVC".COMPARE_SNAPSHOT(S_NAME.SNP_DMY, vData, vCount, vError)
    if ($status < 0)
        INFO.SNP_DMY = vError
        return -1
    endif
    if (vCount = 0)
        INFO.SNP_DMY = "No differences; nothing to restore."
        return 0
    endif
    askmess/question "Restore the snapshot %%(S_NAME.SNP_DMY)? %%(vCount) setting(s) differ and will be replaced.~Restore snapshot", "Yes,No"
    if ($status != 1)
        INFO.SNP_DMY = "Restore cancelled; the differences are shown."
        return 0
    endif
    activate "OPS_SVC".RESTORE_SNAPSHOT(S_NAME.SNP_DMY, vCount, vError)
    if ($status < 0)
        rollback
        INFO.SNP_DMY = vError
        return -1
    endif
    commit
    call DO_COMPARE
    INFO.SNP_DMY = "Snapshot restored (%%(vCount) setting(s))."
    return 0
end
Enter fullscreen mode Exit fullscreen mode

The order is: show the differences, stop if there are none, ask with the number, restore, show the comparison again. After No the differences stay on the screen.

The service: a way back first

Before the service changes anything it stores the current settings as a snapshot named BEFORE_RESTORE:

    activate "USER_SVC".CURRENT_USER(vUser, vRole)
    call SQL_LIT(vUser, vUserLit)
    call STORE("BEFORE_RESTORE", vUserLit, vSaved, pError)
    if ($status < 0)
        pCount = 0
        return -1
    endif
    sql "DELETE FROM APP_SETTING WHERE SETTING_KEY NOT IN (SELECT SETTING_KEY FROM SETTING_SNAPSHOT WHERE SNAPSHOT_NAME = '%%(vName)')", "CUSTOMERS"
    if ($status < 0)
        pCount = 0
        pError = $concat("The settings could not be restored (error ", $procerror, ").")
        return -1
    endif
    sql "INSERT OR REPLACE INTO APP_SETTING (SETTING_KEY, SETTING_VALUE) SELECT SETTING_KEY, SETTING_VALUE FROM SETTING_SNAPSHOT WHERE SNAPSHOT_NAME = '%%(vName)'", "CUSTOMERS"
Enter fullscreen mode Exit fullscreen mode

Keys that are not in the snapshot are deleted. The app reads settings with SETTINGS_SVC.GET_SETTING, which takes a default value, so a deleted key means "back to the default" - the state at the time of the snapshot, when the key did not exist yet.

The automatic snapshot cannot be restored directly, because the next restore would overwrite it:

    if (vName = "BEFORE_RESTORE")
        pError = "The automatic snapshot BEFORE_RESTORE cannot be restored directly. Please save it under another name first."
        return -1
    endif
Enter fullscreen mode Exit fullscreen mode

Click test

On a test copy of the database:

  1. Save the snapshot t1. The list shows T1: 7 setting(s) - the service stores names in upper case.
  2. In the normal settings form change "Backups to keep" from 10 to 11 and save.
  3. "Compare with current settings": 6 differences. One was the changed value, five were ADDED: the settings form saves all its fields, also those that were never stored before (for example the session timeout). This was not planned for the test, but it showed the deletion path.
  4. "Restore snapshot...", No: "Restore cancelled; the differences are shown."
  5. "Restore snapshot...", Yes: "Snapshot restored (6 setting(s))." The comparison then says "No differences."
  6. The snapshot list: BEFORE_RESTORE: 12 setting(s) and T1: 7 setting(s).

A first attempt to change the setting from outside the app, with a Python script on the database file while the app was running, failed with disk I/O error. The test then used the app itself. The database was restored from a backup afterwards.

Notes

  • The question names the number of settings, not the keys. The keys are in the result field above the dialog. In an earlier stage of this project askmess did not always repaint a field before the dialog appeared, so the form also keeps the comparison on the screen after No.
  • "Restore" here is a full replace, not a merge. The question does not say that yet: settings that are not in the snapshot go back to their defaults. A user who only reads the dialog does not learn that.

Top comments (0)