public DTOVersion GetDatabaseVersion() { // Version object to return DTOVersion objVersion = new DTOVersion(); var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(GetConnectionString()); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { try { // There is actually a connection string // Test it by trying to read the Version table var result = (from version in context.AdefHelpDeskVersion orderby version.VersionNumber descending select version).FirstOrDefault(); // We have to find at least one Version record if (result != null) { // Set Version number objVersion.VersionNumber = result.VersionNumber; } } catch { objVersion.VersionNumber = "00.00.00"; } } return(objVersion); }
public DTOVersion CurrentVersion() { // Version object to return DTOVersion objVersion = GetDatabaseVersion(NewDatabaseVersion, GetConnectionString()); objVersion.isNewDatabase = (objVersion.VersionNumber == NewDatabaseVersion); objVersion.isUpToDate = (objVersion.VersionNumber == TargetDatabaseVersion); // Return the result return(objVersion); }
private DTOVersion ReadManifest(DTOVersion objVersion) { string strManifest; string strFilePath = _UpgradeProcessDirectory + $@"\Manifest.json"; using (StreamReader reader = new StreamReader(strFilePath)) { strManifest = reader.ReadToEnd(); reader.Close(); } dynamic objManifest = JsonConvert.DeserializeObject(strManifest); objVersion.ManifestHighestVersion = objManifest.ManifestHighestVersion; objVersion.ManifestLowestVersion = objManifest.ManifestLowestVersion; objVersion.ManifestSuccess = objManifest.ManifestSuccess; objVersion.ManifestFailure = objManifest.ManifestFailure; return(objVersion); }
public static DTOStatus RunUpdateScripts(string _NewDatabaseVersion, IWebHostEnvironment _hostEnvironment, string ConnectionString) { DTOStatus objDTOStatus = new DTOStatus(); objDTOStatus.Success = true; // Get the update scripts Dictionary <int, string> ColScripts = UpdateScripts(); foreach (var sqlScript in ColScripts) { try { // Run the script DTOVersion objVersion = GetDatabaseVersion(_NewDatabaseVersion, ConnectionString); int intCurrentDatabaseVersion = ConvertVersionToInteger(objVersion.VersionNumber); // Only run the script if it is higher than the // current database version if (sqlScript.Key > intCurrentDatabaseVersion) { var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(ConnectionString); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { context.Database.ExecuteSqlCommand(GetSQLScript(sqlScript.Value, _hostEnvironment)); } } } catch (Exception ex) { objDTOStatus.StatusMessage = ex.Message; objDTOStatus.Success = false; return(objDTOStatus); } } return(objDTOStatus); }
// Helpers #region public static DTOVersion GetDatabaseVersion(string NewDatabaseVersion, string ConnectionString) public static DTOVersion GetDatabaseVersion(string NewDatabaseVersion, string ConnectionString) { // Version object to return DTOVersion objVersion = new DTOVersion(); // If Version returned is NewDatabaseVersion // we will assume the Version table does not exist objVersion.VersionNumber = NewDatabaseVersion; var optionsBuilder = new DbContextOptionsBuilder <ADefHelpDeskContext>(); optionsBuilder.UseSqlServer(ConnectionString); using (var context = new ADefHelpDeskContext(optionsBuilder.Options)) { try { // There is actually a connection string // Test it by trying to read the Version table var result = (from version in context.AdefHelpDeskVersion orderby version.VersionNumber descending select version).FirstOrDefault(); // We have to find at least one Version record if (result != null) { // Set Version number objVersion.VersionNumber = result.VersionNumber; } } catch { // Do nothing if we cannot connect // the method will return NewDatabaseVersion } } return(objVersion); }
public IActionResult Index() { // Must be a Super User to proceed if (!UtilitySecurity.IsSuperUser(this.User.Identity.Name, GetConnectionString())) { return(Ok()); } string FileNameAndPath = _SystemFiles + $@"\UpgradePackage.zip"; string WebConfigOrginalFileNameAndPath = _hostEnvironment.ContentRootPath + @"\Web.config"; string WebConfigTempFileNameAndPath = _hostEnvironment.ContentRootPath + @"\Web.config.txt"; try { // Delete file if it exists if (System.IO.File.Exists(FileNameAndPath)) { System.IO.File.Delete(FileNameAndPath); } // Delete UpgradeProcess directory DirectoryInfo UpgradeDirectory = new DirectoryInfo(_UpgradeProcessDirectory); if (System.IO.Directory.Exists(_UpgradeProcessDirectory)) { UpgradeDirectory.Delete(true); } // Save file FormValueProvider formModel; using (var stream = System.IO.File.Create(FileNameAndPath)) { formModel = Request.StreamFile(stream).Result; } // Unzip files to ProcessDirectory ZipFile.ExtractToDirectory(FileNameAndPath, _UpgradeProcessDirectory); // *** Check upgrade // Get current version DTOVersion objVersion = GetDatabaseVersion(); // Examine the manifest file objVersion = ReadManifest(objVersion); try { if (objVersion.ManifestLowestVersion == "") { // Delete the files if (System.IO.Directory.Exists(_UpgradeProcessDirectory)) { UpgradeDirectory.Delete(true); } return(Ok("Error: Cound not find manifest")); } } catch (Exception ex) { // Delete the files if (System.IO.Directory.Exists(_UpgradeProcessDirectory)) { UpgradeDirectory.Delete(true); } return(Ok(ex.ToString())); } // Show error if needed and delete upgrade files if ( (ConvertToInteger(objVersion.VersionNumber) > ConvertToInteger(objVersion.ManifestHighestVersion)) || (ConvertToInteger(objVersion.VersionNumber) < ConvertToInteger(objVersion.ManifestLowestVersion)) ) { // Delete the files if (System.IO.Directory.Exists(_UpgradeProcessDirectory)) { UpgradeDirectory.Delete(true); } // Return the error response return(Ok(objVersion.ManifestFailure)); } // Temporarily rename the web.config file // to release the locks on any assemblies System.IO.File.Copy(WebConfigOrginalFileNameAndPath, WebConfigTempFileNameAndPath); System.IO.File.Delete(WebConfigOrginalFileNameAndPath); // Give the site time to release locks on the assemblies Task.Delay(2000).Wait(); // Wait 2 seconds with blocking // Rename the temp web.config file back to web.config // so the site will be active again System.IO.File.Copy(WebConfigTempFileNameAndPath, WebConfigOrginalFileNameAndPath); System.IO.File.Delete(WebConfigTempFileNameAndPath); return(Ok(objVersion.ManifestSuccess)); } catch (Exception ex1) { try { // Rename the temp web.config file back to web.config // so the site will be active again System.IO.File.Copy(WebConfigTempFileNameAndPath, WebConfigOrginalFileNameAndPath); System.IO.File.Delete(WebConfigTempFileNameAndPath); } catch (Exception ex2) { return(Ok(ex2.Message)); } return(Ok(ex1.Message)); } }