-
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
0 parents
commit 52c7385
Showing
8 changed files
with
299 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Release nuget | ||
|
||
on: | ||
release: | ||
types: [ published ] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Setup .NET Core | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: 6.0.* | ||
- name: Build | ||
run: dotnet build --configuration Release | ||
- name: Test | ||
run: dotnet test --configuration Release | ||
|
||
- name: Create the package - MyJetTools.Sdk.EfPostgres | ||
run: dotnet pack --configuration Release MyJetTools.Sdk.EfPostgres/MyJetTools.Sdk.EfPostgres.csproj /p:Version=${GITHUB_REF#refs/tags/} | ||
|
||
- name: Publish the package | ||
run: dotnet nuget push MyJetTools.Sdk.EfPostgres/bin/Release/*.nupkg -s "https://api.nuget.org/v3/index.json" -k ${{ secrets.NUGET_TOCKEN }} |
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,62 @@ | ||
|
||
# Global wildcards | ||
*.aps | ||
*.bak | ||
*.dll | ||
*.log | ||
*.ncb | ||
*.obj | ||
*.old | ||
*.opensdf | ||
*.orig | ||
*.pdb | ||
*.Publish.xml | ||
*.sdf | ||
*.sln.cache | ||
*.suo | ||
*.tmp | ||
*.user | ||
*.lock.json | ||
|
||
# Well-known infrastructure folders | ||
/packages/ | ||
/target/ | ||
**/aspnet_client/ | ||
|
||
# IDEs | ||
*[Rr]e[Ss]harper* | ||
**/.idea/ | ||
**/.metadata/ | ||
**/.settings/ | ||
/[Tt]est[Rr]esults/ | ||
[Bb]in/ | ||
[Oo]bj/ | ||
[Dd]ebug/ | ||
[Rr]elease/ | ||
ipch/ | ||
**/.vscode/ | ||
|
||
# Visual Studio project upgrade | ||
[Bb]ackup/ | ||
UpgradeLog* | ||
_UpgradeReport_Files/ | ||
|
||
# Operating Systems generated | ||
.DS_Store | ||
[Dd]esktop.ini | ||
[Tt]humbs.db | ||
|
||
#ncrunch | ||
*.*crunch* | ||
_NCrunch_* | ||
|
||
#publish profiles | ||
*publish.ps1 | ||
*.pubxml | ||
*.psm1 | ||
|
||
#some test | ||
.vs | ||
node_modules | ||
**/launchSettings.json | ||
**/appsettings*.json |
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 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyJetTools.Sdk.EfPostgres", "MyJetTools.Sdk.EfPostgres\MyJetTools.Sdk.EfPostgres.csproj", "{0DE9E7AD-F536-4F54-9764-A280F29402CB}" | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
EndGlobal |
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,107 @@ | ||
using System.Diagnostics; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using MyJetTools.Telemetry; | ||
|
||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace MyJetTools.Sdk.EfPostgres | ||
{ | ||
public static class DataBaseHelper | ||
{ | ||
public static void AddDatabase<T>(this IServiceCollection services, string schema, string connectionString, | ||
Func<DbContextOptions, T> contextFactory, | ||
bool replaceSllInstruction = true) | ||
where T : DbContext | ||
{ | ||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); | ||
|
||
connectionString = PrepareConnectionString(connectionString, replaceSllInstruction); | ||
|
||
services.AddSingleton(_ => | ||
{ | ||
var optionsBuilder = new DbContextOptionsBuilder<T>(); | ||
optionsBuilder.UseNpgsql(connectionString, | ||
builder => | ||
builder.MigrationsHistoryTable( | ||
$"__EFMigrationsHistory_{schema}", | ||
schema)); | ||
|
||
return optionsBuilder; | ||
}); | ||
|
||
var contextOptions = services.BuildServiceProvider().GetRequiredService<DbContextOptionsBuilder<T>>(); | ||
|
||
using var activity = TelemetrySource.StartActivity("database migration"); | ||
{ | ||
Console.WriteLine("======= begin database migration ======="); | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
|
||
using var context = contextFactory(contextOptions.Options); | ||
|
||
context.Database.Migrate(); | ||
|
||
sw.Stop(); | ||
Console.WriteLine($"======= end database migration ({sw.Elapsed.ToString()}) ======="); | ||
} | ||
} | ||
|
||
private static string PrepareConnectionString(string connectionString, bool replaceSllInstruction) | ||
{ | ||
if (!connectionString.Contains("ApplicationName")) | ||
{ | ||
var appName = Environment.GetEnvironmentVariable("ENV_INFO") ?? | ||
Assembly.GetEntryAssembly()?.GetName().Name; | ||
|
||
connectionString = connectionString.Last() != ';' | ||
? $"{connectionString};ApplicationName={appName}" | ||
: $"{connectionString}ApplicationName={appName}"; | ||
} | ||
|
||
if (replaceSllInstruction) | ||
connectionString = connectionString.Replace("Ssl Mode=Require", "Ssl Mode=VerifyFull"); | ||
|
||
return connectionString; | ||
} | ||
|
||
public static void AddDatabaseWithoutMigrations<T>(this IServiceCollection services, string schema, | ||
string connectionString, | ||
bool replaceSllInstruction = true) | ||
where T : DbContext | ||
{ | ||
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); | ||
|
||
connectionString = PrepareConnectionString(connectionString, replaceSllInstruction); | ||
|
||
services.AddSingleton(x => | ||
{ | ||
var optionsBuilder = new DbContextOptionsBuilder<T>(); | ||
optionsBuilder.UseNpgsql(connectionString, | ||
builder => | ||
builder.MigrationsHistoryTable( | ||
$"__EFMigrationsHistory_{schema}", | ||
schema)); | ||
|
||
return optionsBuilder; | ||
}); | ||
} | ||
|
||
public static PropertyBuilder<DateTime> SpecifyKindUtc<TEntity>( | ||
this EntityTypeBuilder<TEntity> builder, | ||
Expression<Func<TEntity, DateTime>> propertyExpression) | ||
where TEntity : class | ||
{ | ||
var res = builder | ||
.Property(propertyExpression) | ||
.HasConversion( | ||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc), | ||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc)); | ||
|
||
return res; | ||
} | ||
} | ||
} |
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,14 @@ | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
|
||
namespace MyJetTools.Sdk.EfPostgres | ||
{ | ||
// ReSharper disable once ClassNeverInstantiated.Global | ||
public class MyDateTimeConverterToUtc: ValueConverter<DateTime, DateTime> | ||
{ | ||
public MyDateTimeConverterToUtc() | ||
: base( | ||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc), | ||
v => DateTime.SpecifyKind(v, DateTimeKind.Utc)) | ||
{ } | ||
} | ||
} |
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,32 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace MyJetTools.Sdk.EfPostgres | ||
{ | ||
public class MyDbContext : DbContext | ||
{ | ||
public static ILoggerFactory? LoggerFactory { get; set; } | ||
|
||
public MyDbContext(DbContextOptions options) : base(options) | ||
{ | ||
} | ||
|
||
protected MyDbContext() | ||
{ | ||
} | ||
|
||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
if (LoggerFactory != null) | ||
{ | ||
optionsBuilder.UseLoggerFactory(LoggerFactory).EnableSensitiveDataLogging(); | ||
} | ||
} | ||
|
||
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) | ||
{ | ||
configurationBuilder.Properties<DateTime>().HaveConversion<MyDateTimeConverterToUtc>(); | ||
} | ||
|
||
} | ||
} |
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 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
|
||
// ReSharper disable UnusedMember.Global | ||
|
||
namespace MyJetTools.Sdk.EfPostgres | ||
{ | ||
public class MyDesignTimeContextFactory<T> : IDesignTimeDbContextFactory<T> where T : MyDbContext | ||
{ | ||
private readonly Func<DbContextOptions, T> _contextFactory; | ||
|
||
public MyDesignTimeContextFactory(Func<DbContextOptions, T> contextFactory) | ||
{ | ||
_contextFactory = contextFactory; | ||
} | ||
|
||
public T CreateDbContext(string[] args) | ||
{ | ||
var optionsBuilder = new DbContextOptionsBuilder<T>(); | ||
optionsBuilder.UseNpgsql(); | ||
|
||
return _contextFactory(optionsBuilder.Options); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
MyJetTools.Sdk.EfPostgres/MyJetTools.Sdk.EfPostgres.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,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="MyJetTools.Telemetry" Version="1.0.3" /> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.4" /> | ||
</ItemGroup> | ||
|
||
</Project> |