@@ -130,6 +130,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
130130 protected static Logger LOGGER = LogManager .getLogger (DatabaseUpgradeChecker .class );
131131 private final DatabaseVersionHierarchy hierarchy ;
132132 private static final String VIEWS_DIRECTORY = Paths .get ("META-INF" , "db" , "views" ).toString ();
133+ private static final String PROCEDURES_DIRECTORY = Paths .get ("META-INF" , "db" , "procedures" ).toString ();
133134
134135 @ Inject
135136 VersionDao _dao ;
@@ -298,83 +299,120 @@ private void updateSystemVmTemplates(DbUpgrade[] upgrades) {
298299 }
299300
300301 protected void upgrade (CloudStackVersion dbVersion , CloudStackVersion currentVersion ) {
302+ executeProcedureScripts ();
303+ final DbUpgrade [] upgrades = executeUpgrades (dbVersion , currentVersion );
304+
305+ executeViewScripts ();
306+ updateSystemVmTemplates (upgrades );
307+ }
308+
309+ protected void executeProcedureScripts () {
310+ LOGGER .info (String .format ("Executing Stored Procedure scripts that are under resource directory [%s]." , PROCEDURES_DIRECTORY ));
311+ List <String > filesPathUnderViewsDirectory = FileUtil .getFilesPathsUnderResourceDirectory (PROCEDURES_DIRECTORY );
312+
313+ try (TransactionLegacy txn = TransactionLegacy .open ("execute-procedure-scripts" )) {
314+ Connection conn = txn .getConnection ();
315+
316+ for (String filePath : filesPathUnderViewsDirectory ) {
317+ LOGGER .debug (String .format ("Executing PROCEDURE script [%s]." , filePath ));
318+
319+ InputStream viewScript = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (filePath );
320+ runScript (conn , viewScript );
321+ }
322+
323+ LOGGER .info (String .format ("Finished execution of PROCEDURE scripts that are under resource directory [%s]." , PROCEDURES_DIRECTORY ));
324+ } catch (SQLException e ) {
325+ String message = String .format ("Unable to execute PROCEDURE scripts due to [%s]." , e .getMessage ());
326+ LOGGER .error (message , e );
327+ throw new CloudRuntimeException (message , e );
328+ }
329+ }
330+
331+ private DbUpgrade [] executeUpgrades (CloudStackVersion dbVersion , CloudStackVersion currentVersion ) {
301332 LOGGER .info ("Database upgrade must be performed from " + dbVersion + " to " + currentVersion );
302333
303334 final DbUpgrade [] upgrades = calculateUpgradePath (dbVersion , currentVersion );
304335
305336 for (DbUpgrade upgrade : upgrades ) {
306- VersionVO version ;
307- LOGGER .debug ("Running upgrade " + upgrade .getClass ().getSimpleName () + " to upgrade from " + upgrade .getUpgradableVersionRange ()[0 ] + "-" + upgrade
308- .getUpgradableVersionRange ()[1 ] + " to " + upgrade .getUpgradedVersion ());
309- TransactionLegacy txn = TransactionLegacy .open ("Upgrade" );
310- txn .start ();
311- try {
312- Connection conn ;
313- try {
314- conn = txn .getConnection ();
315- } catch (SQLException e ) {
316- String errorMessage = "Unable to upgrade the database" ;
317- LOGGER .error (errorMessage , e );
318- throw new CloudRuntimeException (errorMessage , e );
319- }
320- InputStream [] scripts = upgrade .getPrepareScripts ();
321- if (scripts != null ) {
322- for (InputStream script : scripts ) {
323- runScript (conn , script );
324- }
325- }
326-
327- upgrade .performDataMigration (conn );
328-
329- version = new VersionVO (upgrade .getUpgradedVersion ());
330- version = _dao .persist (version );
337+ VersionVO version = executeUpgrade (upgrade );
338+ executeUpgradeCleanup (upgrade , version );
339+ }
340+ return upgrades ;
341+ }
331342
332- txn .commit ();
333- } catch (CloudRuntimeException e ) {
343+ private VersionVO executeUpgrade (DbUpgrade upgrade ) {
344+ VersionVO version ;
345+ LOGGER .debug ("Running upgrade " + upgrade .getClass ().getSimpleName () + " to upgrade from " + upgrade .getUpgradableVersionRange ()[0 ] + "-" + upgrade
346+ .getUpgradableVersionRange ()[1 ] + " to " + upgrade .getUpgradedVersion ());
347+ TransactionLegacy txn = TransactionLegacy .open ("Upgrade" );
348+ txn .start ();
349+ try {
350+ Connection conn ;
351+ try {
352+ conn = txn .getConnection ();
353+ } catch (SQLException e ) {
334354 String errorMessage = "Unable to upgrade the database" ;
335355 LOGGER .error (errorMessage , e );
336356 throw new CloudRuntimeException (errorMessage , e );
337- } finally {
338- txn .close ();
357+ }
358+ InputStream [] scripts = upgrade .getPrepareScripts ();
359+ if (scripts != null ) {
360+ for (InputStream script : scripts ) {
361+ runScript (conn , script );
362+ }
339363 }
340364
341- // Run the corresponding '-cleanup.sql' script
342- txn = TransactionLegacy .open ("Cleanup" );
343- try {
344- LOGGER .info ("Cleanup upgrade " + upgrade .getClass ().getSimpleName () + " to upgrade from " + upgrade .getUpgradableVersionRange ()[0 ] + "-" + upgrade
345- .getUpgradableVersionRange ()[1 ] + " to " + upgrade .getUpgradedVersion ());
365+ upgrade .performDataMigration (conn );
346366
347- txn .start ();
348- Connection conn ;
349- try {
350- conn = txn .getConnection ();
351- } catch (SQLException e ) {
352- LOGGER .error ("Unable to cleanup the database" , e );
353- throw new CloudRuntimeException ("Unable to cleanup the database" , e );
354- }
367+ version = new VersionVO (upgrade .getUpgradedVersion ());
368+ version = _dao .persist (version );
355369
356- InputStream [] scripts = upgrade .getCleanupScripts ();
357- if (scripts != null ) {
358- for (InputStream script : scripts ) {
359- runScript (conn , script );
360- LOGGER .debug ("Cleanup script " + upgrade .getClass ().getSimpleName () + " is executed successfully" );
361- }
362- }
363- txn .commit ();
370+ txn .commit ();
371+ } catch (CloudRuntimeException e ) {
372+ String errorMessage = "Unable to upgrade the database" ;
373+ LOGGER .error (errorMessage , e );
374+ throw new CloudRuntimeException (errorMessage , e );
375+ } finally {
376+ txn .close ();
377+ }
378+ return version ;
379+ }
364380
365- txn .start ();
366- version .setStep (Step .Complete );
367- version .setUpdated (new Date ());
368- _dao .update (version .getId (), version );
369- txn .commit ();
370- LOGGER .debug ("Upgrade completed for version " + version .getVersion ());
371- } finally {
372- txn .close ();
381+ private void executeUpgradeCleanup (DbUpgrade upgrade , VersionVO version ) {
382+ TransactionLegacy txn ;
383+ // Run the corresponding '-cleanup.sql' script
384+ txn = TransactionLegacy .open ("Cleanup" );
385+ try {
386+ LOGGER .info ("Cleanup upgrade " + upgrade .getClass ().getSimpleName () + " to upgrade from " + upgrade .getUpgradableVersionRange ()[0 ] + "-" + upgrade
387+ .getUpgradableVersionRange ()[1 ] + " to " + upgrade .getUpgradedVersion ());
388+
389+ txn .start ();
390+ Connection conn ;
391+ try {
392+ conn = txn .getConnection ();
393+ } catch (SQLException e ) {
394+ LOGGER .error ("Unable to cleanup the database" , e );
395+ throw new CloudRuntimeException ("Unable to cleanup the database" , e );
373396 }
374- }
375397
376- executeViewScripts ();
377- updateSystemVmTemplates (upgrades );
398+ InputStream [] scripts = upgrade .getCleanupScripts ();
399+ if (scripts != null ) {
400+ for (InputStream script : scripts ) {
401+ runScript (conn , script );
402+ LOGGER .debug ("Cleanup script " + upgrade .getClass ().getSimpleName () + " is executed successfully" );
403+ }
404+ }
405+ txn .commit ();
406+
407+ txn .start ();
408+ version .setStep (Step .Complete );
409+ version .setUpdated (new Date ());
410+ _dao .update (version .getId (), version );
411+ txn .commit ();
412+ LOGGER .debug ("Upgrade completed for version " + version .getVersion ());
413+ } finally {
414+ txn .close ();
415+ }
378416 }
379417
380418 protected void executeViewScripts () {
0 commit comments