Ejemplo n.º 1
0
        private ProjectMetadata GetDbProjectMetadata(DbProjectInfo dbProjectInfo, EnvironmentInfo environmentInfo)
        {
            var projectVersions = new List <MachineSpecificProjectVersion>();

            DbProjectConfiguration dbProjectConfiguration =
                environmentInfo.GetDbProjectConfiguration(dbProjectInfo);

            DatabaseServer databaseServer =
                environmentInfo.GetDatabaseServer(dbProjectConfiguration.DatabaseServerId);

            var dbVersions =
                _dbVersionProvider.GetVersions(
                    dbProjectInfo.DbName,
                    databaseServer.MachineName);

            DbVersion latestDbVersion =
                dbVersions
                .Select(s => DbVersion.FromString(s.Version))
                .OrderByDescending(v => v)
                .FirstOrDefault();

            if (latestDbVersion != null)
            {
                projectVersions.Add(new MachineSpecificProjectVersion(databaseServer.MachineName, latestDbVersion.ToString()));
            }

            return(new ProjectMetadata(dbProjectInfo.Name, environmentInfo.Name, projectVersions));
        }
Ejemplo n.º 2
0
        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));
        }