-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
157 additions
and
9 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
41 changes: 41 additions & 0 deletions
41
backend/ZiziBot.DataMigration/MongoDb/Extension/MigrationExtension.cs
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,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(); | ||
} | ||
} |
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,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
11
backend/ZiziBot.DataMigration/MongoDb/Migrations/IMigration.cs
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,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
16
backend/ZiziBot.DataMigration/MongoDb/Migrations/InitMigration.cs
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,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
25
backend/ZiziBot.DataMigration/ZiziBot.DataMigration.csproj
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,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> |
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
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
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