Skip to content

Commit

Permalink
add data migration project
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed Aug 23, 2024
1 parent f57dee3 commit d09718c
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<BaseOutputPath>.bin\</BaseOutputPath>
</PropertyGroup>
<PropertyGroup>
<Version>224.6.8926.36370</Version>
<Version>224.8.9001.10546</Version>
<Company>WinTenDev</Company>
<Authors>azhe403</Authors>
<Product>ZiziBot</Product>
Expand Down
7 changes: 7 additions & 0 deletions ZiziBot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZiziBot.Services", "backend
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06. Console", "06. Console", "{02A8A617-AFF2-4533-BA11-B60997D6AC6F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZiziBot.DataMigration", "backend\ZiziBot.DataMigration\ZiziBot.DataMigration.csproj", "{9BD15D6D-9095-4A29-90BD-793985E4A2DA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -153,6 +155,10 @@ Global
{41FE421C-80A8-48E9-BA15-D4B127D7B8E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41FE421C-80A8-48E9-BA15-D4B127D7B8E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41FE421C-80A8-48E9-BA15-D4B127D7B8E3}.Release|Any CPU.Build.0 = Release|Any CPU
{9BD15D6D-9095-4A29-90BD-793985E4A2DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BD15D6D-9095-4A29-90BD-793985E4A2DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BD15D6D-9095-4A29-90BD-793985E4A2DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BD15D6D-9095-4A29-90BD-793985E4A2DA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -179,6 +185,7 @@ Global
{C8C3B334-D515-4CC3-A515-8554BCC8B5DD} = {1D5062C1-B87A-4FF6-B542-87082E052414}
{41FE421C-80A8-48E9-BA15-D4B127D7B8E3} = {77C0CF9B-B088-4BB0-A6E2-572B76969F44}
{1F47415C-ECF8-4C6F-8C58-71053BAC1AE5} = {02A8A617-AFF2-4533-BA11-B60997D6AC6F}
{9BD15D6D-9095-4A29-90BD-793985E4A2DA} = {451F7CAB-39D5-47A3-ADF0-2DFE8AD9F95A}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {4E6205FB-34E5-4C5C-9C43-9AC95F5E8012}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using ZiziBot.Contracts.Constants;
using ZiziBot.DataMigration.MongoDb.Migrations;
using ZiziBot.Utils;

namespace ZiziBot.DataMigration.MongoDb.Extension;

public static class MigrationExtension
{
public static void AddMongoMigration(this IServiceCollection services)
{
var connectionStr = EnvUtil.GetEnv(Env.MONGODB_CONNECTION_STRING);
var url = new MongoUrl(connectionStr);

services.AddSingleton<MigrationRunner>();
services.AddSingleton<IMongoDatabase>(provider => {
var client = new MongoClient(connectionStr);
var database = client.GetDatabase(url.DatabaseName);
return database;
});

services.Scan(selector =>
selector.FromAssembliesOf(typeof(IMigration))
.AddClasses(filter => filter.InNamespaceOf<IMigration>())
.As<IMigration>()
.WithTransientLifetime()
);
}

public static async Task UseMongoMigration(this IApplicationBuilder app)
{
var runner = app.ApplicationServices.GetService<MigrationRunner>();

if (runner == null)
throw new ApplicationException("Mongo Migration not yet prepared");

await runner.ApplyMigrationAsync();
}
}
47 changes: 47 additions & 0 deletions backend/ZiziBot.DataMigration/MongoDb/MigrationRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using MongoDB.Bson;
using MongoDB.Driver;
using ZiziBot.DataMigration.MongoDb.Migrations;

namespace ZiziBot.DataMigration.MongoDb;

public class MigrationRunner(IServiceProvider serviceProvider, IMongoDatabase database, ILogger<MigrationRunner> logger)
{
private readonly IMongoCollection<BsonDocument> _migrationCollection = database.GetCollection<BsonDocument>("__migrations");

public async Task ApplyMigrationAsync()
{
var migrations = serviceProvider.GetServices<IMigration>();
foreach (var migration in migrations)
{
var migrationId = Builders<BsonDocument>.Filter.Eq("Name", migration.GetType().Name);
var applied = await _migrationCollection.Find(migrationId).FirstOrDefaultAsync();

if (applied == null)
{
logger.LogDebug("Applying Migration {Name}", migration.GetType().Name);

await migration.UpAsync(database);

var migrationHistory = new BsonDocument() {
{ "_id", migration.Id },
{ "Name", migration.GetType().Name },
{ "AppliedAt", DateTime.UtcNow }
};

await _migrationCollection.InsertOneAsync(migrationHistory);
logger.LogDebug("Migration {Name} applied", migration.GetType().Name);
}
else
{
logger.LogDebug("Migration {Name} already applied", migration.GetType().Name);
}
}
}

public async Task RollbackMigrationAsync()
{
// TODO. rollback migration
}
}
11 changes: 11 additions & 0 deletions backend/ZiziBot.DataMigration/MongoDb/Migrations/IMigration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MongoDB.Driver;

namespace ZiziBot.DataMigration.MongoDb.Migrations;

public interface IMigration
{
string Id { get; }

Task UpAsync(IMongoDatabase database);
Task DownAsync(IMongoDatabase database);
}
16 changes: 16 additions & 0 deletions backend/ZiziBot.DataMigration/MongoDb/Migrations/InitMigration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using MongoDB.Driver;

namespace ZiziBot.DataMigration.MongoDb.Migrations;

public class InitMigration : IMigration
{
public string Id => nameof(InitMigration);

public async Task UpAsync(IMongoDatabase database)
{
}

public async Task DownAsync(IMongoDatabase database)
{
}
}
25 changes: 25 additions & 0 deletions backend/ZiziBot.DataMigration/ZiziBot.DataMigration.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ZiziBot.Attributes\ZiziBot.Attributes.csproj"/>
<ProjectReference Include="..\ZiziBot.Contracts\ZiziBot.Contracts.csproj"/>
<ProjectReference Include="..\ZiziBot.Utils\ZiziBot.Utils.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.27.0"/>
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="8.0.3"/>
<PackageReference Include="Scrutor" Version="4.2.2"/>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion backend/ZiziBot.Engine/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
global using ZiziBot.Allowed.TelegramBot;
global using ZiziBot.Console.Extensions;
global using ZiziBot.Contracts.Constants;
global using ZiziBot.DataMigration.MongoDb.Extension;
global using ZiziBot.Hangfire;
global using ZiziBot.Infrastructure;
global using ZiziBot.Kot.MongoMigrations;
global using ZiziBot.Utils;
global using ZiziBot.WebApi;
global using ZiziBot.WebApi.Providers;
2 changes: 1 addition & 1 deletion backend/ZiziBot.Engine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

app.PrintAbout();

await app.RunMigrationsAsync();
await app.UseMongoMigration();

app.UseAuthorization();

Expand Down
7 changes: 4 additions & 3 deletions backend/ZiziBot.Engine/ZiziBot.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0"/>
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.7"/>
<PackageReference Include="NewRelic.Agent" Version="10.27.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
<PackageReference Include="Microsoft.AspNetCore.SpaProxy" Version="8.0.8"/>
<PackageReference Include="NewRelic.Agent" Version="10.29.0"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.1"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ZiziBot.Allowed.TelegramBot\ZiziBot.Allowed.TelegramBot.csproj"/>
<ProjectReference Include="..\ZiziBot.Application\ZiziBot.Application.csproj"/>
<ProjectReference Include="..\ZiziBot.Console\ZiziBot.Console.csproj"/>
<ProjectReference Include="..\ZiziBot.DataMigration\ZiziBot.DataMigration.csproj"/>
<ProjectReference Include="..\ZiziBot.DataSource\ZiziBot.DataSource.csproj"/>
<ProjectReference Include="..\ZiziBot.DiscordNet.DiscordBot\ZiziBot.DiscordNet.DiscordBot.csproj"/>
<ProjectReference Include="..\ZiziBot.Hangfire\ZiziBot.Hangfire.csproj"/>
Expand Down
1 change: 0 additions & 1 deletion backend/ZiziBot.Infrastructure/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
global using ZiziBot.Exceptions;
global using ZiziBot.Infrastructure.LoggerSink;
global using ZiziBot.Infrastructure.MongoConfig;
global using ZiziBot.Kot.MongoMigrations;
global using ZiziBot.Parsers;
global using ZiziBot.Services;
global using ZiziBot.Utils;
3 changes: 2 additions & 1 deletion backend/ZiziBot.Infrastructure/ServiceExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using ZiziBot.DataMigration.MongoDb.Extension;

namespace ZiziBot.Infrastructure;

Expand All @@ -15,7 +16,7 @@ public static class ServiceExtension
public static IServiceCollection ConfigureServices(this IServiceCollection services)
{
services.ConfigureSettings();
services.AddKotMongoMigrations();
services.AddMongoMigration();

services.AddMediator();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<ProjectReference Include="..\ZiziBot.Contracts\ZiziBot.Contracts.csproj" />
<ProjectReference Include="..\ZiziBot.Application\ZiziBot.Application.csproj" />
<ProjectReference Include="..\ZiziBot.Caching\ZiziBot.Caching.csproj" />
<ProjectReference Include="..\ZiziBot.DataMigration\ZiziBot.DataMigration.csproj"/>
<ProjectReference Include="..\ZiziBot.DataSource\ZiziBot.DataSource.csproj" />
<ProjectReference Include="..\ZiziBot.Kot.MongoMigrations\ZiziBot.Kot.MongoMigrations.csproj" />
<ProjectReference Include="..\ZiziBot.Services\ZiziBot.Services.csproj"/>
</ItemGroup>

Expand Down

0 comments on commit d09718c

Please sign in to comment.