From 5d5a70be9d97e18f80dc44ecfbba7d1dfbabb5d7 Mon Sep 17 00:00:00 2001 From: nvazquez Date: Thu, 17 Jul 2025 14:03:35 -0300 Subject: [PATCH] [DB] Add force recreate parameter to cloudstack-setup-databases script --- setup/bindir/cloud-setup-databases.in | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/setup/bindir/cloud-setup-databases.in b/setup/bindir/cloud-setup-databases.in index e2d5b50fe6b2..8c453edda447 100755 --- a/setup/bindir/cloud-setup-databases.in +++ b/setup/bindir/cloud-setup-databases.in @@ -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: @@ -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 @@ -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"] @@ -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")