[evolution-data-server] [ECalCache] Alter timezones table only if the 'refs' column doesn't exist



commit c4ba0ee5c391d34ceeb28fb5f7f774c5ae0ff0b2
Author: Milan Crha <mcrha redhat com>
Date:   Mon Jul 16 09:01:11 2018 +0200

    [ECalCache] Alter timezones table only if the 'refs' column doesn't exist
    
    In case older version is run after the table had been altered can
    cause another try to add the 'refs' column, which fails due to
    duplicity and avoids opening the calendar. This change doesn't
    alter table when the column is already there.
    
    Related to https://bugzilla.gnome.org/show_bug.cgi?id=794108

 src/calendar/libedata-cal/e-cal-cache.c | 39 ++++++++++++++++++++++++++++++---
 1 file changed, 36 insertions(+), 3 deletions(-)
---
diff --git a/src/calendar/libedata-cal/e-cal-cache.c b/src/calendar/libedata-cal/e-cal-cache.c
index 72d8c1859..a91fec1d5 100644
--- a/src/calendar/libedata-cal/e-cal-cache.c
+++ b/src/calendar/libedata-cal/e-cal-cache.c
@@ -2119,6 +2119,31 @@ cal_cache_count_tmd_refs (ECalCache *cal_cache,
        return TRUE;
 }
 
+static gboolean
+e_cal_cache_table_refs_column_exists_cb (ECache *cache,
+                                        gint ncols,
+                                        const gchar *column_names[],
+                                        const gchar *column_values[],
+                                        gpointer user_data)
+{
+       gboolean *prefs_column_exists = user_data;
+       gint ii;
+
+       g_return_val_if_fail (prefs_column_exists != NULL, FALSE);
+       g_return_val_if_fail (column_names != NULL, FALSE);
+       g_return_val_if_fail (column_values != NULL, FALSE);
+
+       for (ii = 0; ii < ncols && !*prefs_column_exists; ii++) {
+               if (column_names[ii] && camel_strcase_equal (column_names[ii], "name")) {
+                       if (column_values[ii])
+                               *prefs_column_exists = camel_strcase_equal (column_values[ii], "refs");
+                       break;
+               }
+       }
+
+       return TRUE;
+}
+
 static gboolean
 e_cal_cache_migrate (ECache *cache,
                     gint from_version,
@@ -2132,13 +2157,21 @@ e_cal_cache_migrate (ECache *cache,
        /* Add any version-related changes here (E_CAL_CACHE_VERSION) */
 
        if (from_version > 0 && from_version < 3) {
+               gboolean refs_column_exists = FALSE;
                gchar *stmt;
 
                g_rec_mutex_lock (&cal_cache->priv->timezones_lock);
 
-               stmt = e_cache_sqlite_stmt_printf ("ALTER TABLE %Q ADD COLUMN refs INTEGER", 
ECC_TABLE_TIMEZONES);
-               success = e_cache_sqlite_exec (E_CACHE (cal_cache), stmt, cancellable, error);
-               e_cache_sqlite_stmt_free (stmt);
+               /* In case an older version modified the local cache version,
+                  then the ALTER TABLE command can fail due to duplicate 'refs' column */
+               success = e_cache_sqlite_select (E_CACHE (cal_cache), "PRAGMA table_info (" 
ECC_TABLE_TIMEZONES ")",
+                       e_cal_cache_table_refs_column_exists_cb, &refs_column_exists, cancellable, NULL);
+
+               if (!success || !refs_column_exists) {
+                       stmt = e_cache_sqlite_stmt_printf ("ALTER TABLE %Q ADD COLUMN refs INTEGER", 
ECC_TABLE_TIMEZONES);
+                       success = e_cache_sqlite_exec (E_CACHE (cal_cache), stmt, cancellable, error);
+                       e_cache_sqlite_stmt_free (stmt);
+               }
 
                if (success) {
                        timezones = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, 
timezone_migration_data_free);


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