@@ -129,6 +129,12 @@ class DBDeployer(object):
129
129
(value , index ) = self .dbDotProperties [key ]
130
130
return value
131
131
132
+ def areCloudDatabasesCreated (self ):
133
+ cmd = "SELECT CASE WHEN COUNT(DISTINCT SCHEMA_NAME) >= 1 THEN 1 ELSE 0 END AS schema_exists \
134
+ FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME IN ('cloud', 'cloud_usage');"
135
+ databases = self .runMysql (cmd , "databases" , self .rootuser != None )
136
+ return databases .replace ("schema_exists" , "" ).strip () == "1"
137
+
132
138
def runMysql (self , text , table , isRoot = False ):
133
139
kwargs = {}
134
140
if not isRoot :
@@ -151,7 +157,7 @@ class DBDeployer(object):
151
157
open (self .tmpMysqlFile , 'w' ).write (text )
152
158
mysqlCmds .append ('<' )
153
159
mysqlCmds .append (self .tmpMysqlFile )
154
- runCmd (mysqlCmds )
160
+ return runCmd (mysqlCmds )
155
161
156
162
except Exception as e :
157
163
err = '''Encountering an error when executing mysql script
@@ -233,6 +239,10 @@ for full help
233
239
("DROP USER 'cloud'@'%' ;" , "DO NULL;" )
234
240
)
235
241
242
+ if self .areCloudDatabasesCreated () and not self .options .forcerecreate :
243
+ self .errorAndExit ("Aborting script as the databases (cloud, cloud_usage) already exist.\n " \
244
+ "Please use the --force-recreate parameter if you want to recreate the schemas." )
245
+
236
246
scriptsToRun = ["create-database" ,"create-schema" , "create-database-premium" ,"create-schema-premium" ]
237
247
if self .options .schemaonly :
238
248
scriptsToRun = ["create-schema" , "create-schema-premium" ]
@@ -610,6 +620,9 @@ for example:
610
620
help = "Creates the db schema without having to pass root credentials - " \
611
621
"Please note: The databases (cloud, cloud_usage) and user (cloud) has to be configured " \
612
622
"manually prior to running this script when using this flag." )
623
+ self .parser .add_option ("--force-recreate" , action = "store_true" , dest = "forcerecreate" , default = False ,
624
+ help = "Force recreation of the existing DB schemas. This option is disabled by default." \
625
+ "Please note: The databases (cloud, cloud_usage) and its tables data will be lost and recreated." )
613
626
614
627
self .parser .add_option ("-a" , "--auto" , action = "store" , type = "string" , dest = "serversetup" , default = "" ,
615
628
help = "Path to an XML file describing an automated unattended cloud setup" )
0 commit comments