Verified Commit 84566b92 authored by WorldTeacher's avatar WorldTeacher
Browse files

fix(migration): store migration in database

parent a62b8d8b
Loading
Loading
Loading
Loading
+34 −25
Original line number Diff line number Diff line
@@ -558,7 +558,10 @@ function BookloreSync:init()
        -- saveSetting / flush calls go through SQLite without any call-site changes.
        self.settings = DbSettings:new(self.db)
        -- Check if we need to migrate from old LuaSettings format
        local migration_check_flag = "old_lua_db_migration_checked"
        local migration_checked = self.settings:readSetting(migration_check_flag)
        if not migration_checked then
            local migration_check_completed = true
            local old_db_path = DataStorage:getSettingsDir() .. "/booklore_db.lua"
            local old_db_file = io.open(old_db_path, "r")
@@ -571,31 +574,37 @@ function BookloreSync:init()
                if stats.total == 0 then
                    logger.info("BookloreSync: Database is empty, migrating from LuaSettings")
                    local migration_success = false
                    local ok, err = pcall(function()
                        local local_db = LuaSettings:open(old_db_path)
                    local success = self.db:migrateFromLuaSettings(local_db)
                        migration_success = self.db:migrateFromLuaSettings(local_db)
                    end)
                    if success then
                    if not ok then
                        migration_check_completed = false
                        logger.err("BookloreSync: Migration failed:", err)
                        UIManager:show(InfoMessage:new{
                            text = _("Failed to migrate old data. Check logs."),
                            timeout = 3,
                        })
                    elseif migration_success then
                        UIManager:show(InfoMessage:new{
                            text = _("Migrated data to new database format"),
                            timeout = 2,
                        })
                    else
                        migration_check_completed = false
                        UIManager:show(InfoMessage:new{
                            text = _("Migration completed with some errors. Check logs."),
                            timeout = 3,
                        })
                    end
                end)
                if not ok then
                    logger.err("BookloreSync: Migration failed:", err)
                    UIManager:show(InfoMessage:new{
                        text = _("Failed to migrate old data. Check logs."),
                        timeout = 3,
                    })
                end
            end
            if migration_check_completed then
                self.settings:saveSetting(migration_check_flag, true)
            end
        end
    end