Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eliminate 'goto's in dbcheck.cpp and musicdbcheck.cpp #913

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 24 additions & 30 deletions mythplugins/mythmusic/mythmusic/musicdbcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ const QString currentDatabaseVersion = "1025";
const QString MythMusicVersionName = "MusicDBSchemaVer";

static bool doUpgradeMusicDatabaseSchema(QString &dbver);
static bool tryUpgradeMusicDatabaseSchema();

bool UpgradeMusicDatabaseSchema(void)
{
#ifdef IGNORE_SCHEMA_VER_MISMATCH
return true;
#endif
SchemaUpgradeWizard *schema_wizard = nullptr;

// Suppress DB messages and turn of the settings cache,
// These are likely to confuse the users and the code, respectively.
GetMythDB()->SetSuppressDBMessages(true);
Expand All @@ -46,65 +45,60 @@ bool UpgradeMusicDatabaseSchema(void)
if (!locked)
{
LOG(VB_GENERAL, LOG_INFO, "Failed to get schema upgrade lock");
goto upgrade_error_exit;
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
return false;
}

schema_wizard = SchemaUpgradeWizard::Get(
bool success = tryUpgradeMusicDatabaseSchema();

// On any exit we want to re-enable the DB messages so errors
// are reported and we want to make sure the setting cache is
// enabled for good performance and we must unlock the schema
// lock.
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
DBUtil::UnlockSchema(query);
return success;
}

static bool tryUpgradeMusicDatabaseSchema()
{
SchemaUpgradeWizard* schema_wizard = SchemaUpgradeWizard::Get(
"MusicDBSchemaVer", "MythMusic", currentDatabaseVersion);

if (schema_wizard->Compare() == 0) // DB schema is what we need it to be..
goto upgrade_ok_exit;
return true;

if (schema_wizard->m_DBver.isEmpty())
{
// We need to create a database from scratch
if (doUpgradeMusicDatabaseSchema(schema_wizard->m_DBver))
goto upgrade_ok_exit;
else
goto upgrade_error_exit;
return doUpgradeMusicDatabaseSchema(schema_wizard->m_DBver);
}

// Pop up messages, questions, warnings, et c.
switch (schema_wizard->PromptForUpgrade("Music", true, false))
{
case MYTH_SCHEMA_USE_EXISTING:
goto upgrade_ok_exit;
return true;
case MYTH_SCHEMA_ERROR:
case MYTH_SCHEMA_EXIT:
goto upgrade_error_exit;
return false;
case MYTH_SCHEMA_UPGRADE:
break;
}

if (!doUpgradeMusicDatabaseSchema(schema_wizard->m_DBver))
{
LOG(VB_GENERAL, LOG_ERR, "Database schema upgrade failed.");
goto upgrade_error_exit;
return false;
}

LOG(VB_GENERAL, LOG_INFO, "MythMusic database schema upgrade complete.");

// On any exit we want to re-enable the DB messages so errors
// are reported and we want to make sure the setting cache is
// enabled for good performance and we must unlock the schema
// lock. We use gotos with labels so it's impossible to miss
// these steps.
upgrade_ok_exit:
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
if (locked)
DBUtil::UnlockSchema(query);
return true;

upgrade_error_exit:
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
if (locked)
DBUtil::UnlockSchema(query);
return false;
}


static bool doUpgradeMusicDatabaseSchema(QString &dbver)
{
if (dbver.isEmpty())
Expand Down
56 changes: 27 additions & 29 deletions mythtv/libs/libmythtv/dbcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
const QString currentDatabaseVersion = MYTH_DATABASE_VERSION;

static bool doUpgradeTVDatabaseSchema(void);
static bool tryUpgradeTVDatabaseSchema(bool upgradeAllowed, bool upgradeIfNoUI, bool informSystemd);

#if CONFIG_SYSTEMD_NOTIFY
#include <systemd/sd-daemon.h>
Expand Down Expand Up @@ -365,8 +366,6 @@ bool UpgradeTVDatabaseSchema(const bool upgradeAllowed,
#ifdef IGNORE_SCHEMA_VER_MISMATCH
return true;
#endif
SchemaUpgradeWizard *schema_wizard = nullptr;

// Suppress DB messages and turn of the settings cache,
// These are likely to confuse the users and the code, respectively.
GetMythDB()->SetSuppressDBMessages(true);
Expand All @@ -387,14 +386,34 @@ bool UpgradeTVDatabaseSchema(const bool upgradeAllowed,
if (!locked)
{
LOG(VB_GENERAL, LOG_INFO, "Failed to get schema upgrade lock");
goto upgrade_error_exit;
if (informSystemd)
db_sd_notify("failed to get schema upgrade lock");
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
return false;
}

bool success = tryUpgradeTVDatabaseSchema(upgradeAllowed, upgradeIfNoUI, informSystemd);

// On any exit we want to re-enable the DB messages so errors
// are reported and we want to make sure the setting cache is
// enabled for good performance and we must unlock the schema
// lock.
if (informSystemd)
db_sd_notify(success ? "success" : "failed");
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
DBUtil::UnlockSchema(query);
return success;
}

static bool tryUpgradeTVDatabaseSchema(bool upgradeAllowed, bool upgradeIfNoUI, bool informSystemd)
{
// Determine if an upgrade is needed
schema_wizard = SchemaUpgradeWizard::Get(
SchemaUpgradeWizard* schema_wizard = SchemaUpgradeWizard::Get(
"DBSchemaVer", "MythTV", currentDatabaseVersion);
if (schema_wizard->Compare() == 0) // DB schema is what we need it to be..
goto upgrade_ok_exit;
return true;

if (!upgradeAllowed)
LOG(VB_GENERAL, LOG_WARNING, "Not allowed to upgrade the database.");
Expand All @@ -406,10 +425,10 @@ bool UpgradeTVDatabaseSchema(const bool upgradeAllowed,
"TV", upgradeAllowed, upgradeIfNoUI, MINIMUM_DBMS_VERSION))
{
case MYTH_SCHEMA_USE_EXISTING:
goto upgrade_ok_exit;
return true;
case MYTH_SCHEMA_ERROR:
case MYTH_SCHEMA_EXIT:
goto upgrade_error_exit;
return false;
case MYTH_SCHEMA_UPGRADE:
break;
}
Expand All @@ -423,33 +442,12 @@ bool UpgradeTVDatabaseSchema(const bool upgradeAllowed,
if (!doUpgradeTVDatabaseSchema())
{
LOG(VB_GENERAL, LOG_ERR, "Database schema upgrade failed.");
goto upgrade_error_exit;
return false;
}

LOG(VB_GENERAL, LOG_INFO, "Database schema upgrade complete.");

// On any exit we want to re-enable the DB messages so errors
// are reported and we want to make sure the setting cache is
// enabled for good performance and we must unlock the schema
// lock. We use gotos with labels so it's impossible to miss
// these steps.
upgrade_ok_exit:
if (informSystemd)
db_sd_notify("success");
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
if (locked)
DBUtil::UnlockSchema(query);
return true;

upgrade_error_exit:
if (informSystemd)
db_sd_notify("failed");
GetMythDB()->SetSuppressDBMessages(false);
gCoreContext->ActivateSettingsCache(true);
if (locked)
DBUtil::UnlockSchema(query);
return false;
}

/** \fn doUpgradeTVDatabaseSchema(void)
Expand Down
Loading