Skip to content

Commit

Permalink
execute migrations with DbUp
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidEggenberger committed Nov 30, 2024
1 parent 17344db commit 03df1b2
Show file tree
Hide file tree
Showing 14 changed files with 304 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using Modules.Channels.Features.DomainFeatures.Channels.Events;
using Shared.Features.Domain;

namespace Modules.Channels.Features.DomainFeatures.Channels
Expand All @@ -19,14 +18,14 @@ public class Message : Entity
public List<Reaction> MakeMessageTopicVotes => votes;
internal void AddVote(Reaction vote)
{
if (votes.Any(v => v.CreatedByUserId == vote.CreatedByUserId) is false)
if (votes.Any(v => v.UserId == vote.UserId) is false)
{
votes.Add(vote);
}
}
internal void RemoveVote(Reaction vote)
{
votes.Remove(votes.FirstOrDefault(v => v.CreatedByUserId == vote.CreatedByUserId));
votes.Remove(votes.FirstOrDefault(v => v.UserId == vote.UserId));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<ProjectReference Include="..\..\..\..\Shared\Client\Shared.Client.csproj" />
<ProjectReference Include="..\..\..\..\Shared\Kernel\Shared.Kernel.csproj" />
<ProjectReference Include="..\..\Shared\Modules.Channels.Shared.csproj" />
<ProjectReference Include="..\DTOs\Modules.Channels.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ public async Task<ActionResult> GetChannelById([FromRoute] Guid id, Cancellation
[HttpPost]
public async Task CreateChannel([FromBody] ChannelDTO createChannelDTO, CancellationToken cancellationToken)
{
var createChannelCommand = mapper.Map<CreateChannel>(createChannelDTO);
await commandDispatcher.DispatchAsync(createChannelCommand, cancellationToken);
//var createChannelCommand = mapper.Map<CreateChannel>(createChannelDTO);
//await commandDispatcher.DispatchAsync(createChannelCommand, cancellationToken);
}

[HttpPut("{id}")]
[AuthorizeTenantAdmin]
public async Task UpdateChannel([FromBody] ChannelDTO updateChannelDTO, CancellationToken cancellationToken)
{
ChangeChannelName updateChannelCommand = mapper.Map<ChangeChannelName>(updateChannelDTO);
await commandDispatcher.DispatchAsync(updateChannelCommand, cancellationToken);
//ChangeChannelName updateChannelCommand = mapper.Map<ChangeChannelName>(updateChannelDTO);
//await commandDispatcher.DispatchAsync(updateChannelCommand, cancellationToken);
}

//[HttpPost("createMessage")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<ProjectReference Include="..\..\..\..\Shared\Features\Shared.Features.csproj" />
<ProjectReference Include="..\..\Features\Modules.Channels.Features.csproj" />
<ProjectReference Include="..\..\Shared\Modules.Channels.Shared.csproj" />
<ProjectReference Include="..\DTOs\Modules.Channels.Shared.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public async Task<Tenant> GetTenantExtendedByIdAsync(Guid tenantId)
var tenant = await Tenants
.Include(t => t.Memberships)
.Include(t => t.Invitations)
.Include(t => t.TenantSubscriptions)
.FirstOrDefaultAsync(t => t.TenantId == tenantId);
if (tenant == null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
IF OBJECT_ID(N'[MigrationHistory_TenantIdentity]') IS NULL
BEGIN
CREATE TABLE [MigrationHistory_TenantIdentity] (
[MigrationId] nvarchar(150) NOT NULL,
[ProductVersion] nvarchar(32) NOT NULL,
CONSTRAINT [PK_MigrationHistory_TenantIdentity] PRIMARY KEY ([MigrationId])
);
END;
GO

BEGIN TRANSACTION;
GO

IF SCHEMA_ID(N'Identity') IS NULL EXEC(N'CREATE SCHEMA [Identity];');
GO

CREATE TABLE [Identity].[AspNetRoles] (
[Id] uniqueidentifier NOT NULL,
[Name] nvarchar(256) NULL,
[NormalizedName] nvarchar(256) NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Identity].[AspNetUsers] (
[Id] uniqueidentifier NOT NULL,
[PictureUri] nvarchar(max) NULL,
[CountOfOpenTabs] int NOT NULL,
[SelectedTenantId] uniqueidentifier NOT NULL,
[UserName] nvarchar(256) NULL,
[NormalizedUserName] nvarchar(256) NULL,
[Email] nvarchar(256) NULL,
[NormalizedEmail] nvarchar(256) NULL,
[EmailConfirmed] bit NOT NULL,
[PasswordHash] nvarchar(max) NULL,
[SecurityStamp] nvarchar(max) NULL,
[ConcurrencyStamp] nvarchar(max) NULL,
[PhoneNumber] nvarchar(max) NULL,
[PhoneNumberConfirmed] bit NOT NULL,
[TwoFactorEnabled] bit NOT NULL,
[LockoutEnd] datetimeoffset NULL,
[LockoutEnabled] bit NOT NULL,
[AccessFailedCount] int NOT NULL,
CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Identity].[TenantSettings] (
[Id] uniqueidentifier NOT NULL,
[IconURI] nvarchar(max) NULL,
[CreatedByUserId] uniqueidentifier NOT NULL,
[IsSoftDeleted] bit NOT NULL,
[RowVersion] varbinary(max) NULL,
[CreatedAt] datetimeoffset NOT NULL,
[LastUpdatedAt] datetimeoffset NOT NULL,
[IsDeleted] bit NOT NULL,
CONSTRAINT [PK_TenantSettings] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Identity].[TenantStylings] (
[Id] uniqueidentifier NOT NULL,
[CreatedByUserId] uniqueidentifier NOT NULL,
[IsSoftDeleted] bit NOT NULL,
[RowVersion] varbinary(max) NULL,
[CreatedAt] datetimeoffset NOT NULL,
[LastUpdatedAt] datetimeoffset NOT NULL,
[IsDeleted] bit NOT NULL,
CONSTRAINT [PK_TenantStylings] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Identity].[AspNetRoleClaims] (
[Id] int NOT NULL IDENTITY,
[RoleId] uniqueidentifier NOT NULL,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [Identity].[AspNetRoles] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[AspNetUserClaims] (
[Id] int NOT NULL IDENTITY,
[UserId] uniqueidentifier NOT NULL,
[ClaimType] nvarchar(max) NULL,
[ClaimValue] nvarchar(max) NULL,
[ApplicationUserId] uniqueidentifier NULL,
CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_ApplicationUserId] FOREIGN KEY ([ApplicationUserId]) REFERENCES [Identity].[AspNetUsers] ([Id]),
CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Identity].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[AspNetUserLogins] (
[LoginProvider] nvarchar(128) NOT NULL,
[ProviderKey] nvarchar(128) NOT NULL,
[ProviderDisplayName] nvarchar(max) NULL,
[UserId] uniqueidentifier NOT NULL,
[ApplicationUserId] uniqueidentifier NULL,
CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_ApplicationUserId] FOREIGN KEY ([ApplicationUserId]) REFERENCES [Identity].[AspNetUsers] ([Id]),
CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Identity].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[AspNetUserRoles] (
[UserId] uniqueidentifier NOT NULL,
[RoleId] uniqueidentifier NOT NULL,
CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY ([UserId], [RoleId]),
CONSTRAINT [FK_AspNetUserRoles_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [Identity].[AspNetRoles] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_AspNetUserRoles_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Identity].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[AspNetUserTokens] (
[UserId] uniqueidentifier NOT NULL,
[LoginProvider] nvarchar(128) NOT NULL,
[Name] nvarchar(128) NOT NULL,
[Value] nvarchar(max) NULL,
[ApplicationUserId] uniqueidentifier NULL,
CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name]),
CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_ApplicationUserId] FOREIGN KEY ([ApplicationUserId]) REFERENCES [Identity].[AspNetUsers] ([Id]),
CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Identity].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[Tenants] (
[Id] uniqueidentifier NOT NULL,
[TenantId] uniqueidentifier NOT NULL,
[Name] nvarchar(max) NULL,
[StylingId] uniqueidentifier NULL,
[SettingsId] uniqueidentifier NULL,
[SubscriptionPlanType] int NOT NULL,
[CreatedByUserId] uniqueidentifier NOT NULL,
[IsSoftDeleted] bit NOT NULL,
[RowVersion] varbinary(max) NULL,
[CreatedAt] datetimeoffset NOT NULL,
[LastUpdatedAt] datetimeoffset NOT NULL,
[IsDeleted] bit NOT NULL,
CONSTRAINT [PK_Tenants] PRIMARY KEY ([Id]),
CONSTRAINT [FK_Tenants_TenantSettings_SettingsId] FOREIGN KEY ([SettingsId]) REFERENCES [Identity].[TenantSettings] ([Id]),
CONSTRAINT [FK_Tenants_TenantStylings_StylingId] FOREIGN KEY ([StylingId]) REFERENCES [Identity].[TenantStylings] ([Id])
);
GO

