Merge authors: Michal Hruby (mhr3) Related merge proposals: https://code.launchpad.net/~mhr3/zeitgeist/bb-schema-ver-table/+merge/79928 proposed by: Michal Hruby (mhr3) review: Approve - Siegfried Gevatter (rainct) review: Approve - Seif Lotfy (seif) ------------------------------------------------------------ revno: 320 [merge] committer: Michal Hruby <michal....@gmail.com> branch nick: bb-fts timestamp: Thu 2011-10-20 15:56:50 +0200 message: Merge lp:~mhr3/zeitgeist/bb-schema-ver-table/ modified: src/errors.vala src/sql-schema.vala src/utils.vala
-- lp:~zeitgeist/zeitgeist/bluebird https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird Your team Zeitgeist Framework Team is subscribed to branch lp:~zeitgeist/zeitgeist/bluebird. To unsubscribe from this branch go to https://code.launchpad.net/~zeitgeist/zeitgeist/bluebird/+edit-subscription
=== modified file 'src/errors.vala' --- src/errors.vala 2011-09-05 19:22:17 +0000 +++ src/errors.vala 2011-10-20 13:32:51 +0000 @@ -26,6 +26,7 @@ DATABASE_ERROR, INVALID_ARGUMENT, INVALID_KEY, + BACKUP_FAILED, } // vala doesn't include proper headers, this fixes it === modified file 'src/sql-schema.vala' --- src/sql-schema.vala 2011-10-14 18:26:09 +0000 +++ src/sql-schema.vala 2011-10-20 13:55:35 +0000 @@ -2,6 +2,8 @@ * * Copyright © 2011 Collabora Ltd. * By Siegfried-Angel Gevatter Pujals <siegfr...@gevatter.com> + * Copyright © 2011 Canonical Ltd. + * By Michal Hruby <michal.hr...@canonical.com> * * Based upon a Python implementation (2009-2011) by: * Markus Korn <thek...@gmx.net> @@ -32,14 +34,61 @@ 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" + try + { + Utils.backup_database (); + } + catch (Error backup_error) + { + var msg = "Database backup failed: " + backup_error.message; + throw new EngineError.BACKUP_FAILED (msg); + } + 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)); + } + } + + private 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[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 +395,21 @@ 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 + ) + """); + + /* The 'ON CONFLICT REPLACE' on the PK converts INSERT to UPDATE + * when appriopriate */ + 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 13:32:51 +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,21 @@ return LOCAL_EXTENSIONS_PATH; } + + public bool using_in_memory_database () + { + return get_database_file_path () == ":memory:"; + } + + public void backup_database () throws Error + { + File original; + File destination; + original = File.new_for_path (get_database_file_path ()); + destination = File.new_for_path (get_database_file_backup_path ()); + + original.copy (destination, FileCopyFlags.OVERWRITE, null, null); + } } }
_______________________________________________ Mailing list: https://launchpad.net/~zeitgeist Post to : zeitgeist@lists.launchpad.net Unsubscribe : https://launchpad.net/~zeitgeist More help : https://help.launchpad.net/ListHelp