A good way to handle this kind of issue is to add a versionning system in the database.
Before using the connection to the database, simply check the app version in the database and, if the new version is higher of the previous one, run all the necessary commands to update your database.
Ex:
public async Task<SQLite.SQLiteConnection> GetSqliteConnectionForUserAsync(string login)
{
using (await _mutex.LockAsync())
{
if (login == null)
{
login = "__anonymous__";
}
SQLite.SQLiteConnection conn;
if (!_userConnections.TryGetValue(login, out conn))
{
conn = new SQLite.SQLiteConnection(Path.Combine(ApplicationData.Current.LocalFolder.Path,
string.Format("{0}.db", Uri.EscapeDataString(login))));
await SqlSchemaHandler.EnsureSchemaReadyAsync(conn, s =>
{
_logger.Info("Schema handler message : {0}", s);
});
_userConnections[login] = conn;
}
return conn;
}
}
And (SqlSchemaHandler):
public static Task EnsureSchemaReadyAsync(SQLiteConnection connection, Action<string> actionReporter)
{
return Task.Run(() =>
{
connection.CreateTable<SchemaInfo>();
var schemaInfo = connection.Table<SchemaInfo>().FirstOrDefault();
if (schemaInfo == null)
{
ApplyV0ToV1(connection);
schemaInfo = new SchemaInfo { Id = 1, Version = 1 };
connection.Insert(schemaInfo);
}
});
}
private static void ApplyV0ToV1(SQLiteConnection connection)
{
connection.CreateTable<Test>();
}
Thanks,