Skip to content

Commit

Permalink
Add database versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
dospuntos committed May 26, 2024
1 parent a311503 commit 237d0d6
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#define UNLOCK ;
#endif

#define DB_VERSION 1

// This is the system default locale, which is US-style. It is used for storage
// and the default preferred locale
Locale gDefaultLocale;
Expand Down Expand Up @@ -123,6 +125,15 @@ Database::CreateFile(const char* path)
// Transaction Status: 10
// Currency Symbol/Decimal: 2

DBCommand(
"CREATE TABLE db_version (version INTEGER PRIMARY KEY);",
"DatabaseCreateFile:create db_version");
BString command;
command << "INSERT INTO db_version (version) VALUES (" << DB_VERSION << ");";
DBCommand(
command.String(),
"Database::CreateFile:create db_version");

DBCommand(
"create table accountlist (accountid int primary key, name varchar(96), "
"type varchar(12), status varchar(30));",
Expand Down Expand Up @@ -192,6 +203,18 @@ Database::OpenFile(const char* path)
return B_ERROR;
}

// Check for and apply DB migrations
if (ApplyMigrations() != B_OK) {
UNLOCK;
ShowAlert("Error upgrading database",
"An error occurred while updating the database to the latest version. "
"This may affect the functionality of the application.");
BString error;
error << "Database::ApplyMigrations() failed";
ShowBug(error.String());
return B_ERROR;
}

// Populate account list
CppSQLite3Query query =
DBQuery("select * from accountlist", "Database::OpenFile:get accounts from list");
Expand Down Expand Up @@ -1493,3 +1516,47 @@ Database::DBQuery(const char* query, const char* functionname)
// this will never be reached - just to shut up the compiler
return CppSQLite3Query();
}

status_t
Database::ApplyMigrations() {
int currentVersion = 0;

// Check db_version, create if it doesn't exist. Set version to 1
CppSQLite3Query query =
DBQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='db_version'",
"Database::OpenFile:check for db_version table");
if (query.eof()) {
DBCommand(
"CREATE TABLE db_version (version INTEGER PRIMARY KEY);",
"DatabaseCreateFile:create db_version");
DBCommand(
"INSERT INTO db_version (version) VALUES (1);",
"Database::CreateFile:create db_version");
}

// Get current db_version
query =
DBQuery("SELECT version FROM db_version","Database::ApplyMigrations: get currentVersion");

if (query.eof())
ShowBug("Error getting database version.");

currentVersion = query.getIntField(0);

// Apply all missing migrations
if (currentVersion < 2) {
/* Example usage:
// Remove an unused column
query =
DBQuery("ALTER TABLE accountlocale DROP COLUMN dateseparator;",
"Database::ApplyMigrations: remove dateseparator");
// Update version number
query =
DBQuery("UPDATE db_version SET version = 2;",
"Database::ApplyMigrations:update version");
currentVersion = 2;
*/
}

return B_OK;
}
1 change: 1 addition & 0 deletions src/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Database : public Notifier {
void CreateFile(const char* path);
status_t OpenFile(const char* path);
void CloseFile(void);
status_t ApplyMigrations();

Account* AddAccount(const char* name, const AccountType& type, const char* status = "Open",
const Locale* locale = NULL);
Expand Down

0 comments on commit 237d0d6

Please sign in to comment.