void TrainDB::checkDBVersion() { // can we get a version number? QSqlQuery query("SELECT table_name, schema_version, creation_date from version;", db->database(sessionid)); bool rc = query.exec(); if (!rc) { // we couldn't read the version table properly // it must be out of date!! QSqlQuery dropM("DROP TABLE version", db->database(sessionid)); dropM.exec(); // recreate version table and add one entry QSqlQuery version("CREATE TABLE version ( table_name varchar primary key, schema_version integer, creation_date date );", db->database(sessionid)); version.exec(); // wipe away whatever (if anything is there) dropWorkoutTable(); createWorkoutTable(); dropVideoTable(); createVideoTable(); dropVideoSyncTable(); createVideoSyncTable(); return; } // ok we checked out ok, so lets adjust db schema to reflect // tne current version / crc bool dropWorkout = false; bool dropVideo = false; bool dropVideoSync = false; while (query.next()) { QString table_name = query.value(0).toString(); int currentversion = query.value(1).toInt(); if (table_name == "workouts" && currentversion != TrainDBSchemaVersion) dropWorkout = true; if (table_name == "videos" && currentversion != TrainDBSchemaVersion) dropVideo = true; if (table_name == "videosyncs" && currentversion != TrainDBSchemaVersion) dropVideoSync = true; } query.finish(); // "workouts" table, is it up-to-date? if (dropWorkout) dropWorkoutTable(); if (dropVideo) dropVideoTable(); if (dropVideoSync) dropVideoSyncTable(); }
// rebuild effectively drops and recreates all tables // but not the version table, since its about deleting // user data (e.g. when rescanning their hard disk) void TrainDB::rebuildDB() { dropWorkoutTable(); createWorkoutTable(); dropVideoTable(); createVideoTable(); }