public void ApplyVariablesFailsOnUnknownVariable()
        {
            var variables = new Mock <IVariables>(MockBehavior.Strict);

            variables
            .Setup(v => v.GetValue(It.IsAny <string>()))
            .Returns((string)null);

            var ex = Assert.Throws <InvalidOperationException>(() => TextScript.ApplyVariables("{{Value_1}}", variables.Object));

            StringAssert.Contains("Value_1", ex.Message);
        }
        public void ExecuteShowVariableReplacement()
        {
            var sut = new TextScript
            {
                ReadSqlContent = () => new MemoryStream(Encoding.Default.GetBytes("{{var1}} {{var1}}"))
            };

            _variables.SetValue(VariableSource.CommandLine, "var1", "[some value]");

            sut.Execute(_command.Object, _variables, _logger.Object);

            _command.VerifyAll();
            Assert.AreEqual(1, _executedScripts.Count);
            Assert.AreEqual("[some value] [some value]", _executedScripts[0]);

            Assert.IsNotNull(_logOutput.Where(i => i.Contains("var1") && i.Contains("[some value]")));
        }
        public void ApplyVariables(string sql, string value, string expected)
        {
            var variables = new Mock <IVariables>(MockBehavior.Strict);

            variables
            .Setup(v => v.GetValue(It.IsAny <string>()))
            .Returns <string>(name =>
            {
                if ("value".Equals(name, StringComparison.OrdinalIgnoreCase))
                {
                    return(value);
                }

                return(null);
            });

            var actual = TextScript.ApplyVariables(sql, variables.Object);

            Assert.AreEqual(expected, actual);
        }
        public void Execute()
        {
            var sut = new TextScript
            {
                ReadSqlContent = () => new MemoryStream(Encoding.Default.GetBytes(@"
{{var1}}
go
text2
go"))
            };

            _variables.SetValue(VariableSource.CommandLine, "var1", "text1");

            sut.Execute(_command.Object, _variables, _logger.Object);

            _command.VerifyAll();

            Assert.AreEqual(2, _executedScripts.Count);
            Assert.AreEqual("text1", _executedScripts[0]);
            Assert.AreEqual("text2", _executedScripts[1]);
        }
Beispiel #5
0
        public void Execute(IScript script, Version currentVersion, Version targetVersion)
        {
            Variables.CurrentVersion = currentVersion.ToString();
            Variables.TargetVersion  = targetVersion.ToString();
            Variables.DatabaseName   = new SqlConnectionStringBuilder(ConnectionString).InitialCatalog;

            var updateVersion = TextScript.ApplyVariables(
                Configuration.SetCurrentVersionScript,
                Variables);

            using (var connection = new SqlConnection(ConnectionString))
                using (var command = connection.CreateCommand())
                {
                    connection.InfoMessage += OnConnectionInfoMessage;

                    command.CommandTimeout = 0;
                    connection.Open();

                    using (var transaction = Transaction == TransactionMode.PerStep ? connection.BeginTransaction(IsolationLevel.ReadCommitted) : null)
                    {
                        command.Transaction = transaction;

                        script.Execute(command, Variables, Log);

                        if (!Variables.DatabaseName.Equals(connection.Database, StringComparison.OrdinalIgnoreCase))
                        {
                            command.CommandText = "USE [{0}]".FormatWith(Variables.DatabaseName);
                            command.ExecuteNonQuery();
                        }

                        command.CommandText = updateVersion;
                        command.ExecuteNonQuery();

                        transaction?.Commit();
                    }
                }
        }