Quantcast

Major refactor: V0.1.1

Brandon Talbot [07-02-16 - 06:11]
Major refactor: V0.1.1
* Added settings migration tool
* Fixes crash when loading with outdated settings
Filename
DJBags.toc
src/lua/settings/settings.lua
diff --git a/DJBags.toc b/DJBags.toc
index 917e6dc..7fecc7e 100644
--- a/DJBags.toc
+++ b/DJBags.toc
@@ -2,7 +2,7 @@
 ## Title: DJBags
 ## Author: DarkJaguar91
 ## Notes: BagAddon - BETA
-## Version: 0.1.0
+## Version: 0.1.1
 ## SavedVariables: DJBagsConfig

 src/manifest.xml
\ No newline at end of file
diff --git a/src/lua/settings/settings.lua b/src/lua/settings/settings.lua
index a21e3e7..ed66a7c 100644
--- a/src/lua/settings/settings.lua
+++ b/src/lua/settings/settings.lua
@@ -1,5 +1,7 @@
 local NAME, ADDON = ...

+local DBVERSION = 1
+
 ADDON.settingsController = {}
 local settings = ADDON.settingsController
 settings.__index = settings
@@ -37,7 +39,24 @@ function settings:GetCharacterSettings()
     local realm = GetRealmName()
     local player = UnitName("player")

+    ADDON.settings.version = DBVERSION
     if DJBagsConfig and DJBagsConfig[realm] and DJBagsConfig[realm][player] then
+        if not DJBagsConfig[realm][player].version or DJBagsConfig[realm][player].version < DBVERSION then
+            DJBagsConfig[realm][player] = self:migrateDb(DJBagsConfig[realm][player].version or 0, DJBagsConfig[realm][player])
+        end
         ADDON.settings = DJBagsConfig[realm][player]
     end
+end
+
+settings.migrateFunc = {}
+settings.migrateFunc[1] = function ()
+    return ADDON.settings
+end
+
+function settings:migrateDb(version, old)
+    while version < DBVERSION do
+        version = version + 1
+        old = self.migrateFunc[version](old)
+    end
+    return old
 end
\ No newline at end of file