Skip to content

Commit d9805d9

Browse files
committed
Copy over from main project
1 parent 1d62108 commit d9805d9

33 files changed

+2115
-4
lines changed

src/Sample/Program.cs

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using DbUp;
5+
using DbUp.Engine;
6+
using DbUp.Helpers;
7+
using DbUp.Support;
8+
9+
namespace SampleApplication
10+
{
11+
static class Program
12+
{
13+
public static void Main(string[] args)
14+
{
15+
var instanceName = @"(localdb)\\MSSQLLocalDB";
16+
// Uncomment the following line to run against sql local db instance.
17+
// string instanceName = @"(localdb)\Projects";
18+
19+
var connectionString =
20+
$"Server={instanceName}; Database=test8; Trusted_connection=true";
21+
22+
DropDatabase.For.SqlDatabase(connectionString);
23+
24+
EnsureDatabase.For.SqlDatabase(connectionString);
25+
26+
var upgradeEngineBuilder = DeployChanges.To
27+
.SqlDatabase(connectionString, null) //null or "" for default schema for user
28+
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), script =>
29+
{
30+
if (script.EndsWith("Script0006 - Transactions.sql"))
31+
return !args.Any(a => "--noError".Equals(a, StringComparison.InvariantCultureIgnoreCase));
32+
33+
return script.StartsWith("SampleApplication.Scripts.");
34+
})
35+
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), script => script.StartsWith("SampleApplication.RunAlways."), new SqlScriptOptions { ScriptType = ScriptType.RunAlways, RunGroupOrder = DbUpDefaults.DefaultRunGroupOrder + 1 })
36+
.LogToConsole();
37+
38+
if (args.Any(a => "--withTransaction".Equals(a, StringComparison.InvariantCultureIgnoreCase)))
39+
{
40+
upgradeEngineBuilder = upgradeEngineBuilder.WithTransaction();
41+
}
42+
else if (args.Any(a => "--withTransactionPerScript".Equals(a, StringComparison.InvariantCultureIgnoreCase)))
43+
{
44+
upgradeEngineBuilder = upgradeEngineBuilder.WithTransactionPerScript();
45+
}
46+
47+
var upgrader = upgradeEngineBuilder.Build();
48+
49+
Console.WriteLine("Is upgrade required: " + upgrader.IsUpgradeRequired());
50+
51+
if (args.Any(a => "--generateReport".Equals(a, StringComparison.InvariantCultureIgnoreCase)))
52+
{
53+
upgrader.GenerateUpgradeHtmlReport("UpgradeReport.html");
54+
}
55+
else
56+
{
57+
var result = upgrader.PerformUpgrade();
58+
59+
// Display the result
60+
if (result.Successful)
61+
{
62+
Console.ForegroundColor = ConsoleColor.Green;
63+
Console.WriteLine("Success!");
64+
}
65+
else
66+
{
67+
Console.ForegroundColor = ConsoleColor.Red;
68+
Console.WriteLine(result.Error);
69+
Console.WriteLine("Failed!");
70+
}
71+
}
72+
73+
Console.ForegroundColor = ConsoleColor.White;
74+
Console.WriteLine();
75+
Console.WriteLine("Press any key to delete your database and continue");
76+
Console.WriteLine();
77+
Console.WriteLine();
78+
Console.WriteLine(
79+
"Try the --withTransaction or --withTransactionPerScript to see transaction support in action");
80+
Console.WriteLine("--noError to exclude the broken script");
81+
Console.ReadKey();
82+
// Database will be deleted at this point
83+
}
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
IF DATABASE_PRINCIPAL_ID('Testing_Role') IS NULL
2+
BEGIN
3+
CREATE ROLE Testing_Role
4+
end

