Skip to content

Commit 3a6a7c1

Browse files
committed
v0.2.3
1 parent 0cc6d2d commit 3a6a7c1

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

src/NuExt.System.Data.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ System.Data.DataTableExtensions
1616
System.Data.DalBase
1717
System.Data.DbConverter<TDbConnection>
1818
System.Data.IDbContext</Description>
19-
<Version>0.2.2</Version>
19+
<Version>0.2.3</Version>
2020
<RootNamespace />
2121
<GenerateDocumentationFile>True</GenerateDocumentationFile>
2222
<NoWarn>$(NoWarn);1591</NoWarn>

src/System/Data/DbConverter.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,29 @@ public abstract class DbConverter<TDbConnection> where TDbConnection : IDbConnec
2323
/// Retrieves the current version of the database schema from the specified database connection.
2424
/// </summary>
2525
/// <param name="connection">The database connection to use for retrieving the version.</param>
26+
/// <param name="cancellationToken">Cancellation token to cancel the execution.</param>
2627
/// <returns>The current version of the database schema.</returns>
27-
public abstract Version GetDbVersion(TDbConnection connection);
28+
public abstract Version GetDbVersion(TDbConnection connection, CancellationToken cancellationToken);
2829

2930
/// <summary>
3031
/// Determines whether the database requires an update to match the version specified by this converter.
3132
/// </summary>
3233
/// <param name="connection">The database connection to check.</param>
34+
/// <param name="cancellationToken">Cancellation token to cancel the execution.</param>
3335
/// <returns><c>true</c> if the database requires an update; otherwise, <c>false</c>.</returns>
34-
public virtual bool RequiresUpdate(TDbConnection connection)
36+
public virtual bool RequiresUpdate(TDbConnection connection, CancellationToken cancellationToken)
3537
{
36-
Version dbVersion = GetDbVersion(connection);
38+
Version dbVersion = GetDbVersion(connection, cancellationToken);
3739
return dbVersion < Version;
3840
}
3941

4042
/// <summary>
4143
/// Applies the necessary updates to the database to bring it in line with the version specified by this converter.
4244
/// </summary>
4345
/// <param name="connection">The database connection to update.</param>
46+
/// <param name="cancellationToken">Cancellation token to cancel the execution.</param>
4447
/// <returns><c>true</c> if the update was successful; otherwise, <c>false</c>.</returns>
45-
public abstract bool Update(TDbConnection connection);
48+
public abstract bool Update(TDbConnection connection, CancellationToken cancellationToken);
4649

4750
#endregion
4851
}

src/System/Data/DbConverterExtensions.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace System.Data
44
{
55
public static class DbConverterExtensions
66
{
7-
private static void CheckDbVersion<TDbConnection>(TDbConnection connection, Func<TDbConnection, Version> getDbVersion, Version dbVersion) where TDbConnection : IDbConnection
7+
private static void CheckDbVersion<TDbConnection>(TDbConnection connection, Func<TDbConnection, CancellationToken, Version> getDbVersion, Version dbVersion, CancellationToken cancellationToken) where TDbConnection : IDbConnection
88
{
99
#if NET6_0_OR_GREATER
1010
ArgumentNullException.ThrowIfNull(connection);
@@ -15,14 +15,14 @@ private static void CheckDbVersion<TDbConnection>(TDbConnection connection, Func
1515
Throw.IfNull(getDbVersion);
1616
Throw.IfNull(dbVersion);
1717
#endif
18-
var version = getDbVersion(connection);
18+
var version = getDbVersion(connection, cancellationToken);
1919
if (version > dbVersion)
2020
{
2121
throw new DataException("File created in a later version of the program. Install the new version to open this file.");
2222
}
2323
}
2424

25-
public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, IDbContext context) where TDbConnection : IDbConnection
25+
public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, IDbContext context, CancellationToken cancellationToken) where TDbConnection : IDbConnection
2626
{
2727
#if NET6_0_OR_GREATER
2828
ArgumentNullException.ThrowIfNull(converters);
@@ -31,10 +31,10 @@ public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbC
3131
Throw.IfNull(converters);
3232
Throw.IfNull(context);
3333
#endif
34-
converters.Initialize((TDbConnection)context.Connection);
34+
converters.Initialize((TDbConnection)context.Connection, cancellationToken);
3535
}
3636

37-
public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection) where TDbConnection : IDbConnection
37+
public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection, CancellationToken cancellationToken) where TDbConnection : IDbConnection
3838
{
3939
#if NET6_0_OR_GREATER
4040
ArgumentNullException.ThrowIfNull(converters);
@@ -57,15 +57,15 @@ public static void Initialize<TDbConnection>(this IReadOnlyList<DbConverter<TDbC
5757
#else
5858
var dbVersion = converters[^1].Version;
5959
#endif
60-
if (converters.RequiresUpdate(connection))
60+
if (converters.RequiresUpdate(connection, cancellationToken))
6161
{
62-
bool result = converters.Update(connection, dbVersion);
62+
bool result = converters.Update(connection, dbVersion, cancellationToken);
6363
Debug.Assert(result);
6464
}
6565
#if NETFRAMEWORK || NETSTANDARD2_0
66-
CheckDbVersion(connection, converters[converters.Count - 1].GetDbVersion, dbVersion);
66+
CheckDbVersion(connection, converters[converters.Count - 1].GetDbVersion, dbVersion, cancellationToken);
6767
#else
68-
CheckDbVersion(connection, converters[^1].GetDbVersion, dbVersion);
68+
CheckDbVersion(connection, converters[^1].GetDbVersion, dbVersion, cancellationToken);
6969
#endif
7070
}
7171
catch (Exception ex)
@@ -88,7 +88,7 @@ private static bool IsOrderedByAscending<TDbConnection>(this IReadOnlyList<DbCon
8888
return true;
8989
}
9090

91-
private static bool RequiresUpdate<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection) where TDbConnection : IDbConnection
91+
private static bool RequiresUpdate<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection, CancellationToken cancellationToken) where TDbConnection : IDbConnection
9292
{
9393
#if NET6_0_OR_GREATER
9494
ArgumentNullException.ThrowIfNull(converters);
@@ -104,14 +104,14 @@ private static bool RequiresUpdate<TDbConnection>(this IReadOnlyList<DbConverter
104104
#else
105105
converters[^1]
106106
#endif
107-
.RequiresUpdate(connection))
107+
.RequiresUpdate(connection, cancellationToken))
108108
{
109109
return true;
110110
}
111111
return false;
112112
}
113113

114-
private static bool Update<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection, Version dbVersion, bool forceUpdate = false) where TDbConnection : IDbConnection
114+
private static bool Update<TDbConnection>(this IReadOnlyList<DbConverter<TDbConnection>> converters, TDbConnection connection, Version dbVersion, CancellationToken cancellationToken) where TDbConnection : IDbConnection
115115
{
116116
Debug.Assert(converters != null);
117117
Debug.Assert(connection is { State: ConnectionState.Open });
@@ -120,9 +120,9 @@ private static bool Update<TDbConnection>(this IReadOnlyList<DbConverter<TDbConn
120120
Version? latestVersion = null;
121121
foreach (var converter in converters!)
122122
{
123-
if (forceUpdate || converter.RequiresUpdate(connection))
123+
if (/*forceUpdate || */converter.RequiresUpdate(connection, cancellationToken))
124124
{
125-
bool updated = converter.Update(connection);
125+
bool updated = converter.Update(connection, cancellationToken);
126126
Debug.Assert(updated);
127127
if (!updated)
128128
{

0 commit comments

Comments
 (0)