Merge lp:~danielalmaguer/wxbanker/Db_Fallback into lp:wxbanker

Proposed by Michael Rooney
Status: Needs review
Proposed branch: lp:~danielalmaguer/wxbanker/Db_Fallback
Merge into: lp:wxbanker
Diff against target: 76 lines (+43/-2)
1 file modified
wxbanker/persistentstore.py (+43/-2)
To merge this branch: bzr merge lp:~danielalmaguer/wxbanker/Db_Fallback
Reviewer Review Type Date Requested Status
wxBanker Developers Pending
Review via email: mp+142181@code.launchpad.net

Description of the change

Thanks! I'll just propose a merge so we have a way to discuss the changes. Overall it looks great, just two suggestions. 1) for the GetLatestBackup function, it would be cleaner to simply sort the results of os.listdir and use the first/last result, using a custom "key" lambda a string sort doesn't do the trick.
2) when it calls DatabaseUpgradeError, it would be helpful to explain that the latest backup has been replaced and to request filing a bug with a link. I guess this dialog will pop up on every run?

Thanks again for your time and contribution. Did you run into a database upgrade error that inspired this improvement? If so I'd love to know about it!

To post a comment you must log in.
877. By d4nii

Added RecoverDatabaseBackup dialog

878. By d4nii

Optimized GetLatestBackup()
Indentation error bugfix in RecoverDatabaseBackup()

Revision history for this message
d4nii (danielalmaguer) wrote :

Thanks for your feedback. The Dialog informing the user and requesting him to file a bug has been implemented in DatabaseUpgradeError().

About optimizing the GetLatestBackup function, it's now done by using the sorted function as you suggested, but we didn't found an easy and neat way to implement the key function with the lambda feature, so we had to implement the function the normal way.

Any more feedback would be appreciated.

"Did you run into a database upgrade error that inspired this improvement?"
   - Not really, I actually found wxBanker very useful and wanted to contribute, so I just jumped out to the buglist and saw this one.

Unmerged revisions

878. By d4nii

Optimized GetLatestBackup()
Indentation error bugfix in RecoverDatabaseBackup()

877. By d4nii

Added RecoverDatabaseBackup dialog

876. By d4nii

Added Database Error Handling code

875. By d4nii

Added Database Fallback functions.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'wxbanker/persistentstore.py'
2--- wxbanker/persistentstore.py 2012-08-11 03:28:39 +0000
3+++ wxbanker/persistentstore.py 2013-01-10 23:47:22 +0000
4@@ -36,6 +36,7 @@
5 import datetime
6 import os
7 import sys
8+import wx
9
10 from sqlite3 import dbapi2 as sqlite
11 import sqlite3
12@@ -120,6 +121,44 @@
13
14 return self.cachedModel
15
16+ def RecoverDatabaseBackup(self):
17+ import shutil
18+ try:
19+ shutil.copyfile(self.GetLatestBackup(), self.Path)
20+ except IOError:
21+ raise Exception("Unable to restore database.")
22+ else:
23+ print "Restored Succesfully"
24+ msg = """There was an error with the current database version.
25+ A previous working database has been restored.
26+ Please report this error to wxBanker developers in order to improve this app.
27+
28+ Would you like to fill a bug report?"""
29+
30+ dlg = wx.MessageDialog(None, msg, "Database Error", style=wx.YES|wx.NO|wx.ICON_WARNING)
31+ ans = dlg.ShowModal()
32+ print ans
33+ if ans & wx.YES:
34+ import webbrowser
35+ webbrowser.open('http://bugs.launchpad.net/wxbanker/+filebug')
36+ dlg.Destroy()
37+
38+ def GetLatestBackup(self):
39+ def get_version(f):
40+ try:
41+ v = int (f[f.find("v")+1:f.find("-",f.find("v"))])
42+ except:
43+ v = 0
44+ return v
45+ f = sorted(os.listdir(os.path.dirname(self.Path)), key=get_version, reverse=True) [0]
46+ return os.path.join(os.path.dirname(self.Path), f)
47+
48+ def DatabaseUpgradeError(self, errorMsg):
49+ self.RecoverDatabaseBackup()
50+ dlg = wx.MessageDialog(None, errorMsg, _("Database upgrade Error"), style=wx.OK|wx.ICON_WARNING)
51+ dlg.ShowModal()
52+ dlg.Destroy()
53+
54 def CreateAccount(self, accountName, currency=0):
55 if isinstance(currency, currencies.BaseCurrency):
56 currency = currencies.GetCurrencyInt(currency)
57@@ -275,7 +314,7 @@
58 try:
59 cursor.execute('ALTER TABLE transactions ADD recurringParent INTEGER')
60 except sqlite3.OperationalError:
61- debug.debug("Detected a broken database from the 0.4 -> 0.6 upgrade, handling appropriately.")
62+ debug.debug("Detected a broken database from the 0.4 -> 0.6 upgrade, handling appropriately.")
63 elif fromVer == 7:
64 # Force a re-sync for the 0.6.1 release after fixing LP: #496341
65 self.needsSync = True
66@@ -300,7 +339,9 @@
67 # globalCurrency entry
68 cursor.execute('INSERT INTO meta VALUES (null, ?, ?)', ('GlobalCurrency', 0))
69 else:
70- raise Exception("Cannot upgrade database from version %i"%fromVer)
71+ # raise Exception("Cannot upgrade database from version %i"%fromVer)
72+ # Fallback to latest database backup
73+ self.DatabaseUpgradeError("Cannot upgrade database from version %i"%fromVer)
74
75 """
76 # Tagging infrastructure (currently unused due to speed of parsing on startup).

Subscribers

People subscribed via source and target branches

to all changes: