Skip to content

Commit

Permalink
Merge pull request #2979 from xerus2000/database-help
Browse files Browse the repository at this point in the history
Update database help message
  • Loading branch information
uklotzde authored Aug 6, 2020
2 parents 83c5b6b + 2f5be78 commit 56e7294
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 75 deletions.
72 changes: 36 additions & 36 deletions src/database/mixxxdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,47 +70,47 @@ MixxxDb::MixxxDb(

bool MixxxDb::initDatabaseSchema(
const QSqlDatabase& database,
const QString& schemaFile,
int schemaVersion) {
int schemaVersion,
const QString& schemaFile) {
QString okToExit = tr("Click OK to exit.");
QString upgradeFailed = tr("Cannot upgrade database schema");
QString upgradeToVersionFailed =
tr("Unable to upgrade your database schema to version %1")
.arg(QString::number(schemaVersion));
QString helpEmail = tr("For help with database issues contact:") + "\n" +
"mixxx[email protected]";

switch (SchemaManager(database).upgradeToSchemaVersion(schemaFile, schemaVersion)) {
case SchemaManager::Result::CurrentVersion:
case SchemaManager::Result::UpgradeSucceeded:
case SchemaManager::Result::NewerVersionBackwardsCompatible:
return true; // done
case SchemaManager::Result::UpgradeFailed:
QMessageBox::warning(
0, upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("Your mixxxdb.sqlite file may be corrupt.") + "\n" +
tr("Try renaming it and restarting Mixxx.") + "\n" +
helpEmail + "\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
case SchemaManager::Result::NewerVersionIncompatible:
QMessageBox::warning(
0, upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("Your mixxxdb.sqlite file was created by a newer "
"version of Mixxx and is incompatible.") +
"\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
case SchemaManager::Result::SchemaError:
QMessageBox::warning(
0, upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("The database schema file is invalid.") + "\n" +
helpEmail + "\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
QString helpContact = tr("For help with database issues consult:") + "\n" +
"https://www.mixxx.org/support";

switch (SchemaManager(database).upgradeToSchemaVersion(schemaVersion, schemaFile)) {
case SchemaManager::Result::CurrentVersion:
case SchemaManager::Result::UpgradeSucceeded:
case SchemaManager::Result::NewerVersionBackwardsCompatible:
return true; // done
case SchemaManager::Result::UpgradeFailed:
QMessageBox::warning(0,
upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("Your mixxxdb.sqlite file may be corrupt.") +
"\n" + tr("Try renaming it and restarting Mixxx.") +
"\n" + helpContact + "\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
case SchemaManager::Result::NewerVersionIncompatible:
QMessageBox::warning(0,
upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("Your mixxxdb.sqlite file was created by a newer "
"version of Mixxx and is incompatible.") +
"\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
case SchemaManager::Result::SchemaError:
QMessageBox::warning(0,
upgradeFailed,
upgradeToVersionFailed + "\n" +
tr("The database schema file is invalid.") + "\n" +
helpContact + "\n\n" + okToExit,
QMessageBox::Ok);
return false; // abort
}
// Suppress compiler warning
DEBUG_ASSERT(!"unhandled switch/case");
Expand Down
4 changes: 2 additions & 2 deletions src/database/mixxxdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class MixxxDb : public QObject {

static bool initDatabaseSchema(
const QSqlDatabase& database,
const QString& schemaFile = kDefaultSchemaFile,
int schemaVersion = kRequiredSchemaVersion);
int schemaVersion = kRequiredSchemaVersion,
const QString& schemaFile = kDefaultSchemaFile);

explicit MixxxDb(
const UserSettingsPointer& pConfig,
Expand Down
3 changes: 1 addition & 2 deletions src/database/schemamanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ bool SchemaManager::isBackwardsCompatibleWithVersion(int targetVersion) const {
}

SchemaManager::Result SchemaManager::upgradeToSchemaVersion(
const QString& schemaFilename,
int targetVersion) {
int targetVersion, const QString& schemaFilename) {
VERIFY_OR_DEBUG_ASSERT(m_currentVersion >= 0) {
return Result::UpgradeFailed;
}
Expand Down
4 changes: 1 addition & 3 deletions src/database/schemamanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ class SchemaManager {

bool isBackwardsCompatibleWithVersion(int targetVersion) const;

Result upgradeToSchemaVersion(
const QString& schemaFilename,
int targetVersion);
Result upgradeToSchemaVersion(int targetVersion, const QString& schemaFilename);

private:
QSqlDatabase m_database;
Expand Down
37 changes: 18 additions & 19 deletions src/test/schemamanager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ TEST_F(SchemaManagerTest, CanUpgradeFreshDatabaseToRequiredVersion) {
{
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeSucceeded, result);
}
// Subsequent upgrade(s)
{
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::CurrentVersion, result);
}
}

TEST_F(SchemaManagerTest, NonExistentSchema) {
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
":file_doesnt_exist.xml", MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, ":file_doesnt_exist.xml");
EXPECT_EQ(SchemaManager::Result::SchemaError, result);
}

Expand All @@ -36,22 +36,22 @@ TEST_F(SchemaManagerTest, BackwardsCompatibleVersion) {
// Upgrade to version 1 to get the settings table.
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, 1);
1, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeSucceeded, result);

SettingsDAO settings(dbConnection());

// Pretend the database version is one past the required version but
// min_compatible is the required version.
settings.setValue(SchemaManager::SETTINGS_VERSION_STRING,
MixxxDb::kRequiredSchemaVersion + 1);
MixxxDb::kRequiredSchemaVersion + 1);
settings.setValue(SchemaManager::SETTINGS_MINCOMPATIBLE_STRING,
MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion);
}

SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::NewerVersionBackwardsCompatible, result);
}

Expand All @@ -61,22 +61,22 @@ TEST_F(SchemaManagerTest, BackwardsIncompatibleVersion) {
// Upgrade to version 1 to get the settings table.
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, 1);
1, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeSucceeded, result);

SettingsDAO settings(dbConnection());

// Pretend the database version is one past the required version and
// min_compatible is one past the required version.
settings.setValue(SchemaManager::SETTINGS_VERSION_STRING,
MixxxDb::kRequiredSchemaVersion + 1);
MixxxDb::kRequiredSchemaVersion + 1);
settings.setValue(SchemaManager::SETTINGS_MINCOMPATIBLE_STRING,
MixxxDb::kRequiredSchemaVersion + 1);
MixxxDb::kRequiredSchemaVersion + 1);
}

SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::NewerVersionIncompatible, result);
}

Expand All @@ -86,18 +86,18 @@ TEST_F(SchemaManagerTest, IgnoreDuplicateColumn) {
// Upgrade to version 3 to get the modern library table.
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, 3);
3, MixxxDb::kDefaultSchemaFile);
ASSERT_EQ(SchemaManager::Result::UpgradeSucceeded, result);
}

// Add a column that will be added again in version 24.
QSqlQuery query(dbConnection());
ASSERT_TRUE(query.exec(
"ALTER TABLE library ADD COLUMN coverart_source TEXT"));
ASSERT_TRUE(
query.exec("ALTER TABLE library ADD COLUMN coverart_source TEXT"));

SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeSucceeded, result);
}

Expand All @@ -107,17 +107,16 @@ TEST_F(SchemaManagerTest, UpgradeFailed) {
// Upgrade to version 3 to get the modern library table.
SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, 3);
3, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeSucceeded, result);
}

// Drop a table that is expected to exist.
QSqlQuery query(dbConnection());
EXPECT_TRUE(query.exec(
"DROP TABLE PlaylistTracks"));
EXPECT_TRUE(query.exec("DROP TABLE PlaylistTracks"));

SchemaManager schemaManager(dbConnection());
SchemaManager::Result result = schemaManager.upgradeToSchemaVersion(
MixxxDb::kDefaultSchemaFile, MixxxDb::kRequiredSchemaVersion);
MixxxDb::kRequiredSchemaVersion, MixxxDb::kDefaultSchemaFile);
EXPECT_EQ(SchemaManager::Result::UpgradeFailed, result);
}
7 changes: 1 addition & 6 deletions src/track/trackid.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef TRACKID_H
#define TRACKID_H

#pragma once

#include "util/db/dbid.h"


class TrackId: public DbId {
public:
// Inherit constructors from base class
Expand All @@ -15,5 +12,3 @@ Q_DECLARE_TYPEINFO(TrackId, Q_MOVABLE_TYPE);
Q_DECLARE_METATYPE(TrackId)

typedef QList<TrackId> TrackIdList;

#endif // TRACKID_H
8 changes: 1 addition & 7 deletions src/util/db/dbconnectionpooler.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef MIXXX_DBCONNECTIONPOOLER_H
#define MIXXX_DBCONNECTIONPOOLER_H

#pragma once

#include "util/db/dbconnectionpool.h"


namespace mixxx {

// Manages the lifetime of a thread-local database connection that is
Expand Down Expand Up @@ -47,6 +44,3 @@ class DbConnectionPooler final {
};

} // namespace mixxx


#endif // MIXXX_DBCONNECTIONPOOLER_H

0 comments on commit 56e7294

Please sign in to comment.