2020-06-29 04:44:35 +02:00
using System ;
2019-08-29 17:24:42 +02:00
using System.Linq ;
2023-02-10 03:43:46 +01:00
using System.Threading.Tasks ;
2017-09-13 08:47:34 +02:00
using Microsoft.AspNetCore.Identity.EntityFrameworkCore ;
using Microsoft.EntityFrameworkCore ;
2020-01-19 13:57:50 +01:00
using Microsoft.EntityFrameworkCore.Design ;
2017-09-27 15:09:59 +02:00
using Microsoft.EntityFrameworkCore.Infrastructure ;
2017-09-13 08:47:34 +02:00
namespace BTCPayServer.Data
{
2020-01-19 13:57:50 +01:00
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory < ApplicationDbContext >
2017-10-27 10:53:04 +02:00
{
2020-01-19 13:57:50 +01:00
public ApplicationDbContext CreateDbContext ( string [ ] args )
2017-10-27 10:53:04 +02:00
{
2020-05-28 21:38:17 +02:00
2020-01-19 13:57:50 +01:00
var builder = new DbContextOptionsBuilder < ApplicationDbContext > ( ) ;
2020-05-28 21:38:17 +02:00
2020-01-19 13:57:50 +01:00
builder . UseSqlite ( "Data Source=temp.db" ) ;
2020-05-28 21:38:17 +02:00
2020-01-19 13:57:50 +01:00
return new ApplicationDbContext ( builder . Options , true ) ;
2017-10-27 10:53:04 +02:00
}
2020-01-19 13:57:50 +01:00
}
2020-05-28 21:38:17 +02:00
2020-01-19 13:57:50 +01:00
public class ApplicationDbContext : IdentityDbContext < ApplicationUser >
{
private readonly bool _designTime ;
2019-12-15 13:58:16 +01:00
2020-01-19 13:57:50 +01:00
public ApplicationDbContext ( DbContextOptions < ApplicationDbContext > options , bool designTime = false )
2017-10-27 10:53:04 +02:00
: base ( options )
{
2020-01-19 13:57:50 +01:00
_designTime = designTime ;
2017-10-27 10:53:04 +02:00
}
2023-02-15 07:59:45 +01:00
#nullable enable
2023-02-10 03:43:46 +01:00
public async Task < string? > GetMigrationState ( )
{
return ( await Settings . FromSqlRaw ( "SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'" ) . AsNoTracking ( ) . FirstOrDefaultAsync ( ) ) ? . Value ;
}
2023-02-15 07:59:45 +01:00
#nullable restore
2020-12-28 23:32:15 +01:00
public DbSet < AddressInvoiceData > AddressInvoices { get ; set ; }
public DbSet < APIKeyData > ApiKeys { get ; set ; }
2020-05-28 21:38:17 +02:00
public DbSet < AppData > Apps { get ; set ; }
2022-05-18 07:59:56 +02:00
public DbSet < CustodianAccountData > CustodianAccount { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < StoredFile > Files { get ; set ; }
2020-05-28 21:38:17 +02:00
public DbSet < InvoiceEventData > InvoiceEvents { get ; set ; }
2020-12-28 23:38:02 +01:00
public DbSet < InvoiceSearchData > InvoiceSearches { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < InvoiceWebhookDeliveryData > InvoiceWebhookDeliveries { get ; set ; }
public DbSet < InvoiceData > Invoices { get ; set ; }
public DbSet < NotificationData > Notifications { get ; set ; }
2020-04-16 07:25:52 +02:00
public DbSet < OffchainTransactionData > OffchainTransactions { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < PairedSINData > PairedSINData { get ; set ; }
public DbSet < PairingCodeData > PairingCodes { get ; set ; }
public DbSet < PayjoinLock > PayjoinLocks { get ; set ; }
2020-05-28 21:38:17 +02:00
public DbSet < PaymentRequestData > PaymentRequests { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < PaymentData > Payments { get ; set ; }
2020-06-24 03:34:09 +02:00
public DbSet < PayoutData > Payouts { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < PendingInvoiceData > PendingInvoices { get ; set ; }
public DbSet < PlannedTransaction > PlannedTransactions { get ; set ; }
public DbSet < PullPaymentData > PullPayments { get ; set ; }
public DbSet < RefundData > Refunds { get ; set ; }
2020-05-28 21:38:17 +02:00
public DbSet < SettingData > Settings { get ; set ; }
2022-06-13 06:36:47 +02:00
public DbSet < StoreSettingData > StoreSettings { get ; set ; }
2020-11-06 12:42:26 +01:00
public DbSet < StoreWebhookData > StoreWebhooks { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < StoreData > Stores { get ; set ; }
public DbSet < U2FDevice > U2FDevices { get ; set ; }
2021-04-20 07:06:32 +02:00
public DbSet < Fido2Credential > Fido2Credentials { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < UserStore > UserStore { get ; set ; }
2023-05-26 16:49:32 +02:00
public DbSet < StoreRole > StoreRoles { get ; set ; }
2022-10-11 10:34:29 +02:00
[Obsolete]
2020-12-28 23:32:15 +01:00
public DbSet < WalletData > Wallets { get ; set ; }
2022-10-11 10:34:29 +02:00
public DbSet < WalletObjectData > WalletObjects { get ; set ; }
public DbSet < WalletObjectLinkData > WalletObjectLinks { get ; set ; }
[Obsolete]
2020-12-28 23:32:15 +01:00
public DbSet < WalletTransactionData > WalletTransactions { get ; set ; }
2020-11-06 12:42:26 +01:00
public DbSet < WebhookDeliveryData > WebhookDeliveries { get ; set ; }
2020-12-28 23:32:15 +01:00
public DbSet < WebhookData > Webhooks { get ; set ; }
2023-01-06 14:18:07 +01:00
public DbSet < LightningAddressData > LightningAddresses { get ; set ; }
2022-04-24 05:19:34 +02:00
public DbSet < PayoutProcessorData > PayoutProcessors { get ; set ; }
2023-02-20 11:35:54 +01:00
public DbSet < FormData > Forms { get ; set ; }
2020-11-06 12:42:26 +01:00
2017-10-27 10:53:04 +02:00
protected override void OnConfiguring ( DbContextOptionsBuilder optionsBuilder )
{
var isConfigured = optionsBuilder . Options . Extensions . OfType < RelationalOptionsExtension > ( ) . Any ( ) ;
if ( ! isConfigured )
optionsBuilder . UseSqlite ( "Data Source=temp.db" ) ;
}
protected override void OnModelCreating ( ModelBuilder builder )
{
base . OnModelCreating ( builder ) ;
2020-12-28 23:32:15 +01:00
// some of the data models don't have OnModelCreating for now, commenting them
2023-02-21 07:06:34 +01:00
ApplicationUser . OnModelCreating ( builder , Database ) ;
2020-12-28 23:32:15 +01:00
AddressInvoiceData . OnModelCreating ( builder ) ;
2023-02-21 07:06:34 +01:00
APIKeyData . OnModelCreating ( builder , Database ) ;
2020-06-27 14:55:16 +02:00
AppData . OnModelCreating ( builder ) ;
2023-02-21 07:06:34 +01:00
CustodianAccountData . OnModelCreating ( builder , Database ) ;
2020-12-28 23:32:15 +01:00
//StoredFile.OnModelCreating(builder);
2020-06-27 14:55:16 +02:00
InvoiceEventData . OnModelCreating ( builder ) ;
2020-12-28 23:32:15 +01:00
InvoiceSearchData . OnModelCreating ( builder ) ;
InvoiceWebhookDeliveryData . OnModelCreating ( builder ) ;
2023-02-21 07:06:34 +01:00
InvoiceData . OnModelCreating ( builder , Database ) ;
NotificationData . OnModelCreating ( builder , Database ) ;
2020-12-28 23:32:15 +01:00
//OffchainTransactionData.OnModelCreating(builder);
2022-04-24 05:19:34 +02:00
BTCPayServer . Data . PairedSINData . OnModelCreating ( builder ) ;
2020-12-28 23:32:15 +01:00
PairingCodeData . OnModelCreating ( builder ) ;
//PayjoinLock.OnModelCreating(builder);
2023-02-21 07:06:34 +01:00
PaymentRequestData . OnModelCreating ( builder , Database ) ;
PaymentData . OnModelCreating ( builder , Database ) ;
2020-06-24 03:34:09 +02:00
PayoutData . OnModelCreating ( builder ) ;
2020-12-28 23:32:15 +01:00
PendingInvoiceData . OnModelCreating ( builder ) ;
//PlannedTransaction.OnModelCreating(builder);
PullPaymentData . OnModelCreating ( builder ) ;
2020-06-24 10:51:00 +02:00
RefundData . OnModelCreating ( builder ) ;
2022-11-28 12:36:18 +01:00
SettingData . OnModelCreating ( builder , Database ) ;
2022-06-13 06:36:47 +02:00
StoreSettingData . OnModelCreating ( builder , Database ) ;
2020-12-28 23:32:15 +01:00
StoreWebhookData . OnModelCreating ( builder ) ;
2022-11-28 12:36:18 +01:00
StoreData . OnModelCreating ( builder , Database ) ;
2020-10-06 17:56:55 +02:00
U2FDevice . OnModelCreating ( builder ) ;
2023-02-21 07:06:34 +01:00
Fido2Credential . OnModelCreating ( builder , Database ) ;
2022-04-24 05:19:34 +02:00
BTCPayServer . Data . UserStore . OnModelCreating ( builder ) ;
2020-12-28 23:32:15 +01:00
//WalletData.OnModelCreating(builder);
2022-10-11 10:34:29 +02:00
WalletObjectData . OnModelCreating ( builder , Database ) ;
WalletObjectLinkData . OnModelCreating ( builder , Database ) ;
#pragma warning disable CS0612 // Type or member is obsolete
2020-12-28 23:32:15 +01:00
WalletTransactionData . OnModelCreating ( builder ) ;
2022-10-11 10:34:29 +02:00
#pragma warning restore CS0612 // Type or member is obsolete
2023-04-10 04:07:03 +02:00
WebhookDeliveryData . OnModelCreating ( builder , Database ) ;
LightningAddressData . OnModelCreating ( builder , Database ) ;
PayoutProcessorData . OnModelCreating ( builder , Database ) ;
WebhookData . OnModelCreating ( builder , Database ) ;
FormData . OnModelCreating ( builder , Database ) ;
2023-05-26 16:49:32 +02:00
StoreRole . OnModelCreating ( builder , Database ) ;
2020-11-06 12:42:26 +01:00
2023-04-10 04:07:03 +02:00
if ( Database . IsSqlite ( ) & & ! _designTime )
2020-03-09 09:44:44 +01:00
{
// SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
// here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
// To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset
// use the DateTimeOffsetToBinaryConverter
// Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
// This only supports millisecond precision, but should be sufficient for most use cases.
foreach ( var entityType in builder . Model . GetEntityTypes ( ) )
{
var properties = entityType . ClrType . GetProperties ( ) . Where ( p = > p . PropertyType = = typeof ( DateTimeOffset ) ) ;
foreach ( var property in properties )
{
builder
. Entity ( entityType . Name )
. Property ( property . Name )
. HasConversion ( new Microsoft . EntityFrameworkCore . Storage . ValueConversion . DateTimeOffsetToBinaryConverter ( ) ) ;
}
}
}
2017-10-27 10:53:04 +02:00
}
}
2019-05-02 14:01:08 +02:00
2017-09-13 08:47:34 +02:00
}