| | 1 | | using Microsoft.EntityFrameworkCore; |
| | 2 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Persistence.EntityConfiguration.Bitcoin; |
| | 5 | |
|
| | 6 | | using Domain.Crypto.Constants; |
| | 7 | | using Entities.Bitcoin; |
| | 8 | | using Enums; |
| | 9 | | using ValueConverters; |
| | 10 | |
|
| | 11 | | public static class BlockchainStateEntityConfiguration |
| | 12 | | { |
| | 13 | | public static void ConfigureBlockchainStateEntity(this ModelBuilder modelBuilder, DatabaseType databaseType) |
| | 14 | | { |
| 0 | 15 | | modelBuilder.Entity<BlockchainStateEntity>(entity => |
| 0 | 16 | | { |
| 0 | 17 | | // Set PrimaryKey |
| 0 | 18 | | entity.HasKey(e => e.Id); |
| 0 | 19 | |
|
| 0 | 20 | | // Set required props |
| 0 | 21 | | entity.Property(e => e.LastProcessedHeight).IsRequired(); |
| 0 | 22 | | entity.Property(e => e.LastProcessedBlockHash) |
| 0 | 23 | | .HasConversion<HashConverter>() |
| 0 | 24 | | .IsRequired(); |
| 0 | 25 | | entity.Property(e => e.LastProcessedAt).IsRequired(); |
| 0 | 26 | |
|
| 0 | 27 | | // Configure Id as Guid |
| 0 | 28 | | entity.Property(e => e.Id) |
| 0 | 29 | | .HasConversion<Guid>() |
| 0 | 30 | | .IsRequired(); |
| 0 | 31 | |
|
| 0 | 32 | | if (databaseType == DatabaseType.MicrosoftSql) |
| 0 | 33 | | OptimizeConfigurationForSqlServer(entity); |
| 0 | 34 | | }); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | private static void OptimizeConfigurationForSqlServer(EntityTypeBuilder<BlockchainStateEntity> entity) |
| | 38 | | { |
| 0 | 39 | | entity.Property(e => e.LastProcessedBlockHash).HasColumnType($"varbinary({CryptoConstants.Sha256HashLen})"); |
| 0 | 40 | | } |
| | 41 | | } |