/// <summary> /// Notification that the application is about to shutdown. Terminate all our child /// processes. /// </summary> /// <param name="sender">The object that is sending this event.</param> /// <param name="e">The arguments that describe the event.</param> private void Dispatcher_ShutdownStarted(object sender, EventArgs e) { // // If we have an IIS Express process, then kill it. // if (iisExpressProcess != null) { iisExpressProcess.Kill(); iisExpressProcess = null; } // // If we have a LocalDb instance, then kill it off. // if (localDb != null) { if (localDb.IsRunning) { localDb.Stop(); } localDb = null; } UpdateState(); }
private static IEnumerable <string> GetPhysicalFilesForServer(SqlLocalDbInstance existingInstance) { if (!existingInstance.IsRunning) { existingInstance.Start(); } List <string> filePaths = new List <string>(); using (var connection = existingInstance.CreateConnection()) using (var command = connection.CreateCommand()) { connection.Open(); command.CommandText = "SELECT physical_name AS physicalPath FROM sys.master_files"; using (var reader = command.ExecuteReader()) { while (reader.Read()) { filePaths.Add(reader["physicalPath"].ToString()); } } } return(filePaths); }
private ISqlLocalDbInstance GetAppropriateLocalDbInstance( SqlLocalDbApiWrapper localDbApi, string localDbInstanceName, IEnumerable <string> allowedLocalDbVersions) { SqlLocalDbProvider localDbProvider = new SqlLocalDbProvider(); SqlLocalDbInstance existingInstance = GetExistingInstance(localDbProvider, localDbInstanceName); if (existingInstance != null) { return(existingInstance); } // No versions configured so just create an instance without specifying a version. // This will create an instance using the latest version. if (!allowedLocalDbVersions.Any()) { SqlLocalDbInstance newInstance = localDbProvider.CreateInstance(localDbInstanceName); return(newInstance); } // Order the version numbers so we try highest version to lowest version. IEnumerable <string> orderedVersionNumbers = allowedLocalDbVersions.OrderByDescending(version => version); IEnumerable <string> installedVersions = localDbApi.Versions; foreach (string versionNumber in orderedVersionNumbers) { if (installedVersions.Contains(versionNumber)) { localDbProvider.Version = versionNumber; SqlLocalDbInstance newInstance = localDbProvider.CreateInstance(localDbInstanceName); return(newInstance); } } string errorMessage = $@"DbTestMonkey was unable to find an appropriate LocalDb version to use. The following versions were configured to be considered in the app.config by you: {string.Join(",", allowedLocalDbVersions.Select(s => "\"" + s + "\""))} However only the following LocalDb versions were found to be installed: {string.Join(",", installedVersions.Select(s => "\"" + s + "\""))} Please correct this error by one of the following options: * Add an installed version of LocalDb into your projects app.config <allowedLocalDbVersions> element. You can find the installed versions by running ""sqllocaldb versions"" at the command line. * Remove the <allowedLocalDbVersions> element from your app.config which means DbTestMonkey will use the latest installed version of LocalDb on your machine. * Install a version of LocalDb that is configured in your app.config <allowedLocalDbVersions> element."; throw new InvalidOperationException(errorMessage); }
private SqlLocalDbInstance GetExistingInstance(SqlLocalDbProvider localDbProvider, string localDbInstanceName) { try { SqlLocalDbInstance existingInstance = localDbProvider.GetInstance(localDbInstanceName); IEnumerable <string> filePaths = GetPhysicalFilesForServer(existingInstance); bool isMissingFiles = filePaths.Any(path => !File.Exists(path)); // If at least one file doesn't exist, delete them all. // We have found that the SqlLocalDbInstanceInfo.Exists property to not be reliable. if (isMissingFiles) { LogAction("Existing LocalDb instance with name " + localDbInstanceName + " was found but some physical files were missing"); LogAction("Deleting instance and will attempt to recreate"); existingInstance.Stop(); SqlLocalDbApi.DeleteInstance(existingInstance.Name, deleteFiles: true); foreach (string filePath in filePaths) { try { File.Delete(filePath); } catch (DirectoryNotFoundException) { } } return(null); } else { LogAction("Found existing LocalDb instance with name " + localDbInstanceName + " and will use it"); LogAction("If you would like to delete this existing instance and let DbTestMonkey create a new instance run the following commands at a command line"); LogAction(" sqllocaldb stop " + localDbInstanceName); LogAction(" sqllocaldb delete " + localDbInstanceName); return(existingInstance); } } catch (InvalidOperationException) { LogAction("Existing LocalDb instance with name " + localDbInstanceName + " was not found"); } return(null); }
/// <summary> /// Load all github tags in the background. /// </summary> private void LoadData() { // // Initialize the LocalDb service only once. // if (localDb == null) { var provider = new SqlLocalDbProvider(); SqlLocalDbInstance instance; try { // // If we find an existing instance then shut it down and delete it. // instance = provider.GetInstance("RockLauncher"); if (instance.IsRunning) { instance.Stop(); } SqlLocalDbInstance.Delete(instance); } finally { // // Create a new instance and keep a reference to it. // localDb = provider.CreateInstance("RockLauncher"); localDb.Start(); } } // // Load all the instances from the file system. // var instances = Directory.GetDirectories(Support.GetInstancesPath()) .Select(d => Path.GetFileName(d)).ToList(); // // Convert pre-1.0 instance folders to 1.0 instance folders, which contain a // RockWeb for the current instance data. // foreach (string instance in instances) { string instancePath = Path.Combine(Support.GetInstancesPath(), instance); string rockwebPath = Path.Combine(instancePath, "RockWeb"); if (!Directory.Exists(rockwebPath)) { Directory.CreateDirectory(rockwebPath); foreach (var d in Directory.GetDirectories(instancePath)) { if (!Path.GetFileName(d).Equals("RockWeb", StringComparison.CurrentCultureIgnoreCase)) { Directory.Move(d, Path.Combine(rockwebPath, Path.GetFileName(d))); } } foreach (var f in Directory.GetFiles(instancePath)) { Directory.Move(f, Path.Combine(rockwebPath, Path.GetFileName(f))); } } } // // Update the UI with the new list of instances. // Dispatcher.Invoke(() => { cbInstances.ItemsSource = instances; if (instances.Count > 0) { cbInstances.SelectedIndex = 0; } txtStatus.Text = "Idle"; UpdateState(); }); }