Closes #5324. Added a simple database upgrade mechanism.
This commit is contained in:
parent
b00dee6643
commit
963627ef6f
2 changed files with 50 additions and 2 deletions
|
|
@ -6,11 +6,17 @@
|
|||
|
||||
public abstract class Geary.Sqlite.Database {
|
||||
internal SQLHeavy.VersionedDatabase db;
|
||||
|
||||
internal File schema_dir;
|
||||
|
||||
private Gee.HashMap<SQLHeavy.Table, Geary.Sqlite.Table> table_map = new Gee.HashMap<
|
||||
SQLHeavy.Table, Geary.Sqlite.Table>();
|
||||
|
||||
|
||||
public signal void pre_upgrade(int version);
|
||||
|
||||
public signal void post_upgrade(int version);
|
||||
|
||||
public Database(File db_file, File schema_dir) throws Error {
|
||||
this.schema_dir = schema_dir;
|
||||
if (!db_file.get_parent().query_exists())
|
||||
db_file.get_parent().make_directory_with_parents();
|
||||
|
||||
|
|
@ -41,5 +47,36 @@ public abstract class Geary.Sqlite.Database {
|
|||
|
||||
return t;
|
||||
}
|
||||
|
||||
public void upgrade() throws Error {
|
||||
// Get the SQLite database version.
|
||||
SQLHeavy.QueryResult result = db.execute("PRAGMA user_version;");
|
||||
int db_version = result.fetch_int();
|
||||
debug("Current db version: %d", db_version);
|
||||
|
||||
// Go through all the version scripts in the schema directory and apply each of them.
|
||||
File upgrade_script;
|
||||
while ((upgrade_script = get_upgrade_script(++db_version)).query_exists()) {
|
||||
pre_upgrade(db_version);
|
||||
|
||||
try {
|
||||
debug("Upgrading to %d", db_version);
|
||||
string upgrade_contents;
|
||||
FileUtils.get_contents(upgrade_script.get_path(), out upgrade_contents);
|
||||
db.run(upgrade_contents);
|
||||
db.run("PRAGMA user_version = %d;".printf(db_version));
|
||||
} catch (Error e) {
|
||||
// TODO Add rollback of changes here when switching away from SQLHeavy.
|
||||
warning("Error upgrading database: %s", e.message);
|
||||
throw e;
|
||||
}
|
||||
|
||||
post_upgrade(db_version);
|
||||
}
|
||||
}
|
||||
|
||||
private File get_upgrade_script(int version) {
|
||||
return File.new_for_path("%s/Version-%03d.sql".printf(schema_dir.get_path(), version));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,9 @@ private class Geary.Sqlite.Account : Object {
|
|||
|
||||
try {
|
||||
db = new ImapDatabase(cred.user, user_data_dir, resource_dir);
|
||||
db.pre_upgrade.connect(on_pre_upgrade);
|
||||
db.post_upgrade.connect(on_post_upgrade);
|
||||
db.upgrade();
|
||||
} catch (Error err) {
|
||||
error("Unable to open database: %s", err.message);
|
||||
}
|
||||
|
|
@ -209,5 +212,13 @@ private class Geary.Sqlite.Account : Object {
|
|||
// drop from folder references table, all cleaned up
|
||||
folder_refs.unset(folder_ref.path);
|
||||
}
|
||||
|
||||
private void on_pre_upgrade(int version){
|
||||
// TODO Add per-version data massaging.
|
||||
}
|
||||
|
||||
private void on_post_upgrade(int version){
|
||||
// TODO Add per-version data massaging.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue