[epiphany/wip/sync: 6/16] profile-migrator: Migrate history to Firefox Sync history



commit d7e4a435e0019f76cd43addc82275d5f362d3b51
Author: Gabriel Ivascu <ivascu gabriel59 gmail com>
Date:   Sat May 27 23:04:51 2017 +0300

    profile-migrator: Migrate history to Firefox Sync history
    
    Add a sync_id column to the urls table and update visit timestamps to
    microseconds in urls and visits tables.

 lib/ephy-profile-utils.h                        |    2 +-
 lib/history/ephy-history-service-hosts-table.c  |    4 +-
 lib/history/ephy-history-service-urls-table.c   |    4 +-
 lib/history/ephy-history-service-visits-table.c |    8 +-
 lib/history/ephy-history-service.c              |    2 +-
 lib/history/ephy-history-types.h                |   10 +-
 src/ephy-history-dialog.c                       |    5 +-
 src/profile-migrator/ephy-profile-migrator.c    |   94 +++++++++++++++++++++++
 tests/ephy-history-test.c                       |    2 +-
 9 files changed, 112 insertions(+), 19 deletions(-)
---
diff --git a/lib/ephy-profile-utils.h b/lib/ephy-profile-utils.h
index ac4841d..ae1ecc8 100644
--- a/lib/ephy-profile-utils.h
+++ b/lib/ephy-profile-utils.h
@@ -24,7 +24,7 @@
 
 G_BEGIN_DECLS
 
-#define EPHY_PROFILE_MIGRATION_VERSION 18
+#define EPHY_PROFILE_MIGRATION_VERSION 19
 #define EPHY_INSECURE_PASSWORDS_MIGRATION_VERSION 11
 #define EPHY_SETTINGS_MIGRATION_VERSION 16
 #define EPHY_FIREFOX_SYNC_PASSWORDS_MIGRATION_VERSION 18
