Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ODS-6111] Update EdFi.Security.DataAccess to .Net 8 #929

Merged
merged 9 commits into from
Jan 29, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
stephenfuqua marked this conversation as resolved.
Show resolved Hide resolved

env:
INFORMATIONAL_VERSION: "6.1"
INFORMATIONAL_VERSION: "6.2"
BUILD_INCREMENTER: "1"
CONFIGURATION: "Release"
AZURE_ARTIFACT_URL: "https://pkgs.dev.azure.com/ed-fi-alliance/Ed-Fi-Alliance-OSS/_packaging/EdFi/nuget/v3/index.json"
Expand All @@ -17,11 +17,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2
- name: Setup .NET
uses: actions/setup-dotnet@c0d4ad69d8bd405d234f1c9166d383b7a4f69ed8 # 2.1.0
with:
dotnet-version: 6.0.x
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: build
run: |
.\build.githubactions.ps1 build -Configuration ${{ env.CONFIGURATION }} -InformationalVersion ${{ env.INFORMATIONAL_VERSION}} -BuildCounter ${{ github.run_number }} -BuildIncrementer ${{env.BUILD_INCREMENTER}} -Solution "Application/EdFi.Security.DataAccess/EdFi.Security.DataAccess.sln" -ProjectFile "Application/EdFi.Security.DataAccess/EdFi.Security.DataAccess.csproj"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ name: EdFi.Security.DataAccess Pull request build and test

stephenfuqua marked this conversation as resolved.
Show resolved Hide resolved
on:
pull_request:
branches: [main]
branches:
- main-6x
paths:
- Application/EdFi.Security.DataAccess/**/*
- tests/EdFi.Security.DataAccess.UnitTests/**/*
- Application/EdFi.Common/**/*

env:
INFORMATIONAL_VERSION: "6.1"
INFORMATIONAL_VERSION: "6.2"
BUILD_INCREMENTER: "1"
CONFIGURATION: "Release"

Expand All @@ -15,11 +20,8 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@ec3a7ce113134d7a93b817d10a8272cb61118579 # v2
- name: Setup .NET
uses: actions/setup-dotnet@c0d4ad69d8bd405d234f1c9166d383b7a4f69ed8 # 2.1.0
with:
dotnet-version: 6.0.x
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1

- name: build
run: |
.\build.githubactions.ps1 build -Configuration ${{ env.CONFIGURATION }} -InformationalVersion ${{ env.INFORMATIONAL_VERSION}} -BuildCounter ${{ github.run_number }} -BuildIncrementer ${{env.BUILD_INCREMENTER}} -Solution "Application/EdFi.Security.DataAccess/EdFi.Security.DataAccess.sln" -ProjectFile "Application/EdFi.Security.DataAccess/EdFi.Security.DataAccess.csproj"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
// See the LICENSE and NOTICES files in the project root for more information.

