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 https://github.com/hughsie/fwupd/issues/909
This commit is contained in:
Richard Hughes 2019-01-02 20:18:14 +00:00
parent 305f1f2ab1
commit f692b71dc4

View File

@ -157,7 +157,7 @@ fu_history_create_database (FuHistory *self, GError **error)
"CREATE TABLE schema (" "CREATE TABLE schema ("
"created timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP," "created timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,"
"version INTEGER DEFAULT 0);" "version INTEGER DEFAULT 0);"
"INSERT INTO schema (version) VALUES (3);" "INSERT INTO schema (version) VALUES (4);"
"CREATE TABLE history (" "CREATE TABLE history ("
"device_id TEXT," "device_id TEXT,"
"update_state INTEGER DEFAULT 0," "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 */ /* rename the table to something out the way */
rc = sqlite3_exec (self->db, 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); NULL, NULL, NULL);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL,
@ -236,7 +237,7 @@ fu_history_migrate_database_v2 (FuHistory *self, GError **error)
} }
/* update version */ /* 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) { if (rc != SQLITE_OK) {
g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL, g_set_error (error, FWUPD_ERROR, FWUPD_ERROR_INTERNAL,
"Failed to migrate database: %s", "Failed to migrate database: %s",
@ -246,6 +247,29 @@ fu_history_migrate_database_v2 (FuHistory *self, GError **error)
return TRUE; 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 */ /* returns 0 if database is not initialised */
static guint static guint
fu_history_get_schema_version (FuHistory *self) 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); g_debug ("migrating v%u database", schema_ver);
if (!fu_history_migrate_database_v2 (self, error)) if (!fu_history_migrate_database_v2 (self, error))
return FALSE; 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; return TRUE;