def slot_restore(self, arg): """Restore a dumped database. It can be either an existing one, or one which has been deleted. """ # Get source file: dbpath = self.getBDbPath() if not dbpath: return None restore = Restore(dbpath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (dbpath, )) return state = 0 try: if dbname in self.dbList: if not confirmationDialog( _("Replace Database?"), argSub( _("Are you sure you want to replace database '%1'?" ), (dbname, )), False): restore.close() return self.deletedb(dbname) self.db.send(u"""CREATE DATABASE %s OWNER %s ENCODING 'UTF8'""" % (dbname, ADMIN)) state = 1 # Add to 'databases' table self.db.send(u"INSERT INTO databases VALUES (?, ?, ?, ?)", (self.db.getTime(), dbname, u'', u'')) state = 2 newmaster = self.connect(dbname) state = 3 guimessage = argSub( _("New database '%1' created, now read in the data"), (dbname, )) restore.setMaster(newmaster) guiReport(_("Restore Database"), restore, guimessage) #message(_("New database now set up")) self.usersPrivileges(newmaster) # Ensure connection is closed restore = None newmaster.close() newmaster = None except: print_exc() message(_("Couldn't create new database (%1)"), (dbname, )) if (state >= 3): newmaster.close() if (state >= 2): self.db.send(u"DELETE FROM databases WHERE name = ?", (dbname, )) if (state >= 1): self.db.send(u"DROP DATABASE %s" % dbname) # adjust display, select new db self.initDBlist()
def updateDbConfig(self, source): """Update the current database using the configuration file supplied as a CfgZip object in source. The selected config file must match the name of the current database. Before updating from this file, dump the current database state to a folder 'dumps' in the same folder as the config file. That is in case something goes wrong and the old state must be recovered. """ if (self.dbname != source.cfgName): message(_("Database name does not match data folder")) return # Backup existing database state. sPath = self.configEd.getSourcePath() budir = os.path.join(os.path.dirname(sPath), 'dumps') if not os.path.isdir(budir): os.mkdir(budir) backup = Dump(self.master, budir) filepath = backup.filepath if not filepath: return guimessage = argSub( _("New backup file '%1' created, now read in the data"), (filepath, )) guiReport(_("Create Backup File"), backup, guimessage) backup = None if not filepath: return try: guimessage = argSub(_("Updating database '%1' from %2"), (self.dbname, sPath)) mm = MakeMaster(source, self.master) guiReport(_("Updating Master Database"), mm, guimessage) mm = None except: print_exc() message(_("Update failed, trying to restore from '%1'"), (filepath, )) restore = Restore(filepath) dbname = restore.getDbName() if not dbname: message(_("Couldn't open database file '%1'"), (filepath, )) return # Delete all tables for t in self.master.getTables(): self.master.send(u"DROP TABLE %s" % t) # Restore old state guimessage = argSub( _("Database '%1' cleared, now restore the data"), (dbname, )) restore.setMaster(self.master) guiReport(_("Restore Database"), restore, guimessage) self.usersPrivileges(self.master) # adjust display, select new db self.initDBlist()