Skip to content

Commit

Permalink
Allowed variables within quotes (thanks @ksumrall). Updated docs with…
Browse files Browse the repository at this point in the history
… more info on how variables work. Fixes #293.
  • Loading branch information
droyad committed Feb 25, 2018
1 parent 9665f86 commit f1f19af
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
11 changes: 10 additions & 1 deletion docs/more-info/variable-substitution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ DeployChanges.To
Then in your database script:

```
-- $TestVariable$ $AnotherVariable$
print '$TestVariable$'
SELECT * FROM dbo.$TestVariable
```

Will execute `print 'Value'`
Will execute:
```
-- Value $AnotherVariable$
print 'Value'
SELECT * FROM dbo.Value
```

Variables can only contain letters, digits, `_` and `-`. If there are any other characters between the `$`s it is not treated as a variable. If a variable is found within a script, but not supplied an exception will be thrown unless it is within a comment.

**Note:** there is no way to escape variables, if this causes you issues, create a GitHub issue or submit a pull request to allow escaping!
10 changes: 10 additions & 0 deletions src/dbup-core/Support/SqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ private void ReadQuotedString()
ReadCharacter(CharacterType.QuotedString, CurrentChar);
while (Read() != FailedRead)
{
if (IsCustomStatement)
{
ReadCustomStatement();
Read();
}
ReadCharacter(CharacterType.QuotedString, CurrentChar);
if (IsQuote)
{
Expand All @@ -374,6 +379,11 @@ private void ReadBracketedText()
ReadCharacter(CharacterType.BracketedText, CurrentChar);
while (Read() != FailedRead)
{
if (IsCustomStatement)
{
ReadCustomStatement();
Read();
}
ReadCharacter(CharacterType.BracketedText, CurrentChar);
if (IsEndOfBracketedText)
{
Expand Down
46 changes: 44 additions & 2 deletions src/dbup-tests/Engine/VariableSubstitutionPreprocessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void substitutes_variables_in_body()
}

[Fact]
public void ignores_variables_in_quoted_text()
public void substitutes_variables_in_quoted_text()
{
var journal = Substitute.For<IJournal>();
var connection = Substitute.For<IDbConnection>();
Expand All @@ -52,7 +52,7 @@ public void ignores_variables_in_quoted_text()

upgradeEngine.PerformUpgrade();

command.CommandText.ShouldBe("'$somevar$'");
command.CommandText.ShouldBe("'coriander'");
}

[Fact]
Expand All @@ -74,6 +74,7 @@ public void ignores_undefined_variables_in_comments()

command.CommandText.ShouldBe("/*$somevar$*/");
}

[Fact]
public void ignores_undefined_variables_in_complex_comments()
{
Expand Down Expand Up @@ -135,5 +136,46 @@ public void throws_for_undefined_variable()
result.Successful.ShouldBeFalse();
result.Error.ShouldBeOfType<InvalidOperationException>();
}

[Fact]
public void ignores_if_whitespace_between_dollars()
{
var journal = Substitute.For<IJournal>();
var connection = Substitute.For<IDbConnection>();
var command = Substitute.For<IDbCommand>();
connection.CreateCommand().Returns(command);

var upgradeEngine = DeployChanges.To
.SqlDatabase(() => connection, "Db")
.WithScript("testscript", "$some var$")
.JournalTo(journal)
.WithVariable("some var", "coriander")
.Build();

var result = upgradeEngine.PerformUpgrade();

result.Successful.ShouldBeTrue();
command.CommandText.ShouldBe("$some var$");
}

[Fact]
public void ignores_if_newline_between_dollars()
{
var journal = Substitute.For<IJournal>();
var connection = Substitute.For<IDbConnection>();
var command = Substitute.For<IDbCommand>();
connection.CreateCommand().Returns(command);

var upgradeEngine = DeployChanges.To
.SqlDatabase(() => connection, "Db")
.WithScript("testscript", "$some\nvar$")
.JournalTo(journal)
.WithVariable("somevar", "coriander")
.Build();

var result = upgradeEngine.PerformUpgrade();

result.Successful.ShouldBeTrue();
}
}
}

0 comments on commit f1f19af

Please sign in to comment.