Skip to content

Commit

Permalink
More docs updates
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeGinnivan committed Sep 11, 2015
1 parent 6aac4ec commit 4e09b5c
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 0 deletions.
Binary file added docs/images/add-scripts.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/first-run.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/logo.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/second-run.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -1 +1,68 @@
DbUp is a .NET library that helps you to deploy changes to SQL Server databases. It tracks which SQL scripts have been run already, and runs the change scripts that are needed to get your database up to date.

## Getting started
Start by creating a simple C# console project in Visual Studio, and adding your SQL scripts to it. From the Properties window, mark them as Embedded Resources:

![DbUp Sample](./images/add-scripts.png)

Next, use NuGet to install the DbUp package:

> Install-Package DbUp
Finally, in `Program.cs`, add the following code:

``` csharp
static int Main(string[] args)
{
var connectionString =
args.FirstOrDefault()
?? "Server=(local)\\SqlExpress; Database=MyApp; Trusted_connection=true";

var upgrader =
DeployChanges.To
.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.LogToConsole()
.Build();

var result = upgrader.PerformUpgrade();

if (!result.Successful)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(result.Error);
Console.ResetColor();
#if DEBUG
Console.ReadLine();
#endif
return -1;
}

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Success!");
Console.ResetColor();
return 0;
}
```

You now have a console application that can run change scripts against your database! After running it, you'll see something like this:
![First run - the scripts get executed](./images/first-run.png)

The next time you run it, you'll see:
![Second run - the scripts have already been executed](./images/second-run.png)

## Why a console application?
By putting your migration scripts in an executable that can be run, you'll get a lot of benefits:

1. You can hit F5 at any time to test the migration scripts
2. Other developers on your team can do the same
3. You can execute them as part of an automated deployment
4. You can run it during your integration tests
5. Since it's in your VS solution, it will go into source control along with the rest of your code

Alternatively, instead of a console application, you can create a class library that references DbUp, and embed it into a maintenance screen inside your application.

Check out this blog post to learn more about [the philosophy behind DbUp](./philosophy-behind-dbup.md).

## Other ways to use DbUp
See [usage](./usage.md)
4 changes: 4 additions & 0 deletions docs/more-info/script-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ DbUp can get it's scripts which need to be executed from anywhere. Out of the bo
// Custom script provider
builder.WithScripts(new MyCustomScriptProvider());
```
* **EmbeddedScriptAndCodeProvider**
An enhanced script provider implementation which retrieves upgrade scripts or IScript code upgrade scripts embedded in an assembly.
- Usage: `builder.WithScriptsAndCodeEmbeddedInAssembly(Assembly)`
- **Warning:** DbUp does not protect against Sql Injection attacks, code scripts allow you to generate an upgrade script dynamically based on data in your database if you need to. If this data is put into the resulting script it could well contain a sql injection attack.
56 changes: 56 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,59 @@ You can:
* Useful for bringing development environments into sync with automated environments
* Try to connect to the database (`TryConnect()`)
* Perform the database upgrade (`PerformUpgrade()`)

## Hosting options
A console is not the only way to use DbUp. For instance FunnelWeb detects if it needs an upgrade when it starts, if an upgrade is required it will put the web app into maintainance mode where an administrator needs to login then click the perform migration button.

There are any number of other ways to use DbUp. Feel free to submit a pull request to update this section with more information

## Code-based scripts
Sometimes migrations may require more logic than is easy or possible to perform in SQL alone. Code-based scripts provide the facility to generate SQL in code, with an open database connection and a `System.Data.IDbCommand` factory provided.

The code-based migration is a class that implements the `IScript` interface. The `ProvideScript()` method is called when it is the migration's turn to be executed, so the scripts before it have already been executed.

This example shows a query being called and the results used to build up an arbitrary set of `INSERT` statements:

``` csharp
public class Script0005ComplexUpdate : IScript
{
public string ProvideScript(Func<IDbCommand> commandFactory)
{
var cmd = sqlConnectionString.CreateCommand();

This comment has been minimized.

Copy link
@matt-richardson

matt-richardson Nov 4, 2015

Is this supposed to use the commandFactory here?

cmd.CommandText = "Select * from SomeTable";
var scriptBuilder = new StringBuilder();

using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
scriptBuilder.AppendLine(string.Format("insert into AnotherTable values ({0})", reader.GetString(0)));
}
}

return scriptBuilder;
}
}
```

Of course, the command factory can be used for more than just queries. The entire migration itself can be performed in code:

``` csharp
public class Script0006UpdateInCode : IScript
{
public string ProvideScript(Func<IDbCommand> commandFactory)
{
var command = commandFactory();

command.CommandText = "CREATE TABLE [dbo].[Foo]( [Name] NVARCHAR(MAX) NOT NULL )";
command.ExecuteNonQuery();

return "";
}
}
```

**WARNING:** This code is vulnerable to SQL injection attacks, this functionality is provided for flexibility but like any advanced feature use caution and make sure the data you are reading cannot contain SQL injection code.

### Discovering code scripts
See [script providers](./more-info/script-providers.md)

0 comments on commit 4e09b5c

Please sign in to comment.