Cleanup database migration code

This commit is contained in:
topjohnwu 2022-03-28 00:59:16 -07:00
parent 9e8218089b
commit 7e7ddeb9e2

View File

@ -154,45 +154,58 @@ static char *open_and_init_db(sqlite3 *&db) {
sqlite3_close(db); sqlite3_close(db);
return strdup("Downgrading database is not supported"); return strdup("Downgrading database is not supported");
} }
if (ver < 3) {
// Policies auto create_policy = [&] {
sqlite3_exec(db, sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS policies " "CREATE TABLE IF NOT EXISTS policies "
"(uid INT, package_name TEXT, policy INT, until INT, " "(uid INT, package_name TEXT, policy INT, until INT, "
"logging INT, notification INT, PRIMARY KEY(uid))", "logging INT, notification INT, PRIMARY KEY(uid))",
nullptr, nullptr, &err); nullptr, nullptr, &err);
err_ret(err); };
// Settings auto create_settings = [&] {
sqlite3_exec(db, sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS settings " "CREATE TABLE IF NOT EXISTS settings "
"(key TEXT, value INT, PRIMARY KEY(key))", "(key TEXT, value INT, PRIMARY KEY(key))",
nullptr, nullptr, &err); nullptr, nullptr, &err);
err_ret(err); };
ver = 3; auto create_strings = [&] {
upgrade = true;
}
if (ver < 4) {
// Strings
sqlite3_exec(db, sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS strings " "CREATE TABLE IF NOT EXISTS strings "
"(key TEXT, value TEXT, PRIMARY KEY(key))", "(key TEXT, value TEXT, PRIMARY KEY(key))",
nullptr, nullptr, &err); nullptr, nullptr, &err);
err_ret(err); };
/* Directly jump to version 6 */ auto create_denylist = [&] {
ver = 6;
upgrade = true;
}
if (ver < 7) {
sqlite3_exec(db, sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS hidelist " "CREATE TABLE IF NOT EXISTS denylist "
"(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));", "(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process))",
nullptr, nullptr, &err); nullptr, nullptr, &err);
};
// Database changelog:
//
// 0 - 6: DB stored in app private data. There are no longer any code in the project to
// migrate these data, so no need to take any of these versions into consideration.
// 7 : create table 'hidelist' (process TEXT, PRIMARY KEY(process))
// 8 : add new column (package_name TEXT) to table 'hidelist'
// 9 : rebuild table 'hidelist' to update primary key (PRIMARY KEY(package_name, process))
// 10: remove table 'logs'
// 11: remove table 'hidelist' and create table 'denylist' (same data structure)
if (/* 0, 1, 2, 3, 4, 5, 6 */ ver <= 6) {
create_policy();
err_ret(err); err_ret(err);
/* Directly jump to version 9 */ create_settings();
ver = 9; err_ret(err);
create_strings();
err_ret(err);
create_denylist();
err_ret(err);
// Directly jump to latest
ver = DB_VERSION;
upgrade = true; upgrade = true;
} }
if (ver < 8) { if (ver == 7) {
sqlite3_exec(db, sqlite3_exec(db,
"BEGIN TRANSACTION;" "BEGIN TRANSACTION;"
"ALTER TABLE hidelist RENAME TO hidelist_tmp;" "ALTER TABLE hidelist RENAME TO hidelist_tmp;"
@ -203,11 +216,11 @@ static char *open_and_init_db(sqlite3 *&db) {
"COMMIT;", "COMMIT;",
nullptr, nullptr, &err); nullptr, nullptr, &err);
err_ret(err); err_ret(err);
/* Directly jump to version 9 */ // Directly jump to version 9
ver = 9; ver = 9;
upgrade = true; upgrade = true;
} }
if (ver < 9) { if (ver == 8) {
sqlite3_exec(db, sqlite3_exec(db,
"BEGIN TRANSACTION;" "BEGIN TRANSACTION;"
"ALTER TABLE hidelist RENAME TO hidelist_tmp;" "ALTER TABLE hidelist RENAME TO hidelist_tmp;"
@ -221,20 +234,20 @@ static char *open_and_init_db(sqlite3 *&db) {
ver = 9; ver = 9;
upgrade = true; upgrade = true;
} }
if (ver < 10) { if (ver == 9) {
sqlite3_exec(db, "DROP TABLE IF EXISTS logs", nullptr, nullptr, &err); sqlite3_exec(db, "DROP TABLE IF EXISTS logs", nullptr, nullptr, &err);
err_ret(err); err_ret(err);
ver = 10; ver = 10;
upgrade = true; upgrade = true;
} }
if (ver < 11) { if (ver == 10) {
sqlite3_exec(db, sqlite3_exec(db,
"DROP TABLE IF EXISTS hidelist;" "DROP TABLE IF EXISTS hidelist;"
"CREATE TABLE IF NOT EXISTS denylist "
"(package_name TEXT, process TEXT, PRIMARY KEY(package_name, process));"
"DELETE FROM settings WHERE key='magiskhide';", "DELETE FROM settings WHERE key='magiskhide';",
nullptr, nullptr, &err); nullptr, nullptr, &err);
err_ret(err); err_ret(err);
create_denylist();
err_ret(err);
ver = 11; ver = 11;
upgrade = true; upgrade = true;
} }