~aleteoryx/muditaos

fc076fa4ccf5d1090ed649d35dabdff20e50aa79 — Lefucjusz 2 years ago 0aa333d
[MOS-855] Fix restore from legacy backup

Added checking if requested to restore backup
from pre-UDM version and setting db versions
to zero in such case, as version info file
(called backup.json in legacy backups) doesn't
contain info about versions.
3 files changed, 43 insertions(+), 11 deletions(-)

M module-db/tests/ContactsRecord_tests.cpp
M scripts/lua/restore.lua
M scripts/lua/share/consts.lua
M module-db/tests/ContactsRecord_tests.cpp => module-db/tests/ContactsRecord_tests.cpp +9 -9
@@ 369,7 369,7 @@ TEST_CASE("Contact record numbers update")
        newRecord.numbers = std::vector<ContactRecord::Number>({ContactRecord::Number(numbersPL[0], std::string("")),
                                                                ContactRecord::Number(numbersFR[1], std::string(""))});
        REQUIRE(records.Update(newRecord));
        REQUIRE(contactDB.number.count() == 4);
        REQUIRE(contactsDb.get().number.count() == 4);

        auto validationRecord = records.GetByID(1);
        REQUIRE(validationRecord.numbers.size() == 2);


@@ 395,13 395,13 @@ TEST_CASE("Contact record numbers update")
        newRecord.numbers = std::vector<ContactRecord::Number>({ContactRecord::Number(numbersPL[0], std::string("")),
                                                                ContactRecord::Number(numbersFR[1], std::string(""))});
        REQUIRE(records.Update(newRecord));
        REQUIRE(contactDB.number.count() == 4);
        REQUIRE(contactsDb.get().number.count() == 4);

        // deleting country code
        newRecord.numbers = std::vector<ContactRecord::Number>(
            {ContactRecord::Number(numbers[0], std::string("")), ContactRecord::Number(numbers[1], std::string(""))});
        REQUIRE(records.Update(newRecord));
        REQUIRE(contactDB.number.count() == 4);
        REQUIRE(contactsDb.get().number.count() == 4);

        auto validationRecord = records.GetByID(1);
        REQUIRE(validationRecord.numbers.size() == 2);


@@ 427,17 427,17 @@ TEST_CASE("Contact record numbers update")
        newRecord.numbers = std::vector<ContactRecord::Number>({ContactRecord::Number(numbersPL[0], std::string("")),
                                                                ContactRecord::Number(numbersPL[1], std::string(""))});
        REQUIRE(records.Update(newRecord));
        REQUIRE(contactDB.number.count() == 4);
        REQUIRE(contactsDb.get().number.count() == 4);

        // changing country code (to FR)
        newRecord.numbers = std::vector<ContactRecord::Number>({ContactRecord::Number(numbersFR[0], std::string("")),
                                                                ContactRecord::Number(numbersFR[1], std::string(""))});
        REQUIRE(records.Update(newRecord));
        REQUIRE(contactDB.number.count() == 6);
        REQUIRE(contactDB.number.getById(1).contactID != 1); // old numbers do not belong to any contact
        REQUIRE(contactDB.number.getById(1).contactID != 2);
        REQUIRE(contactDB.number.getById(2).contactID != 1);
        REQUIRE(contactDB.number.getById(2).contactID != 2);
        REQUIRE(contactsDb.get().number.count() == 6);
        REQUIRE(contactsDb.get().number.getById(1).contactID != 1); // old numbers do not belong to any contact
        REQUIRE(contactsDb.get().number.getById(1).contactID != 2);
        REQUIRE(contactsDb.get().number.getById(2).contactID != 1);
        REQUIRE(contactsDb.get().number.getById(2).contactID != 2);

        auto validationRecord = records.GetByID(1);
        REQUIRE(validationRecord.numbers.size() == 2);

M scripts/lua/restore.lua => scripts/lua/restore.lua +33 -2
@@ 10,6 10,7 @@ local restore = {}

local unpacked_backup_dir = paths.temp_dir .. "/backup"
local version_file = unpacked_backup_dir .. "/" .. consts.version_file
local legacy_version_file = unpacked_backup_dir .. "/" .. consts.legacy_version_file

restore.script_name = "restore.lua"
restore.img_in_progress = "assets/gui_image_restore_in_progress.bin"


@@ 46,9 47,34 @@ local function build_db_set(file)
    return set
end

local function get_legacy_db_set()
    local set = {
        ["calllog"] = 0,
        ["sms"] = 0,
        ["events"] = 0,
        ["settings_v2"] = 0,
        ["notes"] = 0,
        ["custom_quotes"] = 0,
        ["predefined_quotes"] = 0,
        ["contacts"] = 0,
        ["alarms"] = 0,
        ["notifications"] = 0,
        ["multimedia"] = 0
    }
    return set
end

local function perform_db_migration()
    print("Performing database migration")
    local dbset = build_db_set(version_file)
    local dbset = {}
    if helpers.exists(version_file) then
        dbset = build_db_set(version_file)
    else
        assert(helpers.exists(legacy_version_file))
        print("Legacy backup file, assuming legacy databases set")
        dbset = get_legacy_db_set()
    end

    local result = migration.migrate(unpacked_backup_dir, paths.migration_scripts_dir, dbset)
    assert(result == migration.retcode.OK, string.format("Database migration process failed with %d", result))
end


@@ 59,7 85,12 @@ local function sync_databases()

    helpers.rm_files_from_dir(paths.db_dir)
    helpers.copy_dir(unpacked_backup_dir, paths.db_dir)
    assert(os.remove(paths.db_dir .. "/" .. consts.version_file))

    local version_file_path = paths.db_dir .. "/" .. consts.version_file
    if not helpers.exists(version_file_path) then
        version_file_path = paths.db_dir .. "/" .. consts.legacy_version_file
    end
    assert(os.remove(version_file_path))
end

local function remove_cache()

M scripts/lua/share/consts.lua => scripts/lua/share/consts.lua +1 -0
@@ 1,6 1,7 @@
local consts = {}

consts.version_file = "version.json"
consts.legacy_version_file = "backup.json" -- Pre-UDM backup package had version.json file named as backup.json
consts.indexer_cache_file = ".directory_is_indexed"

return consts