Skip to content

Commit e5dfebf

Browse files
committed
Implemented onchange features
1 parent 637ca5e commit e5dfebf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1050
-113
lines changed

README.md

+91-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,91 @@
1-
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.
2-
3-
[![Join the chat at https://gitter.im/DbUp/DbUp](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/DbUp/DbUp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4-
[![Build status](https://ci.appveyor.com/api/projects/status/vm3lg8kk1pxn64pj/branch/master?svg=true)](https://ci.appveyor.com/project/DbUp/dbup/branch/master)
5-
6-
| | Stable | Prerelease |
7-
| :--: | :--: | :--: |
8-
| Documentation | [![Documentation Status](https://readthedocs.org/projects/dbup/badge/?version=stable)](https://readthedocs.org/projects/dbup/?badge=stable) | [![Documentation Status](https://readthedocs.org/projects/dbup/badge/?version=latest)](https://readthedocs.org/projects/dbup/?badge=latest) |
9-
| DbUp | [![NuGet](https://img.shields.io/nuget/dt/DbUp.svg)](https://www.nuget.org/packages/dbup) [![NuGet](https://img.shields.io/nuget/v/DbUp.svg)](https://www.nuget.org/packages/dbup) | [![NuGet](https://img.shields.io/nuget/vpre/DbUp.svg)](https://www.nuget.org/packages/dbup) |
10-
| DbUp-SqlServer | [![NuGet](https://img.shields.io/nuget/dt/dbup-sqlserver.svg)](https://www.nuget.org/packages/dbup-sqlserver) [![NuGet](https://img.shields.io/nuget/v/dbup-sqlserver.svg)](https://www.nuget.org/packages/dbup-sqlserver) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-sqlserver.svg)](https://www.nuget.org/packages/dbup-sqlserver) |
11-
| DbUp-MySql | [![NuGet](https://img.shields.io/nuget/dt/dbup-mysql.svg)](https://www.nuget.org/packages/dbup-mysql) [![NuGet](https://img.shields.io/nuget/v/dbup-mysql.svg)](https://www.nuget.org/packages/dbup-mysql) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-mysql.svg)](https://www.nuget.org/packages/dbup-mysql) |
12-
| DbUp-SQLite | [![NuGet](https://img.shields.io/nuget/dt/dbup-sqlite.svg)](https://www.nuget.org/packages/dbup-sqlite) [![NuGet](https://img.shields.io/nuget/v/dbup-sqlite.svg)](https://www.nuget.org/packages/dbup-sqlite) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-sqlite.svg)](https://www.nuget.org/packages/dbup-sqlite) |
13-
| DbUp-SQLite-Mono | [![NuGet](https://img.shields.io/nuget/dt/dbup-sqlite-mono.svg)](https://www.nuget.org/packages/dbup-sqlite-mono) [![NuGet](https://img.shields.io/nuget/v/dbup-sqlite-mono.svg)](https://www.nuget.org/packages/dbup-sqlite-mono) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-sqlite-mono.svg)](https://www.nuget.org/packages/dbup-sqlite-mono) |
14-
| DbUp-SqlCe | [![NuGet](https://img.shields.io/nuget/dt/dbup-sqlce.svg)](https://www.nuget.org/packages/dbup-sqlce) [![NuGet](https://img.shields.io/nuget/v/dbup-sqlce.svg)](https://www.nuget.org/packages/dbup-sqlce) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-sqlce.svg)](https://www.nuget.org/packages/dbup-sqlce) |
15-
| DbUp-PostgreSQL | [![NuGet](https://img.shields.io/nuget/dt/dbup-postgresql.svg)](https://www.nuget.org/packages/dbup-postgresql) [![NuGet](https://img.shields.io/nuget/v/dbup-postgresql.svg)](https://www.nuget.org/packages/dbup-postgresql) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-postgresql.svg)](https://www.nuget.org/packages/dbup-postgresql) |
16-
| DbUp-Firebird | [![NuGet](https://img.shields.io/nuget/dt/dbup-firebird.svg)](https://www.nuget.org/packages/dbup-firebird) [![NuGet](https://img.shields.io/nuget/v/dbup-firebird.svg)](https://www.nuget.org/packages/dbup-firebird) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-firebird.svg)](https://www.nuget.org/packages/dbup-firebird) |
17-
| DbUp-Oracle | [![NuGet](https://img.shields.io/nuget/dt/dbup-oracle.svg)](https://www.nuget.org/packages/dbup-oracle) [![NuGet](https://img.shields.io/nuget/v/dbup-oracle.svg)](https://www.nuget.org/packages/dbup-oracle) | [![NuGet](https://img.shields.io/nuget/vpre/dbup-oracle.svg)](https://www.nuget.org/packages/dbup-oracle) |
18-
19-
# Development
20-
## Getting Started
21-
Install:
22-
- Sql CE 4.0 SP1 https://www.microsoft.com/en-us/download/details.aspx?id=30709
1+
This fork adds several additional script options to DbUp. Implemented only for SQL server.
2+
3+
The most important addition is the redeploy functionality for deploying stored procedures, views, functions, etc.
4+
5+
DbUp provides an option to apply idempotent scripts using _NullJournal_ (http://dbup.readthedocs.io/en/latest/more-info/journaling)
6+
7+
```csharp
8+
DeployChanges.To
9+
.SqlDatabase(connectionString)
10+
.WithScriptsEmbeddedInAssembly(
11+
Assembly.GetExecutingAssembly(),
12+
s => s.Contains("everytime"))
13+
.JournalTo(new NullJournal())
14+
.Build();
15+
```
16+
17+
But the _NullJournal_ approach means that all scripts will be redeployed on each migration, which is not always a good thing because recreating a stored procedure or a view leads to a new execution plan in SQL Server and usage statistics would also be lost.
18+
19+
This can be improved using _ScriptOptions_
20+
21+
```csharp
22+
DeployChanges.To
23+
.SqlDatabase(connectionString)
24+
.WithScriptsFromFileSystem
25+
(
26+
Path.Combine(folderPath, "Migrations"),
27+
new FileSystemScriptOptions() { IncludeSubDirectories = true },
28+
new ScriptOptions()
29+
{
30+
FirstDeploymentAsStartingPoint = true,
31+
IncludeSubDirectoryInName = true
32+
}
33+
)
34+
.WithScriptsFromFileSystem
35+
(
36+
Path.Combine(folderPath, "Programmability"),
37+
new FileSystemScriptOptions() { IncludeSubDirectories = true },
38+
new ScriptOptions()
39+
{
40+
RedeployOnChange = true,
41+
FirstDeploymentAsStartingPoint = true,
42+
DependencyOrderFilePath = Path.Combine(folderPath, "dependencies.txt"),
43+
IncludeSubDirectoryInName = true
44+
}
45+
)
46+
.WithTransaction() // apply all changes in a single transaction
47+
.Build();
48+
```
49+
50+
With this setup, scripts in the _Migrations_ folder will be deployed only once and scripts in the _Programmability_ folder which are activated for redeployment, will be deployed again when their contents change.
51+
52+
A possible structure of the _Migrations_ and _Programmability_ folders:
53+
54+
![Folder structure](https://raw.githubusercontent.com/szilarddavid/dbup.onchange/master/docs/images/onchange.png)
55+
56+
# Available ScriptOptions
57+
58+
### RedeployOnChange
59+
60+
Controls whether scripts should be redeployed on content change
61+
62+
### FirstDeploymentAsStartingPoint
63+
64+
Controls whether the first deployment should be used as a starting point which means that scripts won't be deployed into the database during the first deployment, they will just be marked as processed.
65+
66+
Useful when activating DbUp on an existing database and redeploying all scripts is not ideal
67+
68+
### IncludeSubDirectoryInName
69+
70+
Controls whether to include the subdirectory path of a script file in the name of the script.
71+
72+
By default DbUp marks scripts as executed by file name. But if you have scripts with the same name in different database schemas (stored in different folders) this will lead to issues. By including the subdirectory path in the name of the script it is ensured that script names will be unique.
73+
74+
Another solution would be to include the schema name in the script file, in which case this script option is not necessary.
75+
76+
### DependencyOrderFilePath
77+
78+
Path to a file containing script dependencies
79+
80+
For example by providing a _dependencies.txt_ file:
81+
82+
```
83+
firstView.sql
84+
anotherView.sql
85+
```
86+
87+
This will ensure that _firstView.sql_ will be executed before _anotherView.sql_. Without a dependency order file, the scripts would be executed in alphabetical order.
88+
89+
Useful when having nested SQL views and the parent view references a child view which is not available when trying to create the parent view. With this approach it can be ensured that the child view is created before the parent.
90+
91+
Note: When used together with _IncludeSubDirectoryInName_ option, the scripts in the dependency file must include the path of the subdirectory in which the script is located, something like _Views/firstView.sql_ or _dbo/Views/firstView.sql_ (if you store the scripts in different folders per database schema)

src/README.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
This fork adds several additional script options to DbUp. Implemented only for SQL server.
2+
3+
The most important addition is the redeploy functionality for deploying stored procedures, views, functions, etc.
4+
5+
DbUp provides an option to apply idempotent scripts using _NullJournal_ (http://dbup.readthedocs.io/en/latest/more-info/journaling)
6+
7+
```csharp
8+
DeployChanges.To
9+
.SqlDatabase(connectionString)
10+
.WithScriptsEmbeddedInAssembly(
11+
Assembly.GetExecutingAssembly(),
12+
s => s.Contains("everytime"))
13+
.JournalTo(new NullJournal())
14+
.Build();
15+
```
16+
17+
But the _NullJournal_ approach means that all scripts will be redeployed on each migration, which is not always a good thing because recreating a stored procedure or a view leads to a new execution plan in SQL Server and usage statistics would also be lost.
18+
19+
This can be improved using _ScriptOptions_
20+
21+
```csharp
22+
DeployChanges.To
23+
.SqlDatabase(connectionString)
24+
.WithScriptsFromFileSystem
25+
(
26+
Path.Combine(folderPath, "Migrations"),
27+
new FileSystemScriptOptions() { IncludeSubDirectories = true },
28+
new ScriptOptions()
29+
{
30+
FirstDeploymentAsStartingPoint = true,
31+
IncludeSubDirectoryInName = true
32+
}
33+
)
34+
.WithScriptsFromFileSystem
35+
(
36+
Path.Combine(folderPath, "Programmability"),
37+
new FileSystemScriptOptions() { IncludeSubDirectories = true },
38+
new ScriptOptions()
39+
{
40+
RedeployOnChange = true,
41+
FirstDeploymentAsStartingPoint = true,
42+
DependencyOrderFilePath = Path.Combine(folderPath, "dependencies.txt"),
43+
IncludeSubDirectoryInName = true
44+
}
45+
)
46+
.WithTransaction() // apply all changes in a single transaction
47+
.Build();
48+
```
49+
50+
With this setup, scripts in the _Migrations_ folder will be deployed only once and scripts in the _Programmability_ folder which are activated for redeployment, will be deployed again when their contents change.
51+
52+
A possible structure of the _Migrations_ and _Programmability_ folders:
53+
54+
![Folder structure](https://raw.githubusercontent.com/szilarddavid/dbup.onchange/master/docs/images/onchange.png)
55+
56+
# Available ScriptOptions
57+
58+
### RedeployOnChange
59+
60+
Controls whether scripts should be redeployed on content change
61+
62+
### FirstDeploymentAsStartingPoint
63+
64+
Controls whether the first deployment should be used as a starting point which means that scripts won't be deployed into the database during the first deployment, they will just be marked as processed.
65+
66+
Useful when activating DbUp on an existing database and redeploying all scripts is not ideal
67+
68+
### IncludeSubDirectoryInName
69+
70+
Controls whether to include the subdirectory path of a script file in the name of the script.
71+
72+
By default DbUp marks scripts as executed by file name. But if you have scripts with the same name in different database schemas (stored in different folders) this will lead to issues. By including the subdirectory path in the name of the script it is ensured that script names will be unique.
73+
74+
Another solution would be to include the schema name in the script file, in which case this script option is not necessary.
75+
76+
### DependencyOrderFilePath
77+
78+
Path to a file containing script dependencies
79+
80+
For example by providing a _dependencies.txt_ file:
81+
82+
```
83+
firstView.sql
84+
anotherView.sql
85+
```
86+
87+
This will ensure that _firstView.sql_ will be executed before _anotherView.sql_. Without a dependency order file, the scripts would be executed in alphabetical order.
88+
89+
Useful when having nested SQL views and the parent view references a child view which is not available when trying to create the parent view. With this approach it can be ensured that the child view is created before the parent.
90+
91+
Note: When used together with _IncludeSubDirectoryInName_ option, the scripts in the dependency file must include the path of the subdirectory in which the script is located, something like _Views/firstView.sql_ or _dbo/Views/firstView.sql_ (if you store the scripts in different folders per database schema)

0 commit comments

Comments
 (0)