private bool RunUpgradeScripts( Guid applicationId, string applicationName, string pathToScriptFolder, Version versionToStopAt) { bool result = true; if (!Directory.Exists(pathToScriptFolder)) { WritePageContent( String.Format("{0} {1}", pathToScriptFolder, SetupResource.ScriptFolderNotFoundMessage), false); return(false); } DirectoryInfo directoryInfo = new DirectoryInfo(pathToScriptFolder); FileInfo[] scriptFiles = directoryInfo.GetFiles("*.config"); Array.Sort(scriptFiles, UIHelper.CompareFileNames); if (scriptFiles.Length == 0) { return(false); } Version currentSchemaVersion = DatabaseHelper.GetSchemaVersion(applicationId); foreach (FileInfo scriptFile in scriptFiles) { Version scriptVersion = DatabaseHelper.ParseVersionFromFileName(scriptFile.Name); if ( (scriptVersion != null) && (scriptVersion > currentSchemaVersion) && (versionToStopAt == null || (scriptVersion <= versionToStopAt)) // commented out 2007-08-26 // script is still logged if it fails but version // isn't updated. This was blocking the script from // running again unless user deleted row from cy_SchemaScriptHistory //&& (!DatabaseHelper.SchemaScriptHasBeenRun( // applicationID, // scriptFile.Name) // ) ) { string message = string.Format( SetupResource.RunningScriptMessage, applicationName, scriptFile.Name.Replace(".config", string.Empty)); WritePageContent( message, true); string errorMessage = DatabaseHelper.RunScript( applicationId, scriptFile, null); if (errorMessage.Length > 0) { WritePageContent(errorMessage, true); return(false); } if (string.Equals(applicationName, "Cynthia-core", StringComparison.InvariantCultureIgnoreCase)) { CSetup.DoPostScriptTasks(scriptVersion, null); } Version newVersion = DatabaseHelper.ParseVersionFromFileName(scriptFile.Name); if ( (applicationName != null) && (newVersion != null) ) { DatabaseHelper.UpdateSchemaVersion( applicationId, applicationName, newVersion.Major, newVersion.Minor, newVersion.Build, newVersion.Revision); DatabaseHelper.AddSchemaScriptHistory( applicationId, scriptFile.Name, DateTime.UtcNow, false, string.Empty, string.Empty); if (errorMessage.Length == 0) { currentSchemaVersion = newVersion; } } } } return(result); }
private bool RunSetupScript( Guid applicationId, string applicationName, string pathToScriptFolder, Version versionToStopAt) { bool result = true; if (!Directory.Exists(pathToScriptFolder)) { WritePageContent( pathToScriptFolder + " " + SetupResource.ScriptFolderNotFoundMessage, false); return(false); } DirectoryInfo directoryInfo = new DirectoryInfo(pathToScriptFolder); FileInfo[] scriptFiles = directoryInfo.GetFiles("*.config"); Array.Sort(scriptFiles, UIHelper.CompareFileNames); if (scriptFiles.Length == 0) { WritePageContent( SetupResource.NoScriptsFilesFoundMessage + " " + pathToScriptFolder, false); return(false); } // We only want to run the highest version script from the /SchemaInstallationScripts/dbplatform folder // normally there is only 1 script in this folder, but if someone upgrades and then starts with a clean db // there can be more than one script because of the previous installs so we nned to make sure we only run the highest version found // since we sorted it the highest version is the last item in the array FileInfo scriptFile = scriptFiles[(scriptFiles.Length - 1)]; Version currentSchemaVersion = DatabaseHelper.GetSchemaVersion(applicationId); Version scriptVersion = DatabaseHelper.ParseVersionFromFileName(scriptFile.Name); if ( (scriptVersion != null) && (scriptVersion > currentSchemaVersion) && (versionToStopAt == null || (scriptVersion <= versionToStopAt)) ) { string message = string.Format( SetupResource.RunningScriptMessage, applicationName, scriptFile.Name.Replace(".config", string.Empty)); WritePageContent( message, true); string errorMessage = DatabaseHelper.RunScript( applicationId, scriptFile, null); if (errorMessage.Length > 0) { WritePageContent(errorMessage, true); return(false); } if (string.Equals(applicationName, "Cynthia-core", StringComparison.InvariantCultureIgnoreCase)) { CSetup.DoPostScriptTasks(scriptVersion, null); } Version newVersion = DatabaseHelper.ParseVersionFromFileName(scriptFile.Name); if ( (applicationName != null) && (newVersion != null) ) { DatabaseHelper.UpdateSchemaVersion( applicationId, applicationName, newVersion.Major, newVersion.Minor, newVersion.Build, newVersion.Revision); DatabaseHelper.AddSchemaScriptHistory( applicationId, scriptFile.Name, DateTime.UtcNow, false, string.Empty, string.Empty); if (errorMessage.Length == 0) { currentSchemaVersion = newVersion; } } } return(result); }