using System;
using System.Data.Entity;
using System.Threading;
using System.Threading.Tasks;
using EdFi.Security.DataAccess.Models;
using Action = EdFi.Security.DataAccess.Models.Action;
using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Contexts
{
Expand All @@ -33,6 +34,6 @@ public interface ISecurityContext : IDisposable

int SaveChanges();

Task<int> SaveChangesAsync();
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,22 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.Data.Entity;
using System.Data.Entity.Core.Metadata.Edm;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.ModelConfiguration.Conventions;
using EdFi.Common;
using EdFi.Common.Utils.Extensions;
using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Contexts
{
public class PostgresSecurityContext : SecurityContext
{
public PostgresSecurityContext(string connectionString)
: base(connectionString) { }
public PostgresSecurityContext(DbContextOptions options)
: base(options) { }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Conventions.Add<ForeignKeyLowerCaseNamingConvention>();
modelBuilder.Conventions.Add<TableLowerCaseNamingConvention>();

modelBuilder.Properties().Configure(c => c.HasColumnName(c.ClrPropertyInfo.Name.ToLowerInvariant()));
}

private class TableLowerCaseNamingConvention : IStoreModelConvention<EntitySet>
{
public void Apply(EntitySet entitySet, DbModel model)
{
Preconditions.ThrowIfNull(entitySet, nameof(entitySet));
Preconditions.ThrowIfNull(model, nameof(model));

entitySet.Table = entitySet.Table.ToLowerInvariant();
}
}

private class ForeignKeyLowerCaseNamingConvention : IStoreModelConvention<AssociationType>
{
public void Apply(AssociationType association, DbModel model)
{
Preconditions.ThrowIfNull(association, nameof(association));
Preconditions.ThrowIfNull(model, nameof(model));

if (!association.IsForeignKey)
{
return;
}

association.Constraint.FromProperties.ForEach(PropertyNamesToLowerInvariant);
association.Constraint.ToProperties.ForEach(PropertyNamesToLowerInvariant);

void PropertyNamesToLowerInvariant(EdmProperty property) => property.Name = property.Name.ToLowerInvariant();
}
modelBuilder.Model.GetEntityTypes().ForEach(entityType =>
entityType.SetSchema("dbo"));
}
}
}
18 changes: 3 additions & 15 deletions Application/EdFi.Security.DataAccess/Contexts/SecurityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.Data.Entity;
using EdFi.Security.DataAccess.Models;
using EdFi.Security.DataAccess.Utils;
using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Contexts
{
public abstract class SecurityContext : DbContext, ISecurityContext
{
protected SecurityContext(string connectionString)
: base(connectionString)
{
Database.SetInitializer(new ValidateDatabase<SqlServerSecurityContext>());
Database.SetInitializer(new ValidateDatabase<PostgresSecurityContext>());
}
protected SecurityContext(DbContextOptions options)
: base(options) { }

public DbSet<Application> Applications { get; set; }

Expand All @@ -36,12 +31,5 @@ protected SecurityContext(string connectionString)

public DbSet<ResourceClaimActionAuthorizationStrategies> ResourceClaimActionAuthorizationStrategies { get; set; }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ResourceClaim>()
.HasOptional(rc => rc.ParentResourceClaim)
.WithMany()
.HasForeignKey(fk => fk.ParentResourceClaimId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using EdFi.Common.Configuration;
using EdFi.Security.DataAccess.Providers;
using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Contexts
{
Expand All @@ -27,11 +28,38 @@ public SecurityContextFactory(ISecurityDatabaseConnectionStringProvider connecti
_databaseEngine = databaseEngine;
}

public ISecurityContext CreateContext()
public Type GetSecurityContextType()
{
if (_securityContextTypeByDatabaseEngine.TryGetValue(_databaseEngine, out Type contextType))
{
return Activator.CreateInstance(contextType, _connectionStringProvider.GetConnectionString()) as ISecurityContext;
return contextType;
}

throw new InvalidOperationException(
$"No SecurityContext defined for database type {_databaseEngine.DisplayName}");
}

public ISecurityContext CreateContext()
{
if (_databaseEngine == DatabaseEngine.SqlServer)
{
return Activator.CreateInstance(
GetSecurityContextType(),
new DbContextOptionsBuilder<SqlServerSecurityContext>()
.UseSqlServer(_connectionStringProvider.GetConnectionString())
.Options) as
ISecurityContext;
}

if (_databaseEngine == DatabaseEngine.Postgres)
{
return Activator.CreateInstance(
GetSecurityContextType(),
new DbContextOptionsBuilder<PostgresSecurityContext>()
.UseNpgsql(_connectionStringProvider.GetConnectionString())
.UseLowerCaseNamingConvention()
.Options) as
ISecurityContext;
}

throw new InvalidOperationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Contexts
{
public class SqlServerSecurityContext : SecurityContext
{
// The default behavior is appropriate for this sub-class.
public SqlServerSecurityContext(string connectionString) : base(connectionString) { }
public SqlServerSecurityContext(DbContextOptions options) : base(options) { }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>EdFi.Security.DataAccess</PackageId>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>EdFi.Security.DataAccess</AssemblyName>
<RootNamespace>EdFi.Security.DataAccess</RootNamespace>
<RestorePackages>true</RestorePackages>
Expand All @@ -16,14 +16,15 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Common" Version="6.1.16" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="EntityFramework6.Npgsql" Version="6.4.3" />
<PackageReference Include="Npgsql" Version="6.0.3" />
<PackageReference Include="Autofac.Extras.DynamicProxy" Version="7.1.0" />
<PackageReference Include="EFCore.NamingConventions" Version="8.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.1" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="System.Buffers" Version="4.5.1" />
<PackageReference Include="System.Memory" Version="4.5.4" />
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
// SPDX-License-Identifier: Apache-2.0
// Licensed to the Ed-Fi Alliance under one or more agreements.
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;


namespace EdFi.Security.DataAccess.Models
{
Expand All @@ -14,15 +17,13 @@ public class ClaimSetResourceClaimActionAuthorizationStrategyOverrides

public int ClaimSetResourceClaimActionId { get; set; }

[Required]
[Index(IsUnique = true, Order = 1)]
[Required]
[ForeignKey("ClaimSetResourceClaimActionId")]
public ClaimSetResourceClaimAction ClaimSetResourceClaimAction { get; set; }

public int AuthorizationStrategyId { get; set; }

[Required]
[Index(IsUnique = true, Order = 2)]
[ForeignKey("AuthorizationStrategyId")]
public AuthorizationStrategy AuthorizationStrategy { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using EdFi.Common;
using EdFi.Common.Utils.Extensions;
using EdFi.Security.DataAccess.Contexts;
using EdFi.Security.DataAccess.Models;
using Microsoft.EntityFrameworkCore;

namespace EdFi.Security.DataAccess.Repositories
{
Expand Down
30 changes: 0 additions & 30 deletions Application/EdFi.Security.DataAccess/Utils/ValidateDatabase.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<AssemblyName>EdFi.Security.DataAccess.UnitTests</AssemblyName>
<RootNamespace>EdFi.Security.DataAccess.UnitTests</RootNamespace>
<Copyright>Copyright © 2020 Ed-Fi Alliance, LLC and Contributors</Copyright>
Expand All @@ -16,20 +16,17 @@
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EdFi.Suite3.Common" Version="6.1.19" />
<PackageReference Include="EntityFramework" Version="6.4.4" />
<PackageReference Include="EntityFrameworkTesting.FakeItEasy" Version="1.3.0" />
<PackageReference Include="FakeItEasy" Version="7.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.30" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="FakeItEasy" Version="8.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="NUnit" Version="4.0.1" />
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.36" />
<PackageReference Include="Shouldly" Version="4.2.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Application\EdFi.Security.DataAccess\EdFi.Security.DataAccess.csproj" />
</ItemGroup>
</Project>
</Project>
Loading