Skip to content

Commit

Permalink
Feature/one branch for all versions (#32)
Browse files Browse the repository at this point in the history
* use different target versions in the project

Co-authored-by: Ilya Belyanskiy <win7user20@gmail.com>
  • Loading branch information
win7user10 and Ilya Belyanskiy committed Feb 16, 2022
1 parent 8df693b commit 57f0b42
Show file tree
Hide file tree
Showing 20 changed files with 303 additions and 104 deletions.
19 changes: 12 additions & 7 deletions .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@ jobs:
- name: Checkout repository
uses: actions/checkout@master

- name: Setup .NET environment
- name: Setup .NET5 environment
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.100'
dotnet-version: '5.0.x'

- name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Setup .NET6 environment
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'

- name: Set release env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV && echo "NET_TARGET=net${GITHUB_REF:10:1}.0" >> $GITHUB_ENV

- name: Build project
run: dotnet build --configuration Release
run: dotnet build --framework=${{env.NET_TARGET}} --configuration Release

- name: Run unit tests
run: dotnet test --filter "Category=UnitTest"
run: dotnet test --framework=${{env.NET_TARGET}} --filter "Category=UnitTest"

- name: Generate a NuGet package
run: dotnet pack -p:PackageVersion=${{env.RELEASE_VERSION}} --no-build -c Release -o .
run: dotnet pack -p:TargetFrameworks=${{env.NET_TARGET}} -p:PackageVersion=${{env.RELEASE_VERSION}} --no-build -c Release -o .

- name: Push to GitHub package registry
run: dotnet nuget push "*.nupkg" -k ${{secrets.NUGETORGTOKEN}} -s https://api.nuget.org/v3/index.json --skip-duplicate
7 changes: 7 additions & 0 deletions Laraue.EfCoreTriggers.sln
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Laraue.EfCoreTriggers.MySql
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Laraue.EfCoreTriggers.SqlServer", "src\Laraue.EfCoreTriggers.SqlServer\Laraue.EfCoreTriggers.SqlServer.csproj", "{FBB5388A-4AFD-4891-A90A-51E59E364278}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Laraue.EfCoreTriggers.TestMigration", "tests\Laraue.EfCoreTriggers.TestMigration\Laraue.EfCoreTriggers.TestMigration.csproj", "{50F9C440-4B10-4890-A266-A890341382EA}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -73,6 +75,10 @@ Global
{FBB5388A-4AFD-4891-A90A-51E59E364278}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBB5388A-4AFD-4891-A90A-51E59E364278}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FBB5388A-4AFD-4891-A90A-51E59E364278}.Release|Any CPU.Build.0 = Release|Any CPU
{50F9C440-4B10-4890-A266-A890341382EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{50F9C440-4B10-4890-A266-A890341382EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{50F9C440-4B10-4890-A266-A890341382EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{50F9C440-4B10-4890-A266-A890341382EA}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -88,6 +94,7 @@ Global
{EAF57617-5657-47D1-A719-424E1D6A63E8} = {9CDC27E2-01F4-4AA6-972C-D140415AA103}
{7B377F09-D92F-4844-AFC7-6642AC6174D1} = {9CDC27E2-01F4-4AA6-972C-D140415AA103}
{FBB5388A-4AFD-4891-A90A-51E59E364278} = {9CDC27E2-01F4-4AA6-972C-D140415AA103}
{50F9C440-4B10-4890-A266-A890341382EA} = {7EE4BD3E-BA14-4823-A5E7-1464B3591407}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3167DB00-67A4-49E7-A52F-CE3B5DBE02A6}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using Laraue.EfCoreTriggers.Common.Migrations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.Extensions.DependencyInjection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@ public EfCoreTriggersExtensionInfo(IDbContextOptionsExtension extension)
: base(extension)
{
}

/// <inheritdoc />
public override int GetServiceProviderHashCode()
{
return 0;
}

/// <inheritdoc />

#if NET6_0_OR_GREATER
public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo other)
{
return string.Equals(LogFragment, other.LogFragment, StringComparison.Ordinal);
}
public override int GetServiceProviderHashCode() => 0;
#else
public override long GetServiceProviderHashCode() => 0;
#endif

/// <inheritdoc />
public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<Description>Common classes EfCoreTriggers packages.</Description>
<RepositoryUrl>https://github.com/win7user10/Laraue.EfCoreTriggers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand All @@ -13,12 +13,20 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.0">
<ItemGroup Condition=" '$(TargetFramework)' == 'net5.0' ">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.14">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.14" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.2" />
</ItemGroup>

</Project>
108 changes: 75 additions & 33 deletions src/Laraue.EfCoreTriggers.Common/Migrations/MigrationsModelDiffer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Laraue.EfCoreTriggers.Common.Services.Impl.TriggerVisitors;
using Laraue.EfCoreTriggers.Common.TriggerBuilders.Base;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Storage;
Expand All @@ -31,15 +33,6 @@ public MigrationsModelDiffer(
{
_triggerVisitor = triggerVisitor;
}

private static string[] GetEntityTypeNames(IModel model)
{
return model?
.GetEntityTypes()
.Select(x => x.Name)
.ToArray()
?? Array.Empty<string>();
}

/// <inheritdoc />
public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalModel source, IRelationalModel target)
Expand All @@ -49,9 +42,12 @@ public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalMode

var sourceModel = source?.Model;
var targetModel = target?.Model;

_triggerVisitor.ConvertTriggerAnnotationsToSql(sourceModel);
_triggerVisitor.ConvertTriggerAnnotationsToSql(targetModel);

var oldEntityTypeNames = GetEntityTypeNames(sourceModel);
var newEntityTypeNames = GetEntityTypeNames(targetModel);
var oldEntityTypeNames = sourceModel.GetEntityTypeNames();
var newEntityTypeNames = targetModel.GetEntityTypeNames();

var commonEntityTypeNames = oldEntityTypeNames
.Intersect(newEntityTypeNames)
Expand All @@ -60,7 +56,7 @@ public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalMode
// Drop all triggers for deleted entities.
foreach (var deletedTypeName in oldEntityTypeNames.Except(commonEntityTypeNames))
{
var deletedEntityType = source?.Model.FindEntityType(deletedTypeName);
var deletedEntityType = sourceModel?.FindEntityType(deletedTypeName);

foreach (var annotation in deletedEntityType.GetTriggerAnnotations())
{
Expand All @@ -71,10 +67,9 @@ public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalMode
// Add all triggers to created entities.
foreach (var newTypeName in newEntityTypeNames.Except(commonEntityTypeNames))
{
var entityType = targetModel.FindEntityType(newTypeName);
foreach (var annotation in targetModel?.FindEntityType(newTypeName).GetTriggerAnnotations() ?? Array.Empty<IAnnotation>())
{
_triggerVisitor.AddCreateTriggerSqlMigration(createTriggerOperations, annotation, entityType);
createTriggerOperations.AddCreateTriggerSqlMigration(annotation);
}
}

Expand Down Expand Up @@ -105,16 +100,14 @@ public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalMode
{
var oldValue = sourceModel?.FindEntityType(entityTypeName)?.GetAnnotation(commonAnnotationName);
var newValue = targetModel?.FindEntityType(entityTypeName)?.GetAnnotation(commonAnnotationName);

var oldTrigger = _triggerVisitor.ConvertTriggerToSql(oldValue, oldEntityType);
var newTrigger = _triggerVisitor.ConvertTriggerToSql(newValue, newEntityType);
if (oldTrigger == newTrigger)

if (oldValue?.Value.ToString() == newValue?.Value.ToString())
{
continue;
}

_triggerVisitor.AddDeleteTriggerSqlMigration(deleteTriggerOperations, oldValue, sourceModel);
_triggerVisitor.AddCreateTriggerSqlMigration(createTriggerOperations, newValue, newEntityType);
createTriggerOperations.AddCreateTriggerSqlMigration(newValue);
}

// If trigger was removed, delete it.
Expand All @@ -130,7 +123,7 @@ public override IReadOnlyList<MigrationOperation> GetDifferences(IRelationalMode
{
var newTriggerAnnotation = newEntityType?.GetAnnotation(newTriggerName);

_triggerVisitor.AddCreateTriggerSqlMigration(createTriggerOperations, newTriggerAnnotation, newEntityType);
createTriggerOperations.AddCreateTriggerSqlMigration(newTriggerAnnotation);
}
}

Expand All @@ -154,14 +147,65 @@ private IReadOnlyList<MigrationOperation> MergeOperations(
/// </summary>
public static class MigrationsExtensions
{
public static string ConvertTriggerToSql(this ITriggerVisitor visitor, IAnnotation annotation, IEntityType entityType)
#if NET6_0_OR_GREATER
private static readonly FieldInfo AnnotationsField = typeof(AnnotatableBase)
.GetField("_annotations", BindingFlags.Instance | BindingFlags.NonPublic);
#else
private static readonly FieldInfo AnnotationsField = typeof(Annotatable)
.GetField("_annotations", BindingFlags.Instance | BindingFlags.NonPublic);
#endif


/// <summary>
/// Convert all not translated annotations of <see cref="ITrigger"/> type to SQL.
/// </summary>
/// <param name="triggerVisitor"></param>
/// <param name="model"></param>
public static void ConvertTriggerAnnotationsToSql(this ITriggerVisitor triggerVisitor, IModel model)
{
if (annotation.Value is ITrigger trigger)
foreach (var entityType in model?.GetEntityTypes() ?? Enumerable.Empty<IEntityType>())
{
return visitor.GenerateCreateTriggerSql(trigger);
}
var annotations = (SortedDictionary<string, Annotation>) AnnotationsField.GetValue(entityType);

if (annotations is null)
{
return;
}

foreach (var key in annotations.Keys.ToArray())
{
if (!key.StartsWith(Constants.AnnotationKey))
{
continue;
}

var annotation = annotations[key];

var value = annotation.Value;

throw new InvalidOperationException("The configured trigger cannot be converted to SQL");
if (value is not ITrigger trigger)
{
continue;
}

var sql = triggerVisitor.GenerateCreateTriggerSql(trigger);
annotations[key] = new ConventionAnnotation(key, sql, ConfigurationSource.DataAnnotation);
}
}
}

/// <summary>
/// Get names of entities in <see cref="IModel"/>.
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public static string[] GetEntityTypeNames(this IModel model)
{
return model?
.GetEntityTypes()
.Select(x => x.Name)
.ToArray()
?? Array.Empty<string>();
}

/// <summary>
Expand All @@ -181,20 +225,17 @@ public static IEnumerable<IAnnotation> GetTriggerAnnotations(this IEntityType en
/// </summary>
/// <param name="list"></param>
/// <param name="annotation"></param>
/// <param name="entityType"></param>
/// <returns></returns>
public static IList<SqlOperation> AddCreateTriggerSqlMigration(this ITriggerVisitor triggerVisitor, IList<SqlOperation> list, IAnnotation annotation, IEntityType entityType)
public static IList<SqlOperation> AddCreateTriggerSqlMigration(this IList<SqlOperation> list, IAnnotation annotation)
{
var triggerSql = triggerVisitor.ConvertTriggerToSql(annotation, entityType);

if (triggerSql is null)
if (annotation.Value is not string createSql)
{
return list;
}

list.Add(new SqlOperation
list.Add(new SqlOperation
{
Sql = triggerSql,
Sql = createSql,
});

return list;
Expand All @@ -203,11 +244,12 @@ public static IList<SqlOperation> AddCreateTriggerSqlMigration(this ITriggerVisi
/// <summary>
/// Adds sql operations necessary to drop triggers.
/// </summary>
/// <param name="triggerVisitor"></param>
/// <param name="list"></param>
/// <param name="annotation"></param>
/// <param name="model"></param>
/// <returns></returns>
public static IList<SqlOperation> AddDeleteTriggerSqlMigration(this ITriggerVisitor triggerVisitor, IList<SqlOperation> list, IAnnotation annotation, IReadOnlyModel model)
public static IList<SqlOperation> AddDeleteTriggerSqlMigration(this ITriggerVisitor triggerVisitor, IList<SqlOperation> list, IAnnotation annotation, IModel model)
{
list.Add(new SqlOperation
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<Description>Generating native MySql triggers through migrations using EFCore entity builder.</Description>
<RepositoryUrl>https://github.com/win7user10/Laraue.EfCoreTriggers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<Description>Generating native PostgreSql triggers through migrations using EFCore entity builder.</Description>
<RepositoryUrl>https://github.com/win7user10/Laraue.EfCoreTriggers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<Description>Generating native SqlLite triggers through migrations using EFCore entity builder.</Description>
<RepositoryUrl>https://github.com/win7user10/Laraue.EfCoreTriggers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
<Description>Generating native SqlServer triggers through migrations using EFCore entity builder.</Description>
<RepositoryUrl>https://github.com/win7user10/Laraue.EfCoreTriggers</RepositoryUrl>
<RepositoryType>git</RepositoryType>
Expand Down
Loading

0 comments on commit 57f0b42

Please sign in to comment.