Added NDB.Infrastructure.DatabaseMigration

master
Tudor Stanciu 2022-02-10 20:21:53 +02:00
parent f00c0d6269
commit bcb7bf342a
15 changed files with 95 additions and 76 deletions

View File

@ -1,7 +1,7 @@
<Project>
<Import Project="dependencies.props" />
<PropertyGroup>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<Authors>Tudor Stanciu</Authors>
<Company>STA</Company>
<PackageTags>IdentityServer</PackageTags>

View File

@ -12,6 +12,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="$(MicrosoftExtensionsPackageVersion)" />
<PackageReference Include="NDB.Extensions.Swagger" Version="$(NDBExtensionsSwaggerPackageVersion)" />
<PackageReference Include="NDB.Infrastructure.DatabaseMigration" Version="$(NDBDatabaseMigrationPackageVersion)" />
<PackageReference Include="Serilog.AspNetCore" Version="$(SerilogPackageVersion)" />
<PackageReference Include="Serilog.Extensions.Logging" Version="$(SerilogExtensionsPackageVersion)" />
<PackageReference Include="Serilog.Sinks.Console" Version="$(SerilogSinksConsolePackageVersion)" />

View File

@ -16,7 +16,7 @@ namespace IdentityServer.Api
{
public static void Main(string[] args)
{
var isConsole = Debugger.IsAttached || args.Contains("--console");
var isConsole = args.Contains("--console");
if (!isConsole)
{
var pathToExe = Process.GetCurrentProcess().MainModule.FileName;

View File

@ -1,4 +1,3 @@
using AutoMapper;
using IdentityServer.Application;
using IdentityServer.Application.Services.Abstractions;
using IdentityServer.Domain.Data;
@ -11,6 +10,8 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants;
using NDB.Infrastructure.DatabaseMigration;
using NDB.Infrastructure.DatabaseMigration.Constants;
using Newtonsoft.Json;
using System.Reflection;
@ -44,6 +45,7 @@ namespace IdentityServer.Api
services.AddSwagger("Identity Server API", AuthorizationType.None);
// Data access
services.AddMigration(DatabaseType.SQLServer);
services.AddDataAccess();
// Application
@ -78,6 +80,8 @@ namespace IdentityServer.Api
});
app.ConfigureSwagger("IdentityServer API");
app.UseMigration();
var behaviorService = app.ApplicationServices.GetService<IBehaviorService>();
behaviorService.FillTokenStore();
}

View File

@ -12,4 +12,28 @@
<ProjectReference Include="..\IdentityServer.Domain\IdentityServer.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Scripts\1.0.0\03.IDX_AppUser_Email_NOTNULL.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.0\02.AppUser table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.0\01.UserStatus table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.1\01.UserClaim table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.1\02.UserToken table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.1\03.***REMOVED***.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Scripts\1.0.1\04.***REMOVED***.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -1,18 +0,0 @@
if not exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U')
begin
create table AppUser
(
UserId int identity(0, 1) constraint PK_AppUser primary key,
UserName varchar(100) not null,
[Password] varchar(100) not null,
CreationDate datetime constraint DF_AppUser_CreationDate default getdate()
)
end
go
if not exists (select top 1 1 from AppUser)
begin
insert into AppUser(UserName, [Password])
select '***REMOVED***', '***REMOVED***'
end
go

View File

@ -7,7 +7,6 @@ begin
StatusName varchar(50) not null
)
end
go
if not exists (select top 1 1 from UserStatus)
begin
@ -16,4 +15,3 @@ begin
select 'INACTIVE', 'Inactive' union
select 'BLOCKED', 'Blocked'
end
go

View File

@ -0,0 +1,19 @@
if not exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U')
begin
create table AppUser
(
UserId int identity(0, 1) constraint PK_AppUser primary key,
UserName varchar(100) not null constraint UQ_AppUser_UserName unique,
[Password] varchar(250) not null,
FirstName varchar(100),
LastName varchar(100),
Email varchar(100),
ProfilePictureUrl varchar(200),
SecurityStamp varchar(200) not null constraint UQ_AppUser_SecurityStamp unique,
StatusId int not null constraint FK_AppUser_UserStatus references UserStatus(StatusId),
CreationDate datetime not null constraint DF_AppUser_CreationDate default getdate(),
FailedLoginAttempts int,
LastLoginDate datetime,
PasswordChangeDate datetime
)
end

