Skip to content

Commit 52c7385

Browse files
committed
first
0 parents  commit 52c7385

File tree

8 files changed

+299
-0
lines changed

8 files changed

+299
-0
lines changed

.github/workflows/release.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release nuget
2+
3+
on:
4+
release:
5+
types: [ published ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v2
12+
- name: Setup .NET Core
13+
uses: actions/setup-dotnet@v1
14+
with:
15+
dotnet-version: 6.0.*
16+
- name: Build
17+
run: dotnet build --configuration Release
18+
- name: Test
19+
run: dotnet test --configuration Release
20+
21+
- name: Create the package - MyJetTools.Sdk.EfPostgres
22+
run: dotnet pack --configuration Release MyJetTools.Sdk.EfPostgres/MyJetTools.Sdk.EfPostgres.csproj /p:Version=${GITHUB_REF#refs/tags/}
23+
24+
- name: Publish the package
25+
run: dotnet nuget push MyJetTools.Sdk.EfPostgres/bin/Release/*.nupkg -s "https://api.nuget.org/v3/index.json" -k ${{ secrets.NUGET_TOCKEN }}

.gitignore

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
# Global wildcards
3+
*.aps
4+
*.bak
5+
*.dll
6+
*.log
7+
*.ncb
8+
*.obj
9+
*.old
10+
*.opensdf
11+
*.orig
12+
*.pdb
13+
*.Publish.xml
14+
*.sdf
15+
*.sln.cache
16+
*.suo
17+
*.tmp
18+
*.user
19+
*.lock.json
20+
21+
# Well-known infrastructure folders
22+
/packages/
23+
/target/
24+
**/aspnet_client/
25+
26+
# IDEs
27+
*[Rr]e[Ss]harper*
28+
**/.idea/
29+
**/.metadata/
30+
**/.settings/
31+
/[Tt]est[Rr]esults/
32+
[Bb]in/
33+
[Oo]bj/
34+
[Dd]ebug/
35+
[Rr]elease/
36+
ipch/
37+
**/.vscode/
38+
39+
# Visual Studio project upgrade
40+
[Bb]ackup/
41+
UpgradeLog*
42+
_UpgradeReport_Files/
43+
44+
# Operating Systems generated
45+
.DS_Store
46+
[Dd]esktop.ini
47+
[Tt]humbs.db
48+
49+
#ncrunch
50+
*.*crunch*
51+
_NCrunch_*
52+
53+
#publish profiles
54+
*publish.ps1
55+
*.pubxml
56+
*.psm1
57+
58+
#some test
59+
.vs
60+
node_modules
61+
**/launchSettings.json
62+
**/appsettings*.json

MyJetTools.Sdk.EfPostgres.sln

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyJetTools.Sdk.EfPostgres", "MyJetTools.Sdk.EfPostgres\MyJetTools.Sdk.EfPostgres.csproj", "{0DE9E7AD-F536-4F54-9764-A280F29402CB}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{0DE9E7AD-F536-4F54-9764-A280F29402CB}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System.Diagnostics;
2+
using System.Linq.Expressions;
3+
using System.Reflection;
4+
using Microsoft.EntityFrameworkCore;
5+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
6+
using Microsoft.Extensions.DependencyInjection;
7+
using MyJetTools.Telemetry;
8+
9+
// ReSharper disable UnusedMember.Global
10+
11+
namespace MyJetTools.Sdk.EfPostgres
12+
{
13+
public static class DataBaseHelper
14+
{
15+
public static void AddDatabase<T>(this IServiceCollection services, string schema, string connectionString,
16+
Func<DbContextOptions, T> contextFactory,
17+
bool replaceSllInstruction = true)
18+
where T : DbContext
19+
{
20+
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
21+
22+
connectionString = PrepareConnectionString(connectionString, replaceSllInstruction);
23+
24+
services.AddSingleton(_ =>
25+
{
26+
var optionsBuilder = new DbContextOptionsBuilder<T>();
27+
optionsBuilder.UseNpgsql(connectionString,
28+
builder =>
29+
builder.MigrationsHistoryTable(
30+
$"__EFMigrationsHistory_{schema}",
31+
schema));
32+
33+
return optionsBuilder;
34+
});
35+
36+
var contextOptions = services.BuildServiceProvider().GetRequiredService<DbContextOptionsBuilder<T>>();
37+
38+
using var activity = TelemetrySource.StartActivity("database migration");
39+
{
40+
Console.WriteLine("======= begin database migration =======");
41+
var sw = new Stopwatch();
42+
sw.Start();
43+
44+
using var context = contextFactory(contextOptions.Options);
45+
46+
context.Database.Migrate();
47+
48+
sw.Stop();
49+
Console.WriteLine($"======= end database migration ({sw.Elapsed.ToString()}) =======");
50+
}
51+
}
52+
53+
private static string PrepareConnectionString(string connectionString, bool replaceSllInstruction)
54+
{
55+
if (!connectionString.Contains("ApplicationName"))
56+
{
57+
var appName = Environment.GetEnvironmentVariable("ENV_INFO") ??
58+
Assembly.GetEntryAssembly()?.GetName().Name;
59+
60+
connectionString = connectionString.Last() != ';'
61+
? $"{connectionString};ApplicationName={appName}"
62+
: $"{connectionString}ApplicationName={appName}";
63+
}
64+
65+
if (replaceSllInstruction)
66+
connectionString = connectionString.Replace("Ssl Mode=Require", "Ssl Mode=VerifyFull");
67+
68+
return connectionString;
69+
}
70+
71+
public static void AddDatabaseWithoutMigrations<T>(this IServiceCollection services, string schema,
72+
string connectionString,
73+
bool replaceSllInstruction = true)
74+
where T : DbContext
75+
{
76+
AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true);
77+
78+
connectionString = PrepareConnectionString(connectionString, replaceSllInstruction);
79+
80+
services.AddSingleton(x =>
81+
{
82+
var optionsBuilder = new DbContextOptionsBuilder<T>();
83+
optionsBuilder.UseNpgsql(connectionString,
84+
builder =>
85+
builder.MigrationsHistoryTable(
86+
$"__EFMigrationsHistory_{schema}",
87+
schema));
88+
89+
return optionsBuilder;
90+
});
91+
}
92+
93+
public static PropertyBuilder<DateTime> SpecifyKindUtc<TEntity>(
94+
this EntityTypeBuilder<TEntity> builder,
95+
Expression<Func<TEntity, DateTime>> propertyExpression)
96+
where TEntity : class
97+
{
98+
var res = builder
99+
.Property(propertyExpression)
100+
.HasConversion(
101+
v => DateTime.SpecifyKind(v, DateTimeKind.Utc),
102+
v => DateTime.SpecifyKind(v, DateTimeKind.Utc));
103+
104+
return res;
105+
}
106+
}
107+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
2+
3+
namespace MyJetTools.Sdk.EfPostgres
4+
{
5+
// ReSharper disable once ClassNeverInstantiated.Global
6+
public class MyDateTimeConverterToUtc: ValueConverter<DateTime, DateTime>
7+
{
8+
public MyDateTimeConverterToUtc()
9+
: base(
10+
v => DateTime.SpecifyKind(v, DateTimeKind.Utc),
11+
v => DateTime.SpecifyKind(v, DateTimeKind.Utc))
12+
{ }
13+
}
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Logging;
3+
4+
namespace MyJetTools.Sdk.EfPostgres
5+
{
6+
public class MyDbContext : DbContext
7+
{
8+
public static ILoggerFactory? LoggerFactory { get; set; }
9+
10+
public MyDbContext(DbContextOptions options) : base(options)
11+
{
12+
}
13+
14+
protected MyDbContext()
15+
{
16+
}
17+
18+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
19+
{
20+
if (LoggerFactory != null)
21+
{
22+
optionsBuilder.UseLoggerFactory(LoggerFactory).EnableSensitiveDataLogging();
23+
}
24+
}
25+
26+
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
27+
{
28+
configurationBuilder.Properties<DateTime>().HaveConversion<MyDateTimeConverterToUtc>();
29+
}
30+
31+
}
32+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
4+
// ReSharper disable UnusedMember.Global
5+
6+
namespace MyJetTools.Sdk.EfPostgres
7+
{
8+
public class MyDesignTimeContextFactory<T> : IDesignTimeDbContextFactory<T> where T : MyDbContext
9+
{
10+
private readonly Func<DbContextOptions, T> _contextFactory;
11+
12+
public MyDesignTimeContextFactory(Func<DbContextOptions, T> contextFactory)
13+
{
14+
_contextFactory = contextFactory;
15+
}
16+
17+
public T CreateDbContext(string[] args)
18+
{
19+
var optionsBuilder = new DbContextOptionsBuilder<T>();
20+
optionsBuilder.UseNpgsql();
21+
22+
return _contextFactory(optionsBuilder.Options);
23+
}
24+
}
25+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.4">
11+
<PrivateAssets>all</PrivateAssets>
12+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13+
</PackageReference>
14+
<PackageReference Include="MyJetTools.Telemetry" Version="1.0.3" />
15+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.4" />
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)