Skip to content

Commit 45f38f0

Browse files
committed
Call NpgsqlDataSource.Clear when doing EnsureDeleted
Fixes #4499
1 parent 342c560 commit 45f38f0

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<EFCoreVersion>[9.0.0-rc.2.24474.1]</EFCoreVersion>
44
<MicrosoftExtensionsVersion>9.0.0-rc.2.24473.5</MicrosoftExtensionsVersion>
5-
<NpgsqlVersion>9.0.0-preview.1-ci.20241025T100626</NpgsqlVersion>
5+
<NpgsqlVersion>9.0.0-preview.1-ci.20241028T080028</NpgsqlVersion>
66
</PropertyGroup>
77

88
<ItemGroup>

src/EFCore.PG/Storage/Internal/INpgsqlRelationalConnection.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
1+
using System.Data.Common;
2+
3+
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
24

35
/// <summary>
46
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -8,6 +10,14 @@
810
/// </summary>
911
public interface INpgsqlRelationalConnection : IRelationalConnection
1012
{
13+
/// <summary>
14+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
15+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
16+
/// any release. You should only use it directly in your code with extreme caution and knowing that
17+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
18+
/// </summary>
19+
DbDataSource? DataSource { get; }
20+
1121
/// <summary>
1222
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
1323
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

src/EFCore.PG/Storage/Internal/NpgsqlDatabaseCreator.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,15 @@ private static bool IsDoesNotExist(PostgresException exception)
273273
/// </summary>
274274
public override void Delete()
275275
{
276-
ClearAllPools();
276+
switch (_connection.DataSource)
277+
{
278+
case NpgsqlDataSource dataSource:
279+
dataSource.Clear();
280+
break;
281+
case null:
282+
ClearAllPools();
283+
break;
284+
}
277285

278286
using (var masterConnection = _connection.CreateAdminConnection())
279287
{
@@ -290,7 +298,16 @@ public override void Delete()
290298
/// </summary>
291299
public override async Task DeleteAsync(CancellationToken cancellationToken = default)
292300
{
293-
ClearAllPools();
301+
switch (_connection.DataSource)
302+
{
303+
case NpgsqlDataSource dataSource:
304+
// TODO: Do this asynchronously once https://github.com/npgsql/npgsql/issues/4499 is done
305+
dataSource.Clear();
306+
break;
307+
case null:
308+
ClearAllPools();
309+
break;
310+
}
294311

295312
var masterConnection = _connection.CreateAdminConnection();
296313
await using (masterConnection)

src/EFCore.PG/Storage/Internal/NpgsqlRelationalConnection.cs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ public class NpgsqlRelationalConnection : RelationalConnection, INpgsqlRelationa
2121
private readonly ProvidePasswordCallback? _providePasswordCallback;
2222
#pragma warning restore CS0618
2323

24-
private DbDataSource? _dataSource;
24+
/// <summary>
25+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
26+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
27+
/// any release. You should only use it directly in your code with extreme caution and knowing that
28+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
29+
/// </summary>
30+
public DbDataSource? DataSource { get; private set; }
2531

2632
/// <summary>
2733
/// Indicates whether the store connection supports ambient transactions
@@ -58,7 +64,7 @@ protected NpgsqlRelationalConnection(RelationalConnectionDependencies dependenci
5864
{
5965
if (dataSource is not null)
6066
{
61-
_dataSource = dataSource;
67+
DataSource = dataSource;
6268

6369
#if DEBUG
6470
// We validate in NpgsqlOptionsExtensions.Validate that DataSource and these callbacks aren't specified together
@@ -92,9 +98,9 @@ protected NpgsqlRelationalConnection(RelationalConnectionDependencies dependenci
9298
/// </summary>
9399
protected override DbConnection CreateDbConnection()
94100
{
95-
if (_dataSource is not null)
101+
if (DataSource is not null)
96102
{
97-
return _dataSource.CreateConnection();
103+
return DataSource.CreateConnection();
98104
}
99105

100106
var conn = new NpgsqlConnection(ConnectionString);
@@ -132,12 +138,12 @@ protected override DbConnection CreateDbConnection()
132138
// TODO: Remove after DbDataSource support is added to EF Core (https://github.com/dotnet/efcore/issues/28266)
133139
public override string? ConnectionString
134140
{
135-
get => _dataSource is null ? base.ConnectionString : _dataSource.ConnectionString;
141+
get => DataSource is null ? base.ConnectionString : DataSource.ConnectionString;
136142
set
137143
{
138144
base.ConnectionString = value;
139145

140-
_dataSource = null;
146+
DataSource = null;
141147
}
142148
}
143149

@@ -155,7 +161,7 @@ public override string? ConnectionString
155161
{
156162
base.DbConnection = value;
157163

158-
_dataSource = null;
164+
DataSource = null;
159165
}
160166
}
161167

@@ -167,12 +173,12 @@ public override string? ConnectionString
167173
/// </summary>
168174
public virtual DbDataSource? DbDataSource
169175
{
170-
get => _dataSource;
176+
get => DataSource;
171177
set
172178
{
173179
DbConnection = null;
174180
ConnectionString = null;
175-
_dataSource = value;
181+
DataSource = value;
176182
}
177183
}
178184

@@ -196,7 +202,7 @@ public virtual INpgsqlRelationalConnection CreateAdminConnection()
196202
Multiplexing = false
197203
}.ToString();
198204

199-
var adminNpgsqlOptions = _dataSource is not null
205+
var adminNpgsqlOptions = DataSource is not null
200206
? npgsqlOptions.WithConnection(((NpgsqlConnection)CreateDbConnection()).CloneWith(adminConnectionString))
201207
: npgsqlOptions.Connection is not null
202208
? npgsqlOptions.WithConnection(DbConnection.CloneWith(adminConnectionString))

0 commit comments

Comments
 (0)