| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Infrastructure.Persistence.EntityConfiguration.Bitcoin; |
| | | 5 | | |
| | | 6 | | using Domain.Channels.Constants; |
| | | 7 | | using Domain.Crypto.Constants; |
| | | 8 | | using Entities.Bitcoin; |
| | | 9 | | using Enums; |
| | | 10 | | using ValueConverters; |
| | | 11 | | |
| | | 12 | | public static class UtxoEntityConfiguration |
| | | 13 | | { |
| | | 14 | | public static void ConfigureUtxoEntity(this ModelBuilder modelBuilder, DatabaseType databaseType) |
| | | 15 | | { |
| | 0 | 16 | | modelBuilder.Entity<UtxoEntity>(entity => |
| | 0 | 17 | | { |
| | 0 | 18 | | // Set Primary Key |
| | 0 | 19 | | entity.HasKey(e => new { e.TransactionId, e.Index }); |
| | 0 | 20 | | |
| | 0 | 21 | | // Set Required props |
| | 0 | 22 | | entity.Property(e => e.AmountSats) |
| | 0 | 23 | | .IsRequired(); |
| | 0 | 24 | | entity.Property(e => e.BlockHeight) |
| | 0 | 25 | | .IsRequired(); |
| | 0 | 26 | | entity.Property(e => e.AddressIndex) |
| | 0 | 27 | | .IsRequired(); |
| | 0 | 28 | | entity.Property(e => e.IsAddressChange) |
| | 0 | 29 | | .IsRequired(); |
| | 0 | 30 | | entity.Property(e => e.AddressType) |
| | 0 | 31 | | .IsRequired(); |
| | 0 | 32 | | |
| | 0 | 33 | | // Set Optional props |
| | 0 | 34 | | entity.Property(e => e.LockedToChannelId) |
| | 0 | 35 | | .IsRequired(false) |
| | 0 | 36 | | .HasConversion<ChannelIdConverter>(); |
| | 0 | 37 | | entity.Property(e => e.UsedInTransactionId) |
| | 0 | 38 | | .IsRequired(false) |
| | 0 | 39 | | .HasConversion<TxIdConverter>(); |
| | 0 | 40 | | |
| | 0 | 41 | | // Set converters |
| | 0 | 42 | | entity.Property(x => x.TransactionId) |
| | 0 | 43 | | .HasConversion<TxIdConverter>(); |
| | 0 | 44 | | |
| | 0 | 45 | | // Set indexes |
| | 0 | 46 | | entity.HasIndex(x => x.AddressType); |
| | 0 | 47 | | entity.HasIndex(x => new { x.AddressIndex, x.IsAddressChange, x.AddressType }); |
| | 0 | 48 | | entity.HasIndex(x => x.LockedToChannelId); |
| | 0 | 49 | | entity.HasIndex(x => x.UsedInTransactionId); |
| | 0 | 50 | | |
| | 0 | 51 | | switch (databaseType) |
| | 0 | 52 | | { |
| | 0 | 53 | | case DatabaseType.MicrosoftSql: |
| | 0 | 54 | | OptimizeConfigurationForSqlServer(entity); |
| | 0 | 55 | | break; |
| | 0 | 56 | | case DatabaseType.PostgreSql: |
| | 0 | 57 | | OptimizeConfigurationForPostgres(entity); |
| | 0 | 58 | | break; |
| | 0 | 59 | | case DatabaseType.Sqlite: |
| | 0 | 60 | | default: |
| | 0 | 61 | | // Nothing to be done |
| | 0 | 62 | | break; |
| | 0 | 63 | | } |
| | 0 | 64 | | }); |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private static void OptimizeConfigurationForSqlServer(EntityTypeBuilder<UtxoEntity> entity) |
| | | 68 | | { |
| | 0 | 69 | | entity.Property(e => e.TransactionId).HasColumnType($"varbinary({CryptoConstants.Sha256HashLen})"); |
| | 0 | 70 | | entity.Property(e => e.LockedToChannelId).HasColumnType($"varbinary({ChannelConstants.ChannelIdLength})"); |
| | | 71 | | |
| | 0 | 72 | | entity.HasIndex(x => x.AddressType) |
| | 0 | 73 | | .IsCreatedOnline(); |
| | 0 | 74 | | entity.HasIndex(x => new { x.AddressIndex, x.IsAddressChange, x.AddressType }) |
| | 0 | 75 | | .IsCreatedOnline(); |
| | 0 | 76 | | entity.HasIndex(x => x.LockedToChannelId) |
| | 0 | 77 | | .IsCreatedOnline(); |
| | 0 | 78 | | entity.HasIndex(x => x.UsedInTransactionId) |
| | 0 | 79 | | .IsCreatedOnline(); |
| | 0 | 80 | | } |
| | | 81 | | |
| | | 82 | | private static void OptimizeConfigurationForPostgres(EntityTypeBuilder<UtxoEntity> entity) |
| | | 83 | | { |
| | 0 | 84 | | entity.HasIndex(x => x.AddressType) |
| | 0 | 85 | | .IsCreatedConcurrently(); |
| | 0 | 86 | | entity.HasIndex(x => new { x.AddressIndex, x.IsAddressChange, x.AddressType }) |
| | 0 | 87 | | .IsCreatedConcurrently(); |
| | 0 | 88 | | entity.HasIndex(x => x.LockedToChannelId) |
| | 0 | 89 | | .IsCreatedConcurrently(); |
| | 0 | 90 | | entity.HasIndex(x => x.UsedInTransactionId) |
| | 0 | 91 | | .IsCreatedConcurrently(); |
| | 0 | 92 | | } |
| | | 93 | | } |