Migrating configuration and data files for sandboxed apps

If you, like me, have been using QML LocalStorage instead of Settings, @vige’s nice example isn’t applicable right off the bat, so here is my poor attempt at doing the same, but for LocalStorage:

void migrateLocalStorage()
{
    // The new location of the LocalStorage database
    QDir newDbDir(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/my.domain/MyApp/QML/OfflineStorage/Databases/");

    if(newDbDir.exists())
        return;

    newDbDir.mkpath(newDbDir.path());

    QString dbname = QString(QCryptographicHash::hash(("MyAppDB"), QCryptographicHash::Md5).toHex());

    // The old LocalStorage database
    QFile oldDb(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/harbour-myapp/harbour-MyApp/QML/OfflineStorage/Databases/" + dbname + ".sqlite");
    QFile oldIni(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/harbour-myapp/harbour-MyApp/QML/OfflineStorage/Databases/" + dbname + ".ini");

    oldDb.copy(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/my.domain/MyApp/QML/OfflineStorage/Databases/" + dbname + ".sqlite");
    oldIni.copy(QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + "/my.domain/MyApp/QML/OfflineStorage/Databases/" + dbname + ".ini");
}

Edit: simplified with QFile.copy()

3 Likes