forked from WalletWasabi/WalletWasabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WalletWasabi#2 from Kukks/feat/remote-notifications
Refactor EF usage
- Loading branch information
Showing
12 changed files
with
187 additions
and
30 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
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
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
WalletWasabi.Backend/Data/APNTokensDesignTimeDbContextFactory.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,13 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace WalletWasabi.Backend.Data | ||
{ | ||
public class WasabiDesignTimeDbContextFactory : | ||
DesignTimeDbContextFactoryBase<WasabiBackendContext> | ||
{ | ||
protected override WasabiBackendContext CreateNewInstance(DbContextOptions<WasabiBackendContext> options) | ||
{ | ||
return new WasabiBackendContext(options); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
WalletWasabi.Backend/Data/DesignTimeDbContextFactoryBase.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,71 @@ | ||
using System; | ||
using System.IO; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Design; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace WalletWasabi.Backend.Data | ||
{ | ||
public abstract class DesignTimeDbContextFactoryBase<TContext> : | ||
IDesignTimeDbContextFactory<TContext> where TContext : DbContext | ||
{ | ||
|
||
public TContext CreateDbContext(string[] args) | ||
{ | ||
return Create( | ||
Directory.GetCurrentDirectory(), | ||
Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")); | ||
} | ||
|
||
protected abstract TContext CreateNewInstance( | ||
DbContextOptions<TContext> options); | ||
|
||
public TContext Create() | ||
{ | ||
var environmentName = | ||
Environment.GetEnvironmentVariable( | ||
"ASPNETCORE_ENVIRONMENT"); | ||
|
||
var basePath = AppContext.BaseDirectory; | ||
|
||
return Create(basePath, environmentName); | ||
} | ||
|
||
private TContext Create(string basePath, string environmentName) | ||
{ | ||
var builder = new ConfigurationBuilder() | ||
.SetBasePath(basePath) | ||
.AddJsonFile("appsettings.json") | ||
.AddJsonFile($"appsettings.{environmentName}.json", true) | ||
.AddEnvironmentVariables(); | ||
|
||
var config = builder.Build(); | ||
|
||
var connstr = config.GetConnectionString("User ID=postgres;Host=127.0.0.1;Port=65466;Database=doesntmatterbecauseitisnotactuallyused;"); | ||
return Create(connstr); | ||
} | ||
|
||
private TContext Create(string connectionString) | ||
{ | ||
if (string.IsNullOrEmpty(connectionString)) | ||
{ | ||
throw new ArgumentException( | ||
$"{nameof(connectionString)} is null or empty.", | ||
nameof(connectionString)); | ||
} | ||
|
||
var optionsBuilder = | ||
new DbContextOptionsBuilder<TContext>(); | ||
|
||
Console.WriteLine( | ||
"MyDesignTimeDbContextFactory.Create(string): Connection string: {0}", | ||
connectionString); | ||
|
||
optionsBuilder.UseNpgsql(connectionString); | ||
|
||
DbContextOptions<TContext> options = optionsBuilder.Options; | ||
|
||
return CreateNewInstance(options); | ||
} | ||
} | ||
} |
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,46 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WalletWasabi.Backend.Data | ||
{ | ||
public class MigrationStartupTask : IStartupTask | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly ILogger<MigrationStartupTask> _logger; | ||
|
||
public MigrationStartupTask(IServiceProvider serviceProvider, ILogger<MigrationStartupTask> logger) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_logger = logger; | ||
} | ||
|
||
public async Task ExecuteAsync(CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
// we cannot simply ask for this service to be injected automatically in the ctor as a dependency for | ||
// initializing IDbContextFactory<WasabiBackendContext> is the Global.Config object, which requires | ||
// another IStartupTask to run, but RunWithTasksAsync requires that all StartupTask services are loaded | ||
// at once | ||
var contextFactory = _serviceProvider.GetRequiredService<IDbContextFactory<WasabiBackendContext>>(); | ||
_logger.LogInformation($"Migrating database to latest version"); | ||
await using var context = contextFactory.CreateDbContext(); | ||
var pendingMigrations = await context.Database.GetPendingMigrationsAsync(cancellationToken); | ||
_logger.LogInformation(pendingMigrations.Any() | ||
? $"Running migrations: {string.Join(", ", pendingMigrations)}" | ||
: $"Database already at latest version"); | ||
await context.Database.MigrateAsync(cancellationToken); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error on the MigrationStartupTask"); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using WalletWasabi.Backend.Models; | ||
|
||
namespace WalletWasabi.Backend.Data | ||
{ | ||
public class WasabiBackendContext : DbContext | ||
{ | ||
public DbSet<AppleDeviceToken> Tokens { get; set; } | ||
|
||
public WasabiBackendContext(DbContextOptions<WasabiBackendContext> options) : base(options) | ||
{ | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
WalletWasabi.Backend/Migrations/20210719221453_InitialCreate.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,14 @@ | ||
version: "3" | ||
services: | ||
postgres: | ||
image: postgres:13.1 | ||
environment: | ||
POSTGRES_HOST_AUTH_METHOD: trust | ||
volumes: | ||
- "postgres_datadir:/var/lib/postgresql/data" | ||
ports: | ||
- "65466:5432" | ||
expose: | ||
- "5432" | ||
volumes: | ||
postgres_datadir: |