-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a Migrations project to migrate the
- Loading branch information
Niek van Weeghel
authored and
Niek van Weeghel
committed
May 17, 2023
1 parent
5409f67
commit b479148
Showing
6 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
IF OBJECT_ID(N'[__EFMigrationsHistory]') IS NULL | ||
BEGIN | ||
CREATE TABLE [__EFMigrationsHistory] ( | ||
[MigrationId] nvarchar(150) NOT NULL, | ||
[ProductVersion] nvarchar(32) NOT NULL, | ||
CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId]) | ||
); | ||
END; | ||
GO | ||
|
||
BEGIN TRANSACTION; | ||
GO | ||
|
||
IF SCHEMA_ID(N'IdentityServerConfig') IS NULL EXEC(N'CREATE SCHEMA [IdentityServerConfig];'); | ||
GO | ||
|
||
CREATE TABLE [IdentityServerConfig].[AuditLog] ( | ||
[ID] int NOT NULL IDENTITY, | ||
[UserId] nvarchar(max) NOT NULL, | ||
[Timestamp] datetime2 NOT NULL, | ||
[Action] nvarchar(max) NOT NULL, | ||
CONSTRAINT [PK_AuditLog] PRIMARY KEY ([ID]) | ||
); | ||
GO | ||
|
||
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) | ||
VALUES (N'20230516071604_InitialCreate', N'7.0.5'); | ||
GO | ||
|
||
COMMIT; | ||
GO | ||
|
||
BEGIN TRANSACTION; | ||
GO | ||
|
||
ALTER TABLE [IdentityServerConfig].[AuditLog] ADD [UserName] nvarchar(max) NOT NULL DEFAULT N''; | ||
GO | ||
|
||
INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion]) | ||
VALUES (N'20230516082321_AddedUserName', N'7.0.5'); | ||
GO | ||
|
||
COMMIT; | ||
GO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<UserSecretsId>8d14d57a-5ec9-430f-9b5a-8e40c3558bb2</UserSecretsId> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="DbUp.Reboot.SqlServer" Version="1.5.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="DeploymentScripts\InitialCreate.sql"> | ||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using IdentityServerConfig.Migrations; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
|
||
|
||
var host = Host.CreateDefaultBuilder(args).ConfigureServices((context, services) => | ||
{ | ||
services.AddHostedService<Worker>(); | ||
}).Build(); | ||
|
||
await host.RunAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.Diagnostics; | ||
using DbUp.Reboot; | ||
using DbUp.Reboot.Engine; | ||
using DbUp.Reboot.ScriptProviders; | ||
using DbUp.Reboot.Support; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace IdentityServerConfig.Migrations; | ||
|
||
public class Worker : BackgroundService | ||
{ | ||
private readonly IHostApplicationLifetime _hostApplicationLifetime; | ||
private readonly ILogger<Worker> _logger; | ||
private readonly IConfiguration _configuration; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="Worker" /> class. | ||
/// </summary> | ||
public Worker(ILogger<Worker> logger, IHostApplicationLifetime hostApplicationLifetime, IConfiguration configuration) | ||
{ | ||
_logger = logger; | ||
_hostApplicationLifetime = hostApplicationLifetime; | ||
_configuration = configuration; | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
try | ||
{ | ||
_logger.LogInformation("Starting migration"); | ||
|
||
await Task.Run(() => | ||
{ | ||
var connectionString = _configuration.GetConnectionString("AuditLog"); | ||
if (connectionString == null) | ||
{ | ||
throw new ArgumentException("ConnectionString cannot be null"); | ||
} | ||
_logger.LogInformation("Connection string is {ConnectionString}", connectionString); | ||
|
||
EnsureDatabase.For.SqlDatabase(connectionString); | ||
|
||
var path = Path.GetDirectoryName(Process.GetCurrentProcess()?.MainModule?.FileName); | ||
_logger.LogInformation(path); | ||
var upgradeEngineBuilder = DeployChanges.To | ||
.SqlDatabase(connectionString, null) | ||
.WithScriptsFromFileSystem(path, | ||
new FileSystemScriptOptions { IncludeSubDirectories = true, Filter = (x) => x.Contains("DeploymentScripts") }, | ||
new SqlScriptOptions { ScriptType = ScriptType.RunOnce, RunGroupOrder = 1 }) | ||
.WithTransaction() | ||
.LogToConsole(); | ||
|
||
Task.Delay(10000); | ||
|
||
var upgrader = upgradeEngineBuilder.Build(); | ||
_logger.LogInformation("Is upgrade required: " + upgrader.IsUpgradeRequired()); | ||
|
||
var result = upgrader.PerformUpgrade(); | ||
|
||
// Display the result | ||
if (result.Successful) | ||
{ | ||
_logger.LogInformation("Success!"); | ||
} | ||
else | ||
{ | ||
_logger.LogError(result.Error, "Migration failed."); | ||
Environment.ExitCode = 2; | ||
} | ||
|
||
_logger.LogInformation("Finished executing migrations, shutting down"); | ||
_hostApplicationLifetime.StopApplication(); | ||
}, stoppingToken); | ||
} | ||
catch | ||
{ | ||
Environment.ExitCode = 1; | ||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters