Example #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));
        }
Example #2
0
        public void Test_CompareTo_equal(string dbVersionStr1, string dbVersionStr2)
        {
            DbVersion dbVersion1 = DbVersion.FromString(dbVersionStr1);
            DbVersion dbVersion2 = DbVersion.FromString(dbVersionStr2);

            Assert.AreEqual(0, dbVersion1.CompareTo(dbVersion2));
            Assert.AreEqual(0, dbVersion2.CompareTo(dbVersion1));
        }
Example #3
0
        public void Test_CompareTo_first_smaller(string dbVersionStr1, string dbVersionStr2)
        {
            DbVersion dbVersion1 = DbVersion.FromString(dbVersionStr1);
            DbVersion dbVersion2 = DbVersion.FromString(dbVersionStr2);

            Assert.AreEqual(-1, dbVersion1.CompareTo(dbVersion2));
            Assert.AreEqual(1, dbVersion2.CompareTo(dbVersion1));
        }
Example #4
0
        public void DoExecute_fails_on_nonversioned_script()
        {
            // arrange
            IEnumerable <DbScriptToRun> nonVersionedScript = new List <DbScriptToRun>()
            {
                new DbScriptToRun(DbVersion.FromString("1.0"), "Core/TestData/NonVersionedScript/01.NonVersionedScript.sql")
            };

            _deploymentStep = new RunDbScriptsDeploymentStep(_dbScriptRunnerFake.Object, _DatabaseServerName, nonVersionedScript);

            // act, assert
            Assert.Throws <DeploymentTaskException>(() => _deploymentStep.PrepareAndExecute());
        }
Example #5
0
        public void DoExecute_fails_on_not_existing_script()
        {
            // arrange
            IEnumerable <DbScriptToRun> notExistingScripts = new List <DbScriptToRun>()
            {
                new DbScriptToRun(DbVersion.FromString("1.0"), "someScript.sql")
            };

            _deploymentStep = new RunDbScriptsDeploymentStep(_dbScriptRunnerFake.Object, _DatabaseServerName, notExistingScripts);

            // act, assert
            Assert.Throws <FileNotFoundException>(() => _deploymentStep.PrepareAndExecute());
        }
Example #6
0
        public void DoExecute_does_not_fail_on_nonversioned_script_when_is_marked_as_non_transactional()
        {
            // arrange
            IEnumerable <DbScriptToRun> nonTransactionalScript = new List <DbScriptToRun>()
            {
                new DbScriptToRun(DbVersion.FromString("1.0"), "Core/TestData/NonVersionedScript/02.NonVersionedScript.notrans.sql")
            };

            _deploymentStep = new RunDbScriptsDeploymentStep(_dbScriptRunnerFake.Object, _DatabaseServerName, nonTransactionalScript);

            // act
            _deploymentStep.PrepareAndExecute();

            // assert
            _dbScriptRunnerFake.Verify(x => x.Execute(It.IsAny <string>()));
        }
Example #7
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));
        }