CREATE TABLE [Identity].[TenantInvitations] (
[TenantId] uniqueidentifier NOT NULL,
[UserId] uniqueidentifier NOT NULL,
[Role] int NOT NULL,
CONSTRAINT [FK_TenantInvitations_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [Identity].[AspNetUsers] ([Id]) ON DELETE CASCADE,
CONSTRAINT [FK_TenantInvitations_Tenants_TenantId] FOREIGN KEY ([TenantId]) REFERENCES [Identity].[Tenants] ([Id]) ON DELETE CASCADE
);
GO

CREATE TABLE [Identity].[TenantMeberships] (
[Id] uniqueidentifier NOT NULL,
[UserId] uniqueidentifier NOT NULL,
[TenantId] uniqueidentifier NULL,
[Role] int NOT NULL,
[ApplicationUserId] uniqueidentifier NULL,
[CreatedByUserId] uniqueidentifier NOT NULL,
[IsSoftDeleted] bit NOT NULL,
[RowVersion] varbinary(max) NULL,
[CreatedAt] datetimeoffset NOT NULL,
[LastUpdatedAt] datetimeoffset NOT NULL,
[IsDeleted] bit NOT NULL,
CONSTRAINT [PK_TenantMeberships] PRIMARY KEY ([Id]),
CONSTRAINT [FK_TenantMeberships_AspNetUsers_ApplicationUserId] FOREIGN KEY ([ApplicationUserId]) REFERENCES [Identity].[AspNetUsers] ([Id]),
CONSTRAINT [FK_TenantMeberships_Tenants_TenantId] FOREIGN KEY ([TenantId]) REFERENCES [Identity].[Tenants] ([Id])
);
GO

CREATE INDEX [IX_AspNetRoleClaims_RoleId] ON [Identity].[AspNetRoleClaims] ([RoleId]);
GO

CREATE UNIQUE INDEX [RoleNameIndex] ON [Identity].[AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL;
GO

CREATE INDEX [IX_AspNetUserClaims_ApplicationUserId] ON [Identity].[AspNetUserClaims] ([ApplicationUserId]);
GO

CREATE INDEX [IX_AspNetUserClaims_UserId] ON [Identity].[AspNetUserClaims] ([UserId]);
GO

CREATE INDEX [IX_AspNetUserLogins_ApplicationUserId] ON [Identity].[AspNetUserLogins] ([ApplicationUserId]);
GO

CREATE INDEX [IX_AspNetUserLogins_UserId] ON [Identity].[AspNetUserLogins] ([UserId]);
GO

CREATE INDEX [IX_AspNetUserRoles_RoleId] ON [Identity].[AspNetUserRoles] ([RoleId]);
GO

CREATE INDEX [EmailIndex] ON [Identity].[AspNetUsers] ([NormalizedEmail]);
GO

CREATE UNIQUE INDEX [UserNameIndex] ON [Identity].[AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL;
GO

CREATE INDEX [IX_AspNetUserTokens_ApplicationUserId] ON [Identity].[AspNetUserTokens] ([ApplicationUserId]);
GO

CREATE INDEX [IX_TenantInvitations_TenantId] ON [Identity].[TenantInvitations] ([TenantId]);
GO

CREATE INDEX [IX_TenantInvitations_UserId] ON [Identity].[TenantInvitations] ([UserId]);
GO

CREATE INDEX [IX_TenantMeberships_ApplicationUserId] ON [Identity].[TenantMeberships] ([ApplicationUserId]);
GO

CREATE INDEX [IX_TenantMeberships_TenantId] ON [Identity].[TenantMeberships] ([TenantId]);
GO

CREATE INDEX [IX_Tenants_SettingsId] ON [Identity].[Tenants] ([SettingsId]);
GO

CREATE INDEX [IX_Tenants_StylingId] ON [Identity].[Tenants] ([StylingId]);
GO

INSERT INTO [MigrationHistory_TenantIdentity] ([MigrationId], [ProductVersion])
VALUES (N'20240210092230_initial', N'8.0.1');
GO

COMMIT;
GO
41 changes: 41 additions & 0 deletions Source/Shared/Features/EFCore/DbUp/Registrator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using DbUp;
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shared.Features.EFCore.Configuration;
using Shared.Kernel.BuildingBlocks;
using System.Reflection;

namespace Shared.Features.EFCore.DbUp
{
public static class Registrator
{
public static async Task<IServiceCollection> AddDbUpMigration(this IServiceCollection services)
{
using var serviceProvider = services.BuildServiceProvider();

var isProduction = serviceProvider.GetRequiredService<IExecutionContext>().HostingEnvironment.IsProduction();
var efCoreConfiguration = serviceProvider.GetRequiredService<EFCoreConfiguration>();
var connectionString = isProduction ? efCoreConfiguration.SQLServerConnectionString_Prod : efCoreConfiguration.SQLServerConnectionString_Dev;

EnsureDatabase.For.SqlDatabase(connectionString);

var upgrader = DeployChanges.To
.SqlDatabase(connectionString)
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
.JournalToSqlTable("dbo", "MigrationHistory_DbUP")
.LogToConsole()
.Build();

var scripts = upgrader.GetScriptsToExecute();
foreach (var script in scripts)
{
Console.WriteLine(script.Name);
}

upgrader.PerformUpgrade();

return services;
}
}
}
1 change: 1 addition & 0 deletions Source/Shared/Features/Shared.Features.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="dbup-sqlserver" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.3" />
<PackageReference Include="Microsoft.Extensions.Caching.SqlServer" Version="8.0.3" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Web.Web.Client.BuildingBlocks.Authentication.Antiforgery;
using Shared.Kernel.BuildingBlocks.Auth.Constants;
using Modules.TenantIdentity.Shared.DTOs;
using Modules.TenantIdentity.Shared.DTOs.IdentityOperations;

namespace Web.Web.Client.BuildingBlocks.Authentication
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Modules.TenantIdentity.Shared.DTOs.Team;
using Modules.TenantIdentity.Shared.DTOs.Tenant;
using Shared.Client;

namespace Web.Client.Layouts.MainLayoutMenu
{
public partial class TeamsOverviewComponentBase : BaseComponent
{
protected IEnumerable<TeamDTO> teams;
protected IEnumerable<TenantDTO> teams;
protected override async Task OnInitializedAsync()
{
teams = await httpClientService.GetFromAPIAsync<IEnumerable<TeamDTO>>("/user/allTeams");
teams = await httpClientService.GetFromAPIAsync<IEnumerable<TenantDTO>>("/user/allTeams");
}
protected bool expanded = true;
public void Click()
Expand Down
6 changes: 3 additions & 3 deletions Source/Web/Client/Pages/Dashboard.razor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Microsoft.AspNetCore.Components;
using Modules.TenantIdentity.Shared.DTOs.Team;
using Modules.TenantIdentity.Shared.DTOs.Tenant;
using Shared.Client;
using Shared.Kernel.BuildingBlocks.Services.Http;

Expand All @@ -9,11 +9,11 @@ public partial class Dashboard : BaseComponent
{
[Inject]
public HttpClientService httpClientService { get; set; }
public List<TeamDTO> Teams { get; set; }
public List<TenantDTO> Teams { get; set; }

protected override async Task OnInitializedAsync()
{
Teams = await httpClientService.GetFromAPIAsync<List<TeamDTO>>("api/Team/all");
Teams = await httpClientService.GetFromAPIAsync<List<TenantDTO>>("api/Team/all");
}
}
}
Loading

0 comments on commit 03df1b2

Please sign in to comment.