← Back to team overview

zeitgeist team mailing list archive

[Merge] lp:~mhr3/zeitgeist/bb-schema-ver-table into lp:~zeitgeist/zeitgeist/bluebird

 

Michal Hruby has proposed merging lp:~mhr3/zeitgeist/bb-schema-ver-table into lp:~zeitgeist/zeitgeist/bluebird.

Requested reviews:
  Zeitgeist Framework Team (zeitgeist)

For more details, see:
https://code.launchpad.net/~mhr3/zeitgeist/bb-schema-ver-table/+merge/79928


-- 
https://code.launchpad.net/~mhr3/zeitgeist/bb-schema-ver-table/+merge/79928
Your team Zeitgeist Framework Team is requested to review the proposed merge of lp:~mhr3/zeitgeist/bb-schema-ver-table into lp:~zeitgeist/zeitgeist/bluebird.
=== modified file 'src/sql-schema.vala'
--- src/sql-schema.vala	2011-10-14 18:26:09 +0000
+++ src/sql-schema.vala	2011-10-20 09:04:37 +0000
@@ -2,6 +2,8 @@
  *
  * Copyright © 2011 Collabora Ltd.
  *             By Siegfried-Angel Gevatter Pujals <siegfried@xxxxxxxxxxxx>
+ *           © 2011 Canonical Ltd.
+ *             By Michal Hruby <michal.hruby@xxxxxxxxxxxxx>
  *
  * Based upon a Python implementation (2009-2011) by:
  *  Markus Korn <thekorn@xxxxxxx>
@@ -32,14 +34,51 @@
     public class DatabaseSchema : Object
     {
 
+        public const string CORE_SCHEMA = "core";
+        public const int CORE_SCHEMA_VERSION = 5;
+
         public static void ensure_schema (Sqlite.Database database)
             throws EngineError
         {
-            //if (Constants.DATABASE_FILE_PATH != ":memory:" && !new_db)
-            // assume temporary memory backed DBs are good
-            // check_core_schema_upgrade
-
-            create_schema (database);
+            int schema_version = Utils.using_in_memory_database () ?
+                -1 : get_schema_version (database);
+
+            if (schema_version == -1)
+            {
+                // most likely a new DB
+                create_schema (database);
+            }
+            else if (schema_version == 4)
+            {
+                // DB from latest python Zeitgeist, which we can "upgrade"
+                Utils.backup_database ();
+                create_schema (database);
+            }
+            else if (schema_version < CORE_SCHEMA_VERSION)
+            {
+                throw new EngineError.DATABASE_ERROR ("Unable to upgrade from schema version %d".printf (schema_version));
+            }
+        }
+
+        public static int get_schema_version (Sqlite.Database database)
+        {
+          var sql = "SELECT version FROM schema_version WHERE schema='core'";
+          int schema_version = -1;
+          database.exec (sql,
+              (n_cols, values, column_names) =>
+              {
+                  if (values != null && values[0] != null)
+                  {
+                      schema_version = int.parse (values[0]);
+                  }
+                  return 0;
+              }, null);
+
+          // we don't really care about the return value of exec, the result
+          // will be -1 if something went wrong anyway
+          debug ("schema_version is %d", schema_version);
+
+          return schema_version;
         }
 
         public static void create_schema (Sqlite.Database database)
@@ -346,6 +385,18 @@
                             AS actor_uri
                     FROM event
                 """);
+
+            // Set schema version
+            exec_query (database, """
+                CREATE TABLE IF NOT EXISTS schema_version (
+                    schema VARCHAR PRIMARY KEY ON CONFLICT REPLACE,
+                    version INT
+                )
+                """);
+            var schema_sql = "INSERT INTO schema_version VALUES ('%s', %d)"
+                .printf (CORE_SCHEMA, CORE_SCHEMA_VERSION);
+            exec_query (database, schema_sql);
+
         }
 
         /**

=== modified file 'src/utils.vala'
--- src/utils.vala	2011-09-16 09:15:06 +0000
+++ src/utils.vala	2011-10-20 09:04:37 +0000
@@ -38,10 +38,6 @@
         public const string DBUS_INTERFACE = "";
         public const string SIG_EVENT = "asaasay";
 
-        // Required version of DB schema
-        public const string CORE_SCHEMA = "core";
-        public const int CORE_SCHEMA_VERSION = 4;
-
         // configure runtime cache for events
         // default size is 2000
         public const uint CACHE_SIZE = 0;
@@ -102,6 +98,29 @@
 
             return LOCAL_EXTENSIONS_PATH;
         }
+
+        public bool using_in_memory_database ()
+        {
+            return get_database_file_path () == ":memory:";
+        }
+
+        public void backup_database ()
+        {
+            File original;
+            File destination;
+            original = File.new_for_path (get_database_file_path ());
+            destination = File.new_for_path (get_database_file_backup_path ());
+
+            try
+            {
+                original.copy (destination, FileCopyFlags.OVERWRITE, null,
+                    null);
+            }
+            catch (Error err)
+            {
+                warning ("Unable to backup database: %s", err.message);
+            }
+        }
     }
 }
 


Follow ups