public static void SetSelectedScriptsToRun(Guid deploymentId, DbScriptsToRunSelection dbScriptsToRunSelection) { lock (_collectedScriptsByDeploymentId) { _collectedScriptsByDeploymentId[deploymentId] = new DbScriptsToRunSelectionResult { DbScriptsToRunSelection = dbScriptsToRunSelection }; } }
public void SetSelectedDbScriptsToRun(Guid deploymentId, DbScriptsToRunSelection scriptsToRunSelection) { Exec(@as => @as.SetSelectedDbScriptsToRun(deploymentId, scriptsToRunSelection)); }
private IEnumerable <DbScriptToRun> GetScriptsToRun() { // get db versions var versions = _dbVersionProvider.GetVersions(_dbName, _sqlServerName); var dbVersionsModel = new DbVersionsModel(); dbVersionsModel.AddDatabase(_environmentName, _dbName, versions.SelectMany(s => s.GetRunnedVersions())); // sort db versions List <DbVersion> dbVersionsList = dbVersionsModel.GetAllSortedDbVersions(_dbName) .Select(DbVersion.FromString) .ToList(); DbVersion currentDbVersion = dbVersionsList.LastOrDefault(); var dbVersionsSet = new HashSet <DbVersion>(dbVersionsList); // collect scripts that weren't executed on database string[] scriptFilePaths = Directory.GetFiles( _scriptsDirectoryPathProvider.Value, "*.sql", SearchOption.AllDirectories); Dictionary <DbVersion, string> scriptsToRunDict = (from filePath in scriptFilePaths let dbVersion = DbVersion.FromString(Path.GetFileNameWithoutExtension(filePath)) where !dbVersionsSet.Contains(dbVersion) select new { dbVersion, filePath }) .ToDictionary(x => x.dbVersion, x => x.filePath); Dictionary <DbVersion, string> scriptsNewerThanCurrentVersion = scriptsToRunDict .Where(kvp => currentDbVersion == null || kvp.Key.IsGreatherThan(currentDbVersion)) .OrderBy(kvp => kvp.Key) .Select(x => x) .ToDictionary(x => x.Key, x => x.Value); IEnumerable <DbVersion> scriptsToRunOlderThanCurrentVersion = scriptsToRunDict.Keys.Except(scriptsNewerThanCurrentVersion.Keys) .OrderBy(v => v); foreach (DbVersion dbVersion in scriptsToRunOlderThanCurrentVersion) { if (!IsScriptSupported(dbVersion)) { continue; } PostDiagnosticMessage(string.Format("This script should be run but it's older than the current version so we won't run it: '{0}'.", dbVersion), DiagnosticMessageType.Warn); } RemoveNotSupportedScripts(scriptsNewerThanCurrentVersion); List <DbScriptToRun> scriptsToRun = scriptsNewerThanCurrentVersion .Select(x => new DbScriptToRun(x.Key, x.Value)) .ToList(); if (scriptsToRun.Any() == false) { return(scriptsToRun); } var scriptFileNames = scriptsToRun.Select(s => s.GetScriptFileName()).ToArray(); DbScriptsToRunSelection scriptsToRunSelection = _scriptsToRunSelector.GetSelectedScriptsToRun(_deploymentInfo.DeploymentId, scriptFileNames); if (scriptsToRunSelection.SelectedScripts == null || scriptsToRunSelection.SelectedScripts.Length == 0) { return(Enumerable.Empty <DbScriptToRun>()); } return(FilterScripts(scriptsToRun, scriptsToRunSelection)); }
private static IEnumerable <DbScriptToRun> FilterScripts(List <DbScriptToRun> scriptsToRun, DbScriptsToRunSelection scriptsToRunSelection) { switch (scriptsToRunSelection.DatabaseScriptToRunSelectionType) { case DatabaseScriptToRunSelectionType.LastVersion: var lastScriptToRun = scriptsToRunSelection.SelectedScripts.Last(); int lastScriptPosition = scriptsToRun.FindLastIndex(x => x.GetScriptFileName() == lastScriptToRun); if (lastScriptPosition < 0) { throw new Exception("Selected script does not exist on scripts to run list."); } return(scriptsToRun.Take(lastScriptPosition + 1)); case DatabaseScriptToRunSelectionType.Multiselect: return(scriptsToRun.Where(scriptToRun => scriptsToRunSelection.SelectedScripts.Any(selectedScript => selectedScript == scriptToRun.GetScriptFileName()))); default: throw new ArgumentOutOfRangeException(); } }
private static IEnumerable<DbScriptToRun> FilterScripts(List<DbScriptToRun> scriptsToRun, DbScriptsToRunSelection scriptsToRunSelection) { switch (scriptsToRunSelection.DatabaseScriptToRunSelectionType) { case DatabaseScriptToRunSelectionType.LastVersion: var lastScriptToRun = scriptsToRunSelection.SelectedScripts.Last(); int lastScriptPosition = scriptsToRun.FindLastIndex(x => x.GetScriptFileName() == lastScriptToRun); if (lastScriptPosition < 0) { throw new Exception("Selected script does not exist on scripts to run list."); } return scriptsToRun.Take(lastScriptPosition + 1); case DatabaseScriptToRunSelectionType.Multiselect: return scriptsToRun.Where(scriptToRun => scriptsToRunSelection.SelectedScripts.Any(selectedScript => selectedScript == scriptToRun.GetScriptFileName())); default: throw new ArgumentOutOfRangeException(); } }