View File

@ -0,0 +1,6 @@
if not exists (select top 1 1 from sys.indexes where name = 'IDX_AppUser_Email_NOTNULL' AND object_id = OBJECT_ID('AppUser'))
begin
CREATE UNIQUE NONCLUSTERED INDEX IDX_AppUser_Email_NOTNULL
ON AppUser(Email)
WHERE Email IS NOT NULL
end

View File

@ -1,48 +0,0 @@
if exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U')
and not exists(select top 1 1 from sys.columns where name = 'SecurityStamp' and object_id = object_id('AppUser'))
begin
select * into #TmpAppUser from AppUser
drop table AppUser
end
if not exists (select top 1 1 from sys.objects where name = 'AppUser' and type = 'U')
begin
create table AppUser
(
UserId int identity(0, 1) constraint PK_AppUser primary key,
UserName varchar(100) not null constraint UQ_AppUser_UserName unique,
[Password] varchar(100) not null,
FirstName varchar(100),
LastName varchar(100),
Email varchar(100),
ProfilePictureUrl varchar(200),
SecurityStamp varchar(200) not null constraint UQ_AppUser_SecurityStamp unique,
StatusId int not null constraint FK_AppUser_UserStatus references UserStatus(StatusId),
CreationDate datetime not null constraint DF_AppUser_CreationDate default getdate(),
FailedLoginAttempts int,
LastLoginDate datetime,
PasswordChangeDate datetime
)
end
go
if not exists (select top 1 1 from sys.indexes where name = 'IDX_AppUser_Email_NOTNULL' AND object_id = OBJECT_ID('AppUser'))
begin
CREATE UNIQUE NONCLUSTERED INDEX IDX_AppUser_Email_NOTNULL
ON AppUser(Email)
WHERE Email IS NOT NULL
end
go
if not exists (select top 1 1 from AppUser)
begin
declare @activeStatusId int
select @activeStatusId = StatusId from UserStatus where StatusCode = 'ACTIVE'
insert into AppUser(UserName, [Password], CreationDate, SecurityStamp, StatusId)
select UserName, [Password], CreationDate, cast(newid() as varchar(100)), @activeStatusId as StatusId
from #TmpAppUser
drop table #TmpAppUser
end
go

View File

@ -0,0 +1,13 @@
if not exists (select top 1 1 from AppUser where UserName = '***REMOVED***')
begin
declare @activeStatusId int
select @activeStatusId = StatusId from UserStatus where StatusCode = 'ACTIVE'
insert into AppUser(UserName, [Password], FirstName, CreationDate, SecurityStamp, StatusId)
select '***REMOVED***' as UserName,
'***REMOVED***' as [Password],
***REMOVED*** as FirstName,
getdate() as CreationDate,
cast(newid() as varchar(100)) as SecurityStamp,
@activeStatusId as StatusId
end

View File

@ -0,0 +1,16 @@
if not exists (select top 1 1 from AppUser where UserName = '***REMOVED***')
begin
declare @activeStatusId int
select @activeStatusId = StatusId from UserStatus where StatusCode = 'ACTIVE'
insert into AppUser(UserName, [Password], FirstName, LastName, Email, ProfilePictureUrl, CreationDate, SecurityStamp, StatusId)
select '***REMOVED***' as UserName,
'***REMOVED***' as [Password],
***REMOVED*** as FirstName,
***REMOVED*** as LastName,
'***REMOVED******REMOVED***' as Email,
***REMOVED*** as ProfilePictureUrl,
getdate() as CreationDate,
cast(newid() as varchar(100)) as SecurityStamp,
@activeStatusId as StatusId
end

View File

@ -30,4 +30,10 @@
◾ Upgrade packages MicrosoftExtensions, AutoMapper, EntityFramework, NDB
</Content>
</Note>
<Note>
<Version>1.1.1</Version>
<Content>
◾ Added NDB.Infrastructure.DatabaseMigration
</Content>
</Note>
</ReleaseNotes>