Skip to content

Commit

Permalink
Added support for database patch actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Röthinger committed Mar 26, 2023
1 parent b734682 commit ecbd56c
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<TargetFrameworks>net7.0;net48</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand All @@ -10,7 +10,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ public void AddLiteDatabaseTest_WithOptions()
}
}

[Test()]
public void AddLiteDatabaseTest_WithOptionsAndPatch()
{
var options = new LiteDatabaseServiceOptions(ConnectionString);
options.AddDatabasePatch(action => { action.UserVersion = 1; });
using (var provider = new ServiceCollection()
.AddLiteDatabase(options)
.BuildServiceProvider())
{
LiteDatabase database = null;
Assert.DoesNotThrow(() => database = provider.GetRequiredService<LiteDatabase>());
Assert.That(database.UserVersion, Is.EqualTo(1));
}
}


[Test()]
public void AddLiteDatabaseTest_WithOptions_SupportsLogging()
{
Expand Down Expand Up @@ -107,6 +123,41 @@ public void AddLiteDatabaseTest_WithConfigure()
}
}

[Test()]
public void AddLiteDatabaseTest_WithPatch()
{

using (var provider = new ServiceCollection()
.AddLiteDatabase(configure =>
{
configure.ConnectionString.Filename = ConnectionString;
configure.AddDatabasePatch(action => { action.UserVersion= 1; });
})
.BuildServiceProvider())
{
LiteDatabase database = null;
Assert.DoesNotThrow(() => database = provider.GetRequiredService<LiteDatabase>());
Assert.That(database.UserVersion, Is.EqualTo(1));
}
}

[Test()]
public void AddLiteDatabaseTest_WithPatchExceptionThrows()
{

using (var provider = new ServiceCollection()
.AddLiteDatabase(configure =>
{
configure.ConnectionString.Filename = ConnectionString;
configure.AddDatabasePatch(action => { throw new Exception(); });
})
.BuildServiceProvider())
{
LiteDatabase database = null;
Assert.Throws<LiteException>(() => database = provider.GetRequiredService<LiteDatabase>());
}
}

[Test()]
public void AddLiteDatabaseTest_WithConfigure_SupportsLogging()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<Version>0.0.93</Version>
<TargetFrameworks>net7.0;net48</TargetFrameworks>
<Version>0.1.0</Version>
<Authors>Alexander Röthinger</Authors>
<Company>AleRoe</Company>
<Description>Provides extension methods for registering and customizing a LiteDB.LiteDabase singleton instance using Microsoft.Extensions.DependencyInjection.</Description>
<Copyright>Alexander Röthinger, 2021</Copyright>
<Copyright>Alexander Röthinger, 2022</Copyright>
<PackageProjectUrl>https://aleroe.github.io/LiteDB.Extensions</PackageProjectUrl>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageOutputPath>C:\Users\Alexander\Source\LocalPackages</PackageOutputPath>
<RepositoryUrl>https://github.com/AleRoe/LiteDB.Extensions</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<RepositoryType>git</RepositoryType>
<AssemblyVersion>0.0.9.0003</AssemblyVersion>
<FileVersion>0.0.9.0003</FileVersion>
<AssemblyVersion>0.1.0.0000</AssemblyVersion>
<FileVersion>0.1.0.0000</FileVersion>
<PackageTags>dependency-injection;extension-methods;litedb;microsoft-extensions;litedb-extension</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="LiteDB" Version="5.0.16" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using LiteDB;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -32,7 +33,29 @@ public LiteDatabase Create()
throw new ArgumentNullException("LiteDB.Database connection string is invalid.", nameof(ConnectionString.Filename));

options.Logger?.LogInformation($"Using database {options.ConnectionString.Filename}");
return new LiteDatabase(options.ConnectionString, options.Mapper);
var database = new LiteDatabase(options.ConnectionString, options.Mapper);

if (options.DatabasePatches.Any())
{
try
{
foreach (var item in options.DatabasePatches)
{
item.Invoke(database);
}
database.Commit();
}
catch (Exception e)
{
database.Rollback();
throw new LiteException(e.HResult, e.Message);
}

}


return database;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public static IServiceCollection AddLiteDatabase(this IServiceCollection service
{
configure.ConnectionString = options.ConnectionString;
configure.Mapper = options.Mapper;
options.DatabasePatches.ForEach(x => configure.AddDatabasePatch(x));
if (options.Logger != null)
{
configure.Logger = options.Logger;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using LiteDB;
using Microsoft.Extensions.Logging;

Expand All @@ -9,6 +10,8 @@ namespace AleRoe.LiteDB.Extensions.DependencyInjection
/// </summary>
public class LiteDatabaseServiceOptions
{

public List<Action<LiteDatabase>> DatabasePatches { get; } = new List<Action<LiteDatabase>>();
/// <summary>
/// Initializes a new instance of the <see cref="LiteDatabaseServiceOptions"/> class.
/// </summary>
Expand Down Expand Up @@ -39,5 +42,11 @@ public LiteDatabaseServiceOptions(string connectionString)
/// Gets or sets the logger.
/// </summary>
public ILogger Logger { get; set; }

public LiteDatabaseServiceOptions AddDatabasePatch(Action<LiteDatabase> action)
{
DatabasePatches.Add(action);
return this;
}
}
}

0 comments on commit ecbd56c

Please sign in to comment.