예제 #1
0
        public void WithScriptsAndDowngradeScriptsEmbeddedInAssembly_RunsFromFolder_SuccessfullyStoresDowngradeScripts()
        {
            // Setup
            string folderName = "DowngradeScripts";

            Func <string, bool> scriptsFilter          = s => s.EndsWith(".sql", StringComparison.OrdinalIgnoreCase) && !s.Contains($".{folderName}.") && s.Contains("FolderUpAndDownScrips");
            Func <string, bool> downgradeScriptsFilter = s => s.Contains($".{folderName}.") && s.Contains("FolderUpAndDownScrips");

            DowngradeScriptsSettings settings = new DowngradeScriptsSettings(scriptsFilter, downgradeScriptsFilter,
                                                                             new KeyValuePair <DowngradeScriptsSettingsMode, string>(DowngradeScriptsSettingsMode.Folder, folderName));

            var upgradeEngineBuilder = DeployChanges.To
                                       .SqlDatabase(connectionString)
                                       .WithScriptsAndDowngradeScriptsEmbeddedInAssembly <SqlDowngradeEnabledTableJournal>(Assembly.GetExecutingAssembly(), settings)
                                       .LogToNowhere();

            // Act
            var result = upgradeEngineBuilder.BuildWithDowngrade(true).PerformUpgrade();

            //Assert
            Assert.AreEqual(true, result.Successful);

            Dictionary <string, string> executedScriptsAndDowngradeScripts = GetExecutedScriptsFromDatabase(connectionString);
            var upgradeScripts   = new EmbeddedScriptProvider(Assembly.GetExecutingAssembly(), settings.ScriptsFilter).GetScripts(null);
            var downgradeScripts = new EmbeddedScriptProvider(Assembly.GetExecutingAssembly(), settings.DowngradeScriptsFilter).GetScripts(null);

            Assert.AreEqual(executedScriptsAndDowngradeScripts.Count, upgradeScripts.Count());

            foreach (var storedDowngradeScript in executedScriptsAndDowngradeScripts.Values.Where(v => !string.IsNullOrEmpty(v)))
            {
                Assert.IsTrue(downgradeScripts.Any(script => script.Contents.Equals(storedDowngradeScript)));
            }
        }
예제 #2
0
        public void WithScriptsAndDowngradeScriptsEmbeddedInAssembly_RunsFromSuffix_SuccessfullyRevertIfOlderVersionIsDeployed()
        {
            // Setup
            string suffix = "_downgrade";

            Func <string, bool> scriptsFilter          = s => s.EndsWith(".sql", StringComparison.OrdinalIgnoreCase) && !s.EndsWith($"{suffix}.sql", StringComparison.OrdinalIgnoreCase) && s.Contains("SuffixUpAndDownScripts");
            Func <string, bool> downgradeScriptsFilter = s => s.EndsWith($"{suffix}.sql", StringComparison.OrdinalIgnoreCase) && s.Contains("SuffixUpAndDownScripts");

            DowngradeScriptsSettings settings = new DowngradeScriptsSettings(scriptsFilter, downgradeScriptsFilter,
                                                                             new KeyValuePair <DowngradeScriptsSettingsMode, string>(DowngradeScriptsSettingsMode.Suffix, suffix));

            var upgradeEngineBuilder = DeployChanges.To
                                       .SqlDatabase(connectionString)
                                       .WithScriptsAndDowngradeScriptsEmbeddedInAssembly <SqlDowngradeEnabledTableJournal>(Assembly.GetExecutingAssembly(), settings)
                                       .LogToNowhere();

            // Act
            var result = upgradeEngineBuilder.BuildWithDowngrade(true).PerformUpgrade();

            //Assert
            Assert.AreEqual(true, result.Successful);

            // Perform revert of latest script
            scriptsFilter = s => s.EndsWith(".sql", StringComparison.OrdinalIgnoreCase) && !s.EndsWith($"{suffix}.sql", StringComparison.OrdinalIgnoreCase) &&
                            s.Contains("SuffixUpAndDownScripts") && !s.Contains("Script0004 - Redirects");
            downgradeScriptsFilter = s => s.EndsWith($"{suffix}.sql", StringComparison.OrdinalIgnoreCase) && s.Contains("SuffixUpAndDownScripts");

            settings = new DowngradeScriptsSettings(scriptsFilter, downgradeScriptsFilter,
                                                    new KeyValuePair <DowngradeScriptsSettingsMode, string>(DowngradeScriptsSettingsMode.Suffix, suffix));

            upgradeEngineBuilder = DeployChanges.To
                                   .SqlDatabase(connectionString)
                                   .WithScriptsAndDowngradeScriptsEmbeddedInAssembly <SqlDowngradeEnabledTableJournal>(Assembly.GetExecutingAssembly(), settings)
                                   .LogToNowhere();

            // Act
            result = upgradeEngineBuilder.BuildWithDowngrade(true).PerformUpgrade();

            //Assert
            Assert.AreEqual(true, result.Successful);

            Dictionary <string, string> executedScriptsAndDowngradeScripts = GetExecutedScriptsFromDatabase(connectionString);
            var upgradeScripts = new EmbeddedScriptProvider(Assembly.GetExecutingAssembly(), settings.ScriptsFilter).GetScripts(null);

            Assert.AreEqual(executedScriptsAndDowngradeScripts.Count, upgradeScripts.Count());

            using (var connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand("select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'Redirect'", connection);

                try
                {
                    connection.Open();
                    var executeScalar = command.ExecuteScalar();
                    Assert.IsNull(executeScalar); // If null table no longer exists
                }
                catch
                {
                    throw;
                }
                finally
                {
                    connection.Close();
                }
            }
        }