f-spot r3600 - in trunk: . src src/Core tools



Author: sdelcroix
Date: Fri Jan 25 16:15:34 2008
New Revision: 3600
URL: http://svn.gnome.org/viewvc/f-spot?rev=3600&view=rev

Log:
2008-01-25  Stephane Delcroix  <sdelcroix novell com 

	slightly modified version of a patch from Mirco Bauer to silently
	upgrade dbs form sqlite2 to sqlite3 whenever possible.

	* src/QueuedSqliteDatabase.cs: expose GetFileVersion.

	* src/Core/Defines.cs.in: expose BINDIR

	* src/main.cs: die on DbException.

	* src/Db.cs: invoke f-spot-sqlite-upgrade on demand

	* tools/f-spot-sqlite-upgrade: better checks and error reporting.


Modified:
   trunk/ChangeLog
   trunk/src/Core/Defines.cs.in
   trunk/src/Db.cs
   trunk/src/QueuedSqliteDatabase.cs
   trunk/src/main.cs
   trunk/tools/f-spot-sqlite-upgrade

Modified: trunk/src/Core/Defines.cs.in
==============================================================================
--- trunk/src/Core/Defines.cs.in	(original)
+++ trunk/src/Core/Defines.cs.in	Fri Jan 25 16:15:34 2008
@@ -16,5 +16,6 @@
 		public static string GNOME_ICON_THEME_PREFIX = "@GNOME_ICON_THEME_PREFIX@";
 		public static string PREFIX = "@prefix@";
 		public static string APP_DATA_DIR = "@prefix@/share/@PACKAGE@";
+		public static string BINDIR = PREFIX + "/bin";
 	}			
 }

Modified: trunk/src/Db.cs
==============================================================================
--- trunk/src/Db.cs	(original)
+++ trunk/src/Db.cs	Fri Jan 25 16:15:34 2008
@@ -3,7 +3,8 @@
 using System.IO;
 using System;
 using Banshee.Database;
-
+using System.Diagnostics;
+using FSpot;
 
 public class DbItemEventArgs {
 	private DbItem [] items;
@@ -29,6 +30,12 @@
 public delegate void ItemsRemovedHandler (object sender, DbItemEventArgs args);
 public delegate void ItemsChangedHandler (object sender, DbItemEventArgs args);
 
+public class DbException : ApplicationException {
+	public DbException(string msg) : base(msg)
+	{
+	}
+}
+
 public abstract class DbStore {
 	// DbItem cache.
 
@@ -221,7 +228,9 @@
 		if (new_db && ! create_if_missing)
 			throw new Exception (path + ": File not found");
 
-		database = new QueuedSqliteDatabase(path);	
+		database = new QueuedSqliteDatabase(path);
+		if (database.GetFileVersion(path) == 2)
+			SqliteUpgrade ();
 
 		// Load or create the meta table
  		meta_store = new MetaStore (Database, new_db);
@@ -274,6 +283,41 @@
 		Database.RollbackTransaction ();
 	}
 
+	private void SqliteUpgrade ()
+	{
+		//Close the db
+		database.Dispose();
+
+		string upgrader_path = null;
+		string [] possible_paths = {
+			Path.Combine (Defines.BINDIR, "f-spot-sqlite-upgrade"),
+			"../tools/f-spot-sqlite-upgrade",
+			"/usr/local/bin/f-spot-sqlite-upgrade",
+			"/usr/bin/f-spot-sqlite-upgrade",
+		};
+
+		foreach (string p in possible_paths)
+			if (File.Exists (p)) {
+				upgrader_path = p;
+				break;
+			}
+
+		if (upgrader_path == null)
+			throw new DbException ("Unable to find the f-spot-sqlite-upgrade script on your system");
+
+		Console.WriteLine ("Running {0}...", upgrader_path);
+		ProcessStartInfo updaterInfo = new ProcessStartInfo (upgrader_path);
+		updaterInfo.UseShellExecute = false;
+		updaterInfo.RedirectStandardError = true;
+		Process updater = Process.Start (updaterInfo);
+		string stdError = updater.StandardError.ReadToEnd ();
+		updater.WaitForExit ();
+		if (updater.ExitCode != 0)
+			throw new DbException(stdError);
+
+		//Re-open the db
+		database = new QueuedSqliteDatabase(path);
+	}
 }
 
 

Modified: trunk/src/QueuedSqliteDatabase.cs
==============================================================================
--- trunk/src/QueuedSqliteDatabase.cs	(original)
+++ trunk/src/QueuedSqliteDatabase.cs	Fri Jan 25 16:15:34 2008
@@ -266,7 +266,7 @@
             connection.Close();
         }
 
-        private int GetFileVersion(string path) 
+        public int GetFileVersion(string path) 
         {
             if (!File.Exists(path)) {
                 return 3;

Modified: trunk/src/main.cs
==============================================================================
--- trunk/src/main.cs	(original)
+++ trunk/src/main.cs	Fri Jan 25 16:15:34 2008
@@ -192,6 +192,11 @@
 
 					if (core != null)
 						core.UnregisterServer ();
+
+					// if there is a problem with the DB, so is no way we can survive
+					if (e is DbException) {
+						throw;
+					}
 				}
 				if (control == null) {
 					System.Console.WriteLine ("Can't get a connection to the dbus. Trying again...");

Modified: trunk/tools/f-spot-sqlite-upgrade
==============================================================================
--- trunk/tools/f-spot-sqlite-upgrade	(original)
+++ trunk/tools/f-spot-sqlite-upgrade	Fri Jan 25 16:15:34 2008
@@ -1,21 +1,43 @@
 #!/bin/sh
+set -e
+
 # This only upgrades the default database location
 # if you have specified a different location 
 DB_LOCATION=~/.gnome2/f-spot/photos.db
 BACKUP_LOCATION=$DB_LOCATION.backup
+DUMP_LOCATION=$DB_LOCATION.dump
+NEW_DB_LOCATION=$DB_LOCATION.new
 
 if ! which sqlite >& /dev/null ; then
-	echo "Could not find sqlite binary. Update aborted."
+	echo "Could not find sqlite binary. Update aborted." >&2
 	exit 1
 elif ! which sqlite3 >& /dev/null ; then
-	echo "Could not find sqlite3 binary. Update aborted."
+	echo "Could not find sqlite3 binary. Update aborted." >&2
 	exit 1
 fi
 
+if [ ! -f $DB_LOCATION ]; then
+	echo "Could not find $DB_LOCATION, nothing to update. Update aborted." >&2
+	exit 1
+fi
+
+# make sure nothing gets in the way
+rm -f $BACKUP_LOCATION
+rm -f $DUMP_LOCATION
+rm -f $NEW_DB_LOCATION
+
 if grep "^...This file contains an SQLite 2.1 database" $DB_LOCATION &> /dev/null; then
 	echo "Upgrading from SQLite 2.1 to SQLite3"
-	mv $DB_LOCATION $BACKUP_LOCATION
-	sqlite $BACKUP_LOCATION .dump | sqlite3 $DB_LOCATION
+	cp $DB_LOCATION $BACKUP_LOCATION
+	if sqlite $DB_LOCATION .dump > $DUMP_LOCATION &&
+	   sqlite3 $NEW_DB_LOCATION < $DUMP_LOCATION; then
+		cp $NEW_DB_LOCATION $DB_LOCATION
+		echo "Upgrade was successful."
+	else
+		echo "Upgrade failed, putting old database back." >&2
+		cp $BACKUP_LOCATION $DB_LOCATION
+		exit 1
+	fi
 else
 	echo "Database is already upgraded"
 fi



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]