-
Notifications
You must be signed in to change notification settings - Fork 99
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
1 parent
fef758c
commit 9698c10
Showing
17 changed files
with
490 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 @@ | ||
**/.dockerignore | ||
**/.env | ||
**/.git | ||
**/.gitignore | ||
**/.project | ||
**/.settings | ||
**/.toolstarget | ||
**/.vs | ||
**/.vscode | ||
**/.idea | ||
**/*.*proj.user | ||
**/*.dbmdl | ||
**/*.jfm | ||
**/azds.yaml | ||
**/bin | ||
**/charts | ||
**/docker-compose* | ||
**/Dockerfile* | ||
**/node_modules | ||
**/npm-debug.log | ||
**/obj | ||
**/secrets.dev.yaml | ||
**/values.dev.yaml | ||
LICENSE | ||
README.md |
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 @@ | ||
stages: | ||
- build | ||
- test | ||
|
||
job1: | ||
stage: build | ||
script: | ||
- echo "This job runs in the build stage." | ||
|
||
last-job: | ||
stage: .post | ||
script: | ||
- echo "This job runs in the .post stage, after all other stages." | ||
|
||
job2: | ||
stage: test | ||
script: | ||
- echo "This job runs in the test stage." |
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,8 @@ | ||
namespace Lab.Snapshot.DB; | ||
|
||
public class Account | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Type { get; set; } | ||
} |
13 changes: 13 additions & 0 deletions
13
Shnapshot/Lab.Snapshot/Lab.Snapshot.DB/Lab.Snapshot.DB.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,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" /> | ||
</ItemGroup> | ||
|
||
</Project> |
35 changes: 35 additions & 0 deletions
35
Shnapshot/Lab.Snapshot/Lab.Snapshot.DB/MemberDataEntity.cs
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,35 @@ | ||
namespace Lab.Snapshot.DB; | ||
|
||
public class MemberDataEntity | ||
{ | ||
public string Id { get; set; } | ||
|
||
public Profile Profile { get; set; } | ||
|
||
public List<Account> Accounts { get; set; } | ||
|
||
public DateTimeOffset CreatedAt { get; set; } | ||
|
||
public string CreatedBy { get; set; } | ||
|
||
public DateTimeOffset UpdatedAt { get; set; } | ||
|
||
public string UpdatedBy { get; set; } | ||
|
||
public int Version { get; set; } | ||
|
||
public Dictionary<string, object> ToDictionary() | ||
{ | ||
return new Dictionary<string, object> | ||
{ | ||
{ nameof(this.Id), this.Id }, | ||
{ nameof(this.Profile), this.Profile }, | ||
{ nameof(this.Accounts), this.Accounts }, | ||
{ nameof(this.CreatedAt), this.CreatedAt }, | ||
{ nameof(this.CreatedBy), this.CreatedBy }, | ||
{ nameof(this.UpdatedAt), this.UpdatedAt }, | ||
{ nameof(this.UpdatedBy), this.UpdatedBy }, | ||
{ nameof(this.Version), this.Version } | ||
}; | ||
} | ||
} |
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,69 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lab.Snapshot.DB; | ||
|
||
public class MemberDbContext : DbContext | ||
{ | ||
public DbSet<MemberDataEntity> Members { get; set; } | ||
|
||
public DbSet<SnapshotDataEntity> Snapshots { get; set; } | ||
|
||
public MemberDbContext(DbContextOptions<MemberDbContext> options) | ||
: base(options) | ||
{ | ||
} | ||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
|
||
modelBuilder.HasSequence("member_collection_seqno") | ||
.StartsAt(1) | ||
.IncrementsBy(1); | ||
|
||
modelBuilder.ApplyConfiguration(new MemberConfiguration()); | ||
} | ||
|
||
internal class MemberConfiguration : IEntityTypeConfiguration<MemberDataEntity> | ||
{ | ||
public void Configure(EntityTypeBuilder<MemberDataEntity> builder) | ||
{ | ||
builder.ToTable("Member"); | ||
builder.HasKey(x => x.Id); | ||
builder.Property(x => x.Accounts).HasColumnType("jsonb").IsRequired(); | ||
builder.Property(x => x.Profile).HasColumnType("jsonb").IsRequired(false); | ||
builder.Property(x => x.CreatedAt).HasColumnType("timestamp with time zone").IsRequired(); | ||
builder.Property(x => x.CreatedBy).HasMaxLength(50).IsRequired(); | ||
builder.Property(x => x.UpdatedAt).HasColumnType("timestamp with time zone").IsRequired(); | ||
builder.Property(x => x.UpdatedBy).HasMaxLength(50).IsRequired(); | ||
builder.Property(p => p.Version).IsRequired(); | ||
|
||
// indexes | ||
builder.HasIndex(p => new { p.Id, p.Version }).IsUnique(); | ||
builder.HasIndex(x => x.Accounts).HasMethod("GIN"); | ||
} | ||
} | ||
|
||
internal class SnapshotConfiguration : IEntityTypeConfiguration<SnapshotDataEntity> | ||
{ | ||
public void Configure(EntityTypeBuilder<SnapshotDataEntity> builder) | ||
{ | ||
builder.ToTable("Snapshot"); | ||
builder.HasKey(x => x.Id); | ||
builder.Property(x => x.Data).HasColumnType("jsonb").IsRequired(); | ||
builder.Property(x => x.Type).IsRequired(); | ||
builder.Property(x => x.CreatedAt).HasColumnType("timestamp with time zone").IsRequired(); | ||
builder.Property(x => x.CreatedBy).HasMaxLength(50).IsRequired(); | ||
builder.Property(x => x.UpdatedAt).HasColumnType("timestamp with time zone").IsRequired(); | ||
builder.Property(x => x.UpdatedBy).HasMaxLength(50).IsRequired(); | ||
builder.Property(p => p.Version).IsRequired(); | ||
|
||
// indexes | ||
builder.HasIndex(p => new { p.Id, p.Version }).IsUnique(); | ||
builder.HasIndex(x => x.Data).HasMethod("GIN"); | ||
} | ||
} | ||
|
||
} |
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,8 @@ | ||
namespace Lab.Snapshot.DB; | ||
|
||
public class Profile | ||
{ | ||
public int Age { get; set; } | ||
|
||
public string Name { get; set; } | ||
} |
24 changes: 24 additions & 0 deletions
24
Shnapshot/Lab.Snapshot/Lab.Snapshot.DB/SnapshotDataEntity.cs
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,24 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.Text.Json; | ||
|
||
namespace Lab.Snapshot.DB; | ||
|
||
public class SnapshotDataEntity | ||
{ | ||
public string Id { get; set; } | ||
|
||
public string Type { get; set; } | ||
|
||
[Column(TypeName = "jsonb")] | ||
public Dictionary<string, object> Data { get; set; } = new(); | ||
|
||
public DateTimeOffset CreatedAt { get; set; } | ||
|
||
public string CreatedBy { get; set; } | ||
|
||
public DateTimeOffset UpdatedAt { get; set; } | ||
|
||
public string UpdatedBy { get; set; } | ||
|
||
public int Version { get; set; } | ||
} |
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,6 @@ | ||
namespace Lab.Snapshot.Test; | ||
|
||
class EnvironmentNames | ||
{ | ||
public const string DbConnectionString = "DB_CONNECTION_STRING"; | ||
} |
32 changes: 32 additions & 0 deletions
32
Shnapshot/Lab.Snapshot/Lab.Snapshot.Test/Lab.Snapshot.Test.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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2"/> | ||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10"/> | ||
<PackageReference Include="MSTest.TestFramework" Version="2.2.10"/> | ||
<PackageReference Include="coverlet.collector" Version="3.1.2"/> | ||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" /> | ||
<!-- <PackageReference Include="Npgsql" Version="7.0.4" />--> | ||
<PackageReference Include="Serilog.Extensions.Hosting" Version="7.0.0" /> | ||
<!-- <PackageReference Include="Testcontainers.PostgreSql" Version="3.5.0" />--> | ||
<!-- <PackageReference Include="Testcontainers.Redis" Version="3.5.0" />--> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Lab.Snapshot.DB\Lab.Snapshot.DB.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Lab.Snapshot.DB\Lab.Snapshot.DB.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
28 changes: 28 additions & 0 deletions
28
Shnapshot/Lab.Snapshot/Lab.Snapshot.Test/NpgsqlGenerateScript.cs
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,28 @@ | ||
namespace Lab.Snapshot.Test; | ||
|
||
internal class NpgsqlGenerateScript | ||
{ | ||
public static string ClearAllRecord() | ||
{ | ||
return @" | ||
DO $$ | ||
DECLARE row RECORD; | ||
BEGIN | ||
FOR row IN SELECT table_name | ||
FROM information_schema.tables | ||
WHERE table_type='BASE TABLE' | ||
AND table_schema='public' | ||
AND table_name NOT IN ('admins', 'admin_roles', '__EFMigrationsHistory') | ||
LOOP | ||
EXECUTE format('TRUNCATE TABLE %I CONTINUE IDENTITY CASCADE;', row.table_name); | ||
END LOOP; | ||
END; | ||
$$; | ||
"; | ||
} | ||
|
||
public static string ReseedMemberCollectionSeq() | ||
{ | ||
return "ALTER SEQUENCE member_collection_seqno RESTART WITH 1;"; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
Shnapshot/Lab.Snapshot/Lab.Snapshot.Test/ServiceConfiguration.cs
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,24 @@ | ||
using Lab.Snapshot.DB; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Lab.Snapshot.Test; | ||
|
||
public class ServiceConfiguration | ||
{ | ||
public static void ConfigDb(IServiceCollection services) | ||
{ | ||
services.AddSingleton(p => { return LoggerFactory.Create(builder => { builder.AddConsole(); }); }); | ||
services.AddDbContextFactory<MemberDbContext>((p, options) => | ||
{ | ||
var connectionString = Environment.GetEnvironmentVariable(EnvironmentNames.DbConnectionString); | ||
options.UseNpgsql(connectionString, | ||
builder => builder.EnableRetryOnFailure( | ||
10, | ||
TimeSpan.FromSeconds(30), | ||
new List<string> { "57P01" })) | ||
; | ||
}); | ||
} | ||
} |
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,13 @@ | ||
using Npgsql; | ||
|
||
namespace Lab.Snapshot.Test; | ||
|
||
public class TestAssistant | ||
{ | ||
public const string DbConnectionString = | ||
"Host=localhost;Port=5432;Database=member_integration_test;Username=postgres;Password=guest"; | ||
|
||
public static DateTimeOffset Now { get; set; } = DateTimeOffset.UtcNow; | ||
|
||
public static string UserId { get; set; } = "@@TestUser@@"; | ||
} |
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,36 @@ | ||
using Lab.Snapshot.DB; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Lab.Snapshot.Test; | ||
|
||
[TestClass] | ||
public class TestHook | ||
{ | ||
private static readonly IServiceCollection s_services = new ServiceCollection(); | ||
static IServiceProvider s_serviceProvider; | ||
|
||
[AssemblyInitialize] | ||
public static void AssemblyInitialize(TestContext context) | ||
{ | ||
Console.WriteLine("AssemblyInitialize"); | ||
Environment.SetEnvironmentVariable(EnvironmentNames.DbConnectionString, TestAssistant.DbConnectionString); | ||
ServiceConfiguration.ConfigDb(s_services); | ||
s_serviceProvider = s_services.BuildServiceProvider(); | ||
using var dbContext = s_serviceProvider.GetService<IDbContextFactory<MemberDbContext>>().CreateDbContext(); | ||
|
||
// drop and create database | ||
dbContext.Database.EnsureDeleted(); | ||
dbContext.Database.EnsureCreated(); | ||
} | ||
|
||
[AssemblyCleanup] | ||
public static void AssemblyCleanup() | ||
{ | ||
Console.WriteLine("AssemblyCleanup"); | ||
|
||
// drop database | ||
using var dbContext = s_serviceProvider.GetService<IDbContextFactory<MemberDbContext>>().CreateDbContext(); | ||
dbContext.Database.EnsureDeleted(); | ||
} | ||
} |
Oops, something went wrong.