Merge pull request #839 from vanvonlj/upstream-pr/assume-externally-provisioned-database
Support externally-provisioned databases (opt-in, no drop/create) (cherry picked from commit 2e117c26764483fb4ee3de9540c9bf7a647d8067)
This commit is contained in:
@@ -152,21 +152,30 @@ public class PersistenceContextProvider : IMigratableDatabaseContextProvider
|
||||
/// <summary>
|
||||
/// Recreates the database by deleting and creating it again.
|
||||
/// </summary>
|
||||
/// <param name="dropExistingDatabase">
|
||||
/// If <see langword="true"/> (the default), the database is dropped and created again from scratch.
|
||||
/// If <see langword="false"/>, the existing database is kept and only its schema is built via
|
||||
/// migrations — required when the database is provisioned externally and the connecting role is
|
||||
/// not permitted to create or drop databases.
|
||||
/// </param>
|
||||
/// <returns>The disposable that should be disposed of when the data creation process is finished.</returns>
|
||||
public async Task<IDisposable> ReCreateDatabaseAsync()
|
||||
public async Task<IDisposable> ReCreateDatabaseAsync(bool dropExistingDatabase = true)
|
||||
{
|
||||
var changePublisher = this._changeListener;
|
||||
this._changeListener = null;
|
||||
try
|
||||
{
|
||||
try
|
||||
if (dropExistingDatabase)
|
||||
{
|
||||
await using var installationContext = new EntityDataContext();
|
||||
await installationContext.Database.EnsureDeletedAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (NpgsqlException)
|
||||
{
|
||||
// That's expected for a fresh database
|
||||
try
|
||||
{
|
||||
await using var installationContext = new EntityDataContext();
|
||||
await installationContext.Database.EnsureDeletedAsync().ConfigureAwait(false);
|
||||
}
|
||||
catch (NpgsqlException)
|
||||
{
|
||||
// That's expected for a fresh database
|
||||
}
|
||||
}
|
||||
|
||||
await this.ApplyAllPendingUpdatesAsync().ConfigureAwait(false);
|
||||
|
||||
@@ -63,8 +63,15 @@ public interface IMigratableDatabaseContextProvider : IPersistenceContextProvide
|
||||
/// <summary>
|
||||
/// Recreates the database by deleting and creating it again.
|
||||
/// </summary>
|
||||
/// <param name="dropExistingDatabase">
|
||||
/// If <see langword="true"/> (the default), the database is dropped and created again from scratch.
|
||||
/// If <see langword="false"/>, the existing database is kept and only its schema is built via
|
||||
/// migrations. Set this to <see langword="false"/> when the database is provisioned externally
|
||||
/// (e.g. by a Kubernetes operator, infrastructure-as-code, or a managed cloud database) and the
|
||||
/// connecting role is not permitted to create or drop databases.
|
||||
/// </param>
|
||||
/// <returns>The disposable which should be disposed when the data creation process is finished.</returns>
|
||||
Task<IDisposable> ReCreateDatabaseAsync();
|
||||
Task<IDisposable> ReCreateDatabaseAsync(bool dropExistingDatabase = true);
|
||||
|
||||
/// <summary>
|
||||
/// Resets the cache of this instance.
|
||||
|
||||
@@ -127,7 +127,7 @@ public class InMemoryPersistenceContextProvider : IMigratableDatabaseContextProv
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<IDisposable> ReCreateDatabaseAsync()
|
||||
public Task<IDisposable> ReCreateDatabaseAsync(bool dropExistingDatabase = true)
|
||||
{
|
||||
this._repositoryProvider = new();
|
||||
return Task.FromResult<IDisposable>(new Disposable(() => { }));
|
||||
|
||||
Reference in New Issue
Block a user