Use transaction for database migration

This commit is contained in:
Marvin W 2019-12-15 19:03:02 +01:00
parent 97647f9b52
commit a4a795af33
No known key found for this signature in database
GPG key ID: 072E9235DB996F2A
2 changed files with 12 additions and 2 deletions

View file

@ -38,6 +38,11 @@ public class Database {
} }
private void start_migration() { private void start_migration() {
try {
exec("BEGIN TRANSACTION");
} catch (Error e) {
error("SQLite error: %d - %s", db.errcode(), db.errmsg());
}
meta_table.create_table_at_version(expected_version); meta_table.create_table_at_version(expected_version);
long old_version = 0; long old_version = 0;
old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1]; old_version = meta_table.row_with(meta_name, "version")[meta_int_val, -1];
@ -66,6 +71,11 @@ public class Database {
foreach (Table t in tables) { foreach (Table t in tables) {
t.post(); t.post();
} }
try {
exec("END TRANSACTION");
} catch (Error e) {
error("SQLite error: %d - %s", db.errcode(), db.errmsg());
}
} }
internal int errcode() { internal int errcode() {

View file

@ -48,7 +48,7 @@ public class Table {
try { try {
db.exec(@"INSERT INTO _fts_$name(_fts_$name) VALUES('rebuild');"); db.exec(@"INSERT INTO _fts_$name(_fts_$name) VALUES('rebuild');");
} catch (Error e) { } catch (Error e) {
error(@"Qlite Error: Rebuilding FTS index: $(e.message)"); critical(@"Qlite Error: Rebuilding FTS index: $(e.message)");
} }
} }
@ -171,7 +171,7 @@ public class Table {
try { try {
db.exec(@"ALTER TABLE $name ADD COLUMN $(c.to_column_definition())"); db.exec(@"ALTER TABLE $name ADD COLUMN $(c.to_column_definition())");
} catch (Error e) { } catch (Error e) {
error(@"Qlite Error: Add columns for version: $(e.message)"); critical(@"Qlite Error: Add columns for version: $(e.message)");
} }
} }
} }