src/Sample/Sample.csproj

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFrameworks>net7.0</TargetFrameworks>
4+
<OutputType>Exe</OutputType>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\dbup-sqlserver\dbup-sqlserver.csproj" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<EmbeddedResource Include="Scripts\*.sql" />
14+
<EmbeddedResource Include="RunAlways\*.sql" />
15+
</ItemGroup>
16+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
-- Creates the following tables:
2+
-- * Entry
3+
-- * Feed
4+
-- * FeedItem
5+
-- * Revision
6+
7+
create table $schema$.[Entry](
8+
[Id] [int] identity(1,1) not null constraint PK_Entry_Id primary key,
9+
[Name] [nvarchar](50) not null,
10+
[Title] [nvarchar](200) not null,
11+
[Summary] [nvarchar](max) not null,
12+
[IsVisible] [bit] not null,
13+
[Published] [datetime] not null,
14+
[LatestRevisionId] [int] null
15+
)
16+
go
17+
18+
create table $schema$.[Feed](
19+
[Id] [int] identity(1,1) not null constraint PK_Feed_Id primary key,
20+
[Name] [nvarchar](100) not null,
21+
[Title] [nvarchar](255) not null,
22+
)
23+
go
24+
25+
create table $schema$.[Revision](
26+
[Id] [int] identity(1,1) not null constraint PK_Revision_Id primary key,
27+
[EntryId] [int] not null,
28+
[Body] [nvarchar](max) not null,
29+
[ChangeSummary] [nvarchar](1000) not null,
30+
[Reason] [nvarchar](1000) not null,
31+
[Revised] [datetime] not null,
32+
[Tags] [nvarchar](1000) not null,
33+
[Status] [int] not null,
34+
[IsVisible] [bit] not null,
35+
[RevisionNumber] [int] not null,
36+
)
37+
go
38+
39+
create table $schema$.[FeedItem](
40+
[Id] [int] identity(1,1) not null constraint PK_FeedItem_Id primary key,
41+
[FeedId] [int] not null,
42+
[ItemId] [int] not null,
43+
[SortDate] [datetime] not null,
44+
)
45+
go
46+
47+
create table $schema$.[Comment](
48+
[Id] [int] identity(1,1) not null constraint PK_Comment_Id primary key,
49+
[Body] [nvarchar](max) not null,
50+
[AuthorName] [nvarchar](100) not null,
51+
[AuthorCompany] [nvarchar](100) not null,
52+
[AuthorEmail] [nvarchar](100) not null,
53+
[AuthorUrl] [nvarchar](100) not null,
54+
[Posted] [datetime] not null,
55+
[EntryId] [int] not null,
56+
[Status] int not null
57+
)
58+
go
59+
60+
alter table $schema$.[Revision] with check add constraint [FK_Revision_Entry] foreign key([EntryId])
61+
references $schema$.[Entry] ([Id])
62+
go
63+
64+
alter table $schema$.[Revision] check constraint [FK_Revision_Entry]
65+
go
66+
67+
alter table $schema$.[Revision] add constraint [DF_Revision_RevisionNumber] default ((0)) FOR [RevisionNumber]
68+
go
69+
70+
alter table $schema$.[FeedItem] with check add constraint [FK_FeedItem_Entry] foreign key([ItemId])
71+
references $schema$.[Entry] ([Id])
72+
go
73+
74+
alter table $schema$.[FeedItem] check constraint [FK_FeedItem_Entry]
75+
go
76+
77+
alter table $schema$.[FeedItem] with check add constraint [FK_FeedItem_Feed] foreign key([FeedId])
78+
references $schema$.[Feed] ([Id])
79+
go
80+
81+
alter table $schema$.[FeedItem] check constraint [FK_FeedItem_Feed]
82+
go
83+
84+
alter table $schema$.[Comment] with check add constraint [FK_Comment_Comment] foreign key([EntryId])
85+
references $schema$.[Entry] ([Id])
86+
go
87+
88+
alter table $schema$.[Comment] check constraint [FK_Comment_Comment]
89+
go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Initial setup data
2+
3+
insert into $schema$.Feed([Name], [Title]) values ('default', 'Blog Feed');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- Settings and Statistics
2+
3+
create table $schema$.Setting(
4+
[Id] [int] identity(1,1) not null constraint PK_Setting_Id primary key,
5+
[Name] [nvarchar](50) not null,
6+
[Description] [nvarchar](max) not null,
7+
[DisplayName] [nvarchar](200) not null,
8+
[Value] [nvarchar](max) not null
9+
)
10+
go
11+
12+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('ui-title', 'Title', 'My FunnelWeb Site', 'Text: The title shown at the top in the browser.');
13+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('ui-introduction', 'Introduction', 'Welcome to your FunnelWeb blog. You can <a href="/login">login</a> and edit this message in the administration section. The default username and password is <code>test/test</code>.', 'Markdown: The introductory text that is shown on the home page.');
14+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('ui-links', 'Main Links', '<li><a href="/projects">Projects</a></li>', 'HTML: A list of links shown at the top of each page.');
15+
16+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('search-author', 'Author', 'Daffy Duck', 'Text: Your name.');
17+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('search-keywords', 'Keywords', '.net, c#, test', 'Comma-separated text: Keywords shown to search engines.');
18+
insert into $schema$.Setting([Name], DisplayName, Value, Description) values ('search-description', 'Description', 'My website.', 'Text: The description shown to search engines in the meta description tag.');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Settings and Statistics
2+
3+
create table $schema$.Redirect(
4+
[Id] [int] identity(1,1) not null constraint PK_Redirect_Id primary key,
5+
[From] [nvarchar](255) not null,
6+
[To] [nvarchar](255) not null
7+
)
8+
go
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Data;
3+
using DbUp.Engine;
4+
5+
namespace SampleApplication.Scripts
6+
{
7+
public class Script0005ComplexUpdate : IScript
8+
{
9+
public string ProvideScript(Func<IDbCommand> commandFactory)
10+
{
11+
// If you have something that requires logic to update, it is sometimes easier doing that in code rather than sql.
12+
// By creating a code script, you get an open connection and you can build the sql script on the fly at the time of execution
13+
//
14+
// The ProvideScript method will be called when it is THIS scripts turn to be executed, so the scripts before have already been executed
15+
16+
// Example
17+
//var cmd = sqlConnectionString.CreateCommand();
18+
//cmd.CommandText = "Select * from SomeTable";
19+
//var scriptBuilder = new StringBuilder();
20+
21+
//using (var reader = cmd.ExecuteReader())
22+
//{
23+
// while (reader.Read())
24+
// {
25+
// scriptBuilder.AppendLine(string.Format("insert into AnotherTable values ({0})", reader.GetString(0)));
26+
// }
27+
//}
28+
29+
//return scriptBuilder;
30+
31+
return "select 1";
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Settings and Statistics
2+
create table $schema$.Foo(
3+
[Id] [int] identity(1,1) not null primary key,
4+
[Name] [nvarchar](50) not null
5+
)
6+
go
7+
8+
insert into $schema$.[Entry] values()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
DB Operation: Open connection
2+
Info: Beginning database upgrade
3+
Info: Checking whether journal table exists..
4+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions'
5+
DB Operation: Dispose command
6+
Info: Journal table does not exist
7+
Info: Executing Database Server script 'Script0001.sql'
8+
Info: Checking whether journal table exists..
9+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions'
10+
DB Operation: Dispose command
11+
Info: Creating the [SchemaVersions] table
12+
DB Operation: Execute non query command: create table [SchemaVersions] (
13+
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
14+
[ScriptName] nvarchar(255) not null,
15+
[Applied] datetime not null
16+
)
17+
DB Operation: Dispose command
18+
Info: The [SchemaVersions] table has been created
19+
DB Operation: Execute non query command: script1contents
20+
DB Operation: Dispose command
21+
DB Operation: Create parameter
22+
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
23+
DB Operation: Create parameter
24+
Info: DB Operation: Add parameter to command: applied=<date>
25+
DB Operation: Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
26+
DB Operation: Dispose command
27+
Info: Upgrade successful
28+
DB Operation: Dispose connection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
DB Operation: Open connection
2+
Info: Beginning database upgrade
3+
Info: Checking whether journal table exists..
4+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
5+
DB Operation: Dispose command
6+
Info: Journal table does not exist
7+
Info: Executing Database Server script 'Script0001.sql'
8+
Info: Checking whether journal table exists..
9+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'TestSchemaVersions' and TABLE_SCHEMA = 'test'
10+
DB Operation: Dispose command
11+
Info: Creating the [test].[TestSchemaVersions] table
12+
DB Operation: Execute non query command: create table [test].[TestSchemaVersions] (
13+
[Id] int identity(1,1) not null constraint [PK_TestSchemaVersions_Id] primary key,
14+
[ScriptName] nvarchar(255) not null,
15+
[Applied] datetime not null
16+
)
17+
DB Operation: Dispose command
18+
Info: The [test].[TestSchemaVersions] table has been created
19+
DB Operation: Execute non query command: script1contents
20+
DB Operation: Dispose command
21+
DB Operation: Create parameter
22+
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
23+
DB Operation: Create parameter
24+
Info: DB Operation: Add parameter to command: applied=<date>
25+
DB Operation: Execute non query command: insert into [test].[TestSchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
26+
DB Operation: Dispose command
27+
Info: Upgrade successful
28+
DB Operation: Dispose connection
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
DB Operation: Open connection
2+
Info: Beginning database upgrade
3+
Info: Checking whether journal table exists..
4+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions'
5+
DB Operation: Dispose command
6+
Info: Journal table does not exist
7+
Info: Executing Database Server script 'Script0001.sql'
8+
Info: Checking whether journal table exists..
9+
DB Operation: Execute scalar command: select 1 from INFORMATION_SCHEMA.TABLES where TABLE_NAME = 'SchemaVersions'
10+
DB Operation: Dispose command
11+
Info: Creating the [SchemaVersions] table
12+
DB Operation: Execute non query command: create table [SchemaVersions] (
13+
[Id] int identity(1,1) not null constraint [PK_SchemaVersions_Id] primary key,
14+
[ScriptName] nvarchar(255) not null,
15+
[Applied] datetime not null
16+
)
17+
DB Operation: Dispose command
18+
Info: The [SchemaVersions] table has been created
19+
DB Operation: Execute non query command: print SubstitutedValue
20+
DB Operation: Dispose command
21+
DB Operation: Create parameter
22+
Info: DB Operation: Add parameter to command: scriptName=Script0001.sql
23+
DB Operation: Create parameter
24+
Info: DB Operation: Add parameter to command: applied=<date>
25+
DB Operation: Execute non query command: insert into [SchemaVersions] (ScriptName, Applied) values (@scriptName, @applied)
26+
DB Operation: Dispose command
27+
Info: Upgrade successful
28+
DB Operation: Dispose connection

0 commit comments

Comments
 (0)