Permalink
Browse files

Correctly migrate the history database

I forgot to include the protocol column when migrating to v3, so create a v4
which ignores the sqlite error if the column already exists.

Fixes #909
  • Loading branch information...
hughsie committed Jan 2, 2019
1 parent 305f1f2 commit 48106951c814f0bee201845fc1cc9cbb0f143faf
Showing with 31 additions and 3 deletions.
  1. +31 −3 src/fu-history.c
@@ -157,7 +157,7 @@ fu_history_create_database (FuHistory *self, GError **error)
"CREATE TABLE schema ("
"created timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
"version INTEGER DEFAULT 0);"
"INSERT INTO schema (version) VALUES (3);"
"INSERT INTO schema (version) VALUES (4);"
"CREATE TABLE history ("
"device_id TEXT,"
"update_state INTEGER DEFAULT 0,"
@@ -226,7 +226,8 @@ fu_history_migrate_database_v2 (FuHistory *self, GError **error)

/* rename the table to something out the way */
rc = sqlite3_exec (self->db,
"ALTER TABLE history ADD COLUMN checksum_device TEXT DEFAULT NULL;",
"ALTER TABLE history ADD COLUMN checksum_device TEXT DEFAULT NULL;"
"ALTER TABLE history ADD COLUMN protocol TEXT DEFAULT NULL;",
NULL, NULL, NULL);
if (rc != SQLITE_OK) {
g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL,
@@ -236,7 +237,7 @@ fu_history_migrate_database_v2 (FuHistory *self, GError **error)
}

/* update version */
rc = sqlite3_exec (self->db, "UPDATE schema SET version=3;", NULL, NULL, NULL);
rc = sqlite3_exec (self->db, "UPDATE schema SET version=4;", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL,
"Failed to migrate database: %s",
@@ -246,6 +247,29 @@ fu_history_migrate_database_v2 (FuHistory *self, GError **error)
return TRUE;
}

static gboolean
fu_history_migrate_database_v3 (FuHistory *self, GError **error)
{
gint rc;

/* rename the table to something out the way */
rc = sqlite3_exec (self->db,
"ALTER TABLE history ADD COLUMN protocol TEXT DEFAULT NULL;",
NULL, NULL, NULL);
if (rc != SQLITE_OK)
g_debug ("ignoring database error: %s", sqlite3_errmsg (self->db));

/* update version */
rc = sqlite3_exec (self->db, "UPDATE schema SET version=4;", NULL, NULL, NULL);
if (rc != SQLITE_OK) {
g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL,
"Failed to update schema version: %s",
sqlite3_errmsg (self->db));
return FALSE;
}
return TRUE;
}

/* returns 0 if database is not initialised */
static guint
fu_history_get_schema_version (FuHistory *self)
@@ -333,6 +357,10 @@ fu_history_load (FuHistory *self, GError **error)
g_debug ("migrating v%u database", schema_ver);
if (!fu_history_migrate_database_v2 (self, error))
return FALSE;
} else if (schema_ver == 3) {
g_debug ("migrating v%u database", schema_ver);
if (!fu_history_migrate_database_v3 (self, error))
return FALSE;
}

return TRUE;

0 comments on commit 4810695

Please sign in to comment.