diff --git a/lib/history/ephy-history-service-hosts-table.c b/lib/history/ephy-history-service-hosts-table.c
index 52cee84..77aec64 100644
--- a/lib/history/ephy-history-service-hosts-table.c
+++ b/lib/history/ephy-history-service-hosts-table.c
@@ -296,7 +296,7 @@ ephy_history_service_find_host_rows (EphyHistoryService *self, EphyHistoryQuery
     return NULL;
   }
   if (query->from > 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->from, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->from, &error) == FALSE) {
       g_warning ("Could not build hosts table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
@@ -304,7 +304,7 @@ ephy_history_service_find_host_rows (EphyHistoryService *self, EphyHistoryQuery
     }
   }
   if (query->to > 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->to, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->to, &error) == FALSE) {
       g_warning ("Could not build hosts table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
diff --git a/lib/history/ephy-history-service-urls-table.c b/lib/history/ephy-history-service-urls-table.c
index 2838b13..fd69bca 100644
--- a/lib/history/ephy-history-service-urls-table.c
+++ b/lib/history/ephy-history-service-urls-table.c
@@ -317,7 +317,7 @@ ephy_history_service_find_url_rows (EphyHistoryService *self, EphyHistoryQuery *
   }
 
   if (query->from > 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->from, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->from, &error) == FALSE) {
       g_warning ("Could not build urls table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
@@ -325,7 +325,7 @@ ephy_history_service_find_url_rows (EphyHistoryService *self, EphyHistoryQuery *
     }
   }
   if (query->to > 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->to, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->to, &error) == FALSE) {
       g_warning ("Could not build urls table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
diff --git a/lib/history/ephy-history-service-visits-table.c b/lib/history/ephy-history-service-visits-table.c
index 5609a01..e418deb 100644
--- a/lib/history/ephy-history-service-visits-table.c
+++ b/lib/history/ephy-history-service-visits-table.c
@@ -68,7 +68,7 @@ ephy_history_service_add_visit_row (EphyHistoryService *self, EphyHistoryPageVis
   }
 
   if (ephy_sqlite_statement_bind_int (statement, 0, visit->url->id, &error) == FALSE ||
-      ephy_sqlite_statement_bind_int (statement, 1, visit->visit_time, &error) == FALSE ||
+      ephy_sqlite_statement_bind_int64 (statement, 1, visit->visit_time, &error) == FALSE ||
       ephy_sqlite_statement_bind_int (statement, 2, visit->visit_type, &error) == FALSE) {
     g_warning ("Could not build visits table addition statement: %s", error->message);
     g_error_free (error);
@@ -92,7 +92,7 @@ create_page_visit_from_statement (EphySQLiteStatement *statement)
 {
   EphyHistoryPageVisit *visit =
     ephy_history_page_visit_new (NULL,
-                                 ephy_sqlite_statement_get_column_as_int (statement, 1),
+                                 ephy_sqlite_statement_get_column_as_int64 (statement, 1),
                                  ephy_sqlite_statement_get_column_as_int (statement, 2));
   visit->url->id = ephy_sqlite_statement_get_column_as_int (statement, 0);
   return visit;
@@ -157,7 +157,7 @@ ephy_history_service_find_visit_rows (EphyHistoryService *self, EphyHistoryQuery
   }
 
   if (query->from >= 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->from, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->from, &error) == FALSE) {
       g_warning ("Could not build urls table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
@@ -165,7 +165,7 @@ ephy_history_service_find_visit_rows (EphyHistoryService *self, EphyHistoryQuery
     }
   }
   if (query->to >= 0) {
-    if (ephy_sqlite_statement_bind_int (statement, i++, (int)query->to, &error) == FALSE) {
+    if (ephy_sqlite_statement_bind_int64 (statement, i++, query->to, &error) == FALSE) {
       g_warning ("Could not build urls table query statement: %s", error->message);
       g_error_free (error);
       g_object_unref (statement);
diff --git a/lib/history/ephy-history-service.c b/lib/history/ephy-history-service.c
index 57eff46..5c92776 100644
--- a/lib/history/ephy-history-service.c
+++ b/lib/history/ephy-history-service.c
@@ -1317,7 +1317,7 @@ ephy_history_service_visit_url (EphyHistoryService      *self,
   g_signal_emit (self, signals[VISIT_URL], 0, url, visit_type);
 
   visit = ephy_history_page_visit_new (url,
-                                       time (NULL),
+                                       g_get_real_time (),
                                        visit_type);
   ephy_history_service_add_visit (self,
                                   visit, NULL, NULL, NULL);
diff --git a/lib/history/ephy-history-types.h b/lib/history/ephy-history-types.h
index 7911638..1a45e99 100644
--- a/lib/history/ephy-history-types.h
+++ b/lib/history/ephy-history-types.h
@@ -73,8 +73,8 @@ typedef struct _EphyHistoryURL
   char* title;
   int visit_count;
   int typed_count;
-  gint64 last_visit_time;
-  gint64 thumbnail_time;
+  gint64 last_visit_time; /* Microseconds */
+  gint64 thumbnail_time;  /* Seconds */
   gboolean hidden;
   EphyHistoryHost *host;
 } EphyHistoryURL;
@@ -83,14 +83,14 @@ typedef struct _EphyHistoryPageVisit
 {
   EphyHistoryURL* url;
   int id;
-  gint64 visit_time;
+  gint64 visit_time; /* Microseconds */
   EphyHistoryPageVisitType visit_type;
 } EphyHistoryPageVisit;
 
 typedef struct _EphyHistoryQuery
 {
-  gint64 from;
-  gint64 to;
+  gint64 from;  /* Microseconds */
+  gint64 to;    /* Microseconds */
   guint limit;
   GList* substring_list;
   gboolean ignore_hidden;
diff --git a/src/ephy-history-dialog.c b/src/ephy-history-dialog.c
index b1e43c7..38f029b 100644
--- a/src/ephy-history-dialog.c
+++ b/src/ephy-history-dialog.c
@@ -697,16 +697,15 @@ convert_date_data_func (GtkTreeViewColumn *column,
 {
   int col_id = GPOINTER_TO_INT (user_data);
   gint64 value;
-  time_t time;
   char *friendly;
 
   gtk_tree_model_get (model, iter,
                       col_id,
                       &value,
                       -1);
-  time = (time_t)value;
 
-  friendly = ephy_time_helpers_utf_friendly_time (time);
+  /* Convert back to seconds. */
+  friendly = ephy_time_helpers_utf_friendly_time (value / 1000000);
   g_object_set (renderer, "text", friendly, NULL);
   g_free (friendly);
 }
diff --git a/src/profile-migrator/ephy-profile-migrator.c b/src/profile-migrator/ephy-profile-migrator.c
index 1a3780a..effd7cc 100644
--- a/src/profile-migrator/ephy-profile-migrator.c
+++ b/src/profile-migrator/ephy-profile-migrator.c
@@ -1050,6 +1050,99 @@ out:
 }
 
 static void
+migrate_history_to_firefox_sync_history (void)
+{
+  EphySQLiteConnection *history_db = NULL;
+  EphySQLiteStatement *statement = NULL;
+  GSList *ids = NULL;
+  GError *error = NULL;
+  char *history_filename;
+  const char *sql_query;
+  int id;
+
+  history_filename = g_build_filename (ephy_dot_dir (), EPHY_HISTORY_FILE, NULL);
+  if (!g_file_test (history_filename, G_FILE_TEST_EXISTS)) {
+    LOG ("There is no history to migrate...");
+    goto out;
+  }
+
+  history_db = ephy_sqlite_connection_new (EPHY_SQLITE_CONNECTION_MODE_READWRITE);
+  ephy_sqlite_connection_open (history_db, history_filename, &error);
+  if (error) {
+    g_warning ("Failed to open history database: %s\n", error->message);
+    goto out;
+  }
+
+  /* Get the ID of each row in table. */
+  sql_query = "SELECT DISTINCT urls.id FROM urls ORDER BY urls.id";
+  statement = ephy_sqlite_connection_create_statement (history_db, sql_query, &error);
+  if (error) {
+    g_warning ("Failed to create SQLite statement: %s", error->message);
+    goto out;
+  }
+
+  while (ephy_sqlite_statement_step (statement, &error)) {
+    if (error) {
+      g_warning ("Error in ephy_sqlite_statement_step: %s", error->message);
+      g_error_free (error);
+      error = NULL;
+    } else {
+      id = ephy_sqlite_statement_get_column_as_int (statement, 0);
+      ids = g_slist_prepend (ids, GINT_TO_POINTER (id));
+    }
+  }
+
+  /* Add new sync_id column. */
+  sql_query = "ALTER TABLE urls ADD COLUMN sync_id LONGVARCAR";
+  ephy_sqlite_connection_execute (history_db, sql_query, &error);
+  if (error) {
+    g_warning ("Failed to add new column to urls table: %s", error->message);
+    goto out;
+  }
+
+  /* Set sync_id for each row. */
+  for (GSList *l = ids; l && l->data; l = l->next) {
+    char *sync_id = ephy_sync_crypto_get_random_sync_id ();
+    char *sql = g_strdup_printf ("UPDATE urls SET sync_id = \"%s\" WHERE id=%d",
+                                 sync_id, GPOINTER_TO_INT (l->data));
+
+    ephy_sqlite_connection_execute (history_db, sql, &error);
+    if (error) {
+      g_warning ("Failed to set sync ID: %s", error->message);
+      g_error_free (error);
+      error = NULL;
+    }
+
+    g_free (sync_id);
+    g_free (sql);
+  }
+
+  /* Update visit timestamps to microseconds. */
+  sql_query = "UPDATE urls SET last_visit_time = last_visit_time * 1000000";
+  ephy_sqlite_connection_execute (history_db, sql_query, &error);
+  if (error) {
+    g_warning ("Failed to update last visit time to microseconds: %s", error->message);
+    goto out;
+  }
+
+  sql_query = "UPDATE visits SET visit_time = visit_time * 1000000";
+  ephy_sqlite_connection_execute (history_db, sql_query, &error);
+  if (error)
+    g_warning ("Failed to update visit time to microseconds: %s", error->message);
+
+out:
+  g_free (history_filename);
+  if (history_db)
+    g_object_unref (history_db);
+  if (statement)
+    g_object_unref (statement);
+  if (ids)
+    g_slist_free (ids);
+  if (error)
+    g_error_free (error);
+}
+
+static void
 migrate_nothing (void)
 {
   /* Used to replace migrators that have been removed. Only remove migrators
@@ -1081,6 +1174,7 @@ const EphyProfileMigrator migrators[] = {
   /* 16 */ migrate_settings,
   /* 17 */ migrate_search_engines,
   /* 18 */ migrate_passwords_to_firefox_sync_passwords,
+  /* 19 */ migrate_history_to_firefox_sync_history,
 };
 
 static gboolean
diff --git a/tests/ephy-history-test.c b/tests/ephy-history-test.c
index 33d66a2..9a45298 100644
--- a/tests/ephy-history-test.c
+++ b/tests/ephy-history-test.c
@@ -419,7 +419,7 @@ perform_complex_url_query_with_time_range (EphyHistoryService *service,
   query->substring_list = g_list_prepend (query->substring_list, (gpointer)"k");
   query->limit = 1;
   query->sort_type = EPHY_HISTORY_SORT_MOST_VISITED;
-  query->from = 500;
+  query->from = 500 * 1000000;  /* Microseconds */
 
   /* The expected result. */
   url = ephy_history_url_new ("http://www.webkitgtk.org";,


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