Skip to content

[DB] Add force recreate parameter to cloudstack-setup-databases script #11239

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

Merged
merged 1 commit into from
Jul 28, 2025
Merged
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
15 changes: 14 additions & 1 deletion setup/bindir/cloud-setup-databases.in
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ class DBDeployer(object):
(value, index) = self.dbDotProperties[key]
return value

def areCloudDatabasesCreated(self):
cmd = "SELECT CASE WHEN COUNT(DISTINCT SCHEMA_NAME) >= 1 THEN 1 ELSE 0 END AS schema_exists \
FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME IN ('cloud', 'cloud_usage');"
databases = self.runMysql(cmd, "databases", self.rootuser != None)
return databases.replace("schema_exists", "").strip() == "1"

def runMysql(self, text, table, isRoot=False):
kwargs = {}
if not isRoot:
Expand All @@ -151,7 +157,7 @@ class DBDeployer(object):
open(self.tmpMysqlFile, 'w').write(text)
mysqlCmds.append('<')
mysqlCmds.append(self.tmpMysqlFile)
runCmd(mysqlCmds)
return runCmd(mysqlCmds)

except Exception as e:
err = '''Encountering an error when executing mysql script
Expand Down Expand Up @@ -233,6 +239,10 @@ for full help
("DROP USER 'cloud'@'%' ;", "DO NULL;")
)

if self.areCloudDatabasesCreated() and not self.options.forcerecreate:
self.errorAndExit("Aborting script as the databases (cloud, cloud_usage) already exist.\n" \
"Please use the --force-recreate parameter if you want to recreate the schemas.")

scriptsToRun = ["create-database","create-schema", "create-database-premium","create-schema-premium"]
if self.options.schemaonly:
scriptsToRun = ["create-schema", "create-schema-premium"]
Expand Down Expand Up @@ -610,6 +620,9 @@ for example:
help="Creates the db schema without having to pass root credentials - " \
"Please note: The databases (cloud, cloud_usage) and user (cloud) has to be configured " \
"manually prior to running this script when using this flag.")
self.parser.add_option("--force-recreate", action="store_true", dest="forcerecreate", default=False,
help="Force recreation of the existing DB schemas. This option is disabled by default." \
"Please note: The databases (cloud, cloud_usage) and its tables data will be lost and recreated.")

self.parser.add_option("-a", "--auto", action="store", type="string", dest="serversetup", default="",
help="Path to an XML file describing an automated unattended cloud setup")
Expand Down
Loading