Updated to C# 12
This commit is contained in:
@@ -15,7 +15,11 @@ namespace Microsoft.EntityFrameworkCore
|
||||
/// <summary>
|
||||
/// Extensions for the <see cref="DatabaseFacade"/>.
|
||||
/// </summary>
|
||||
#if NET8_0_OR_GREATER
|
||||
public static partial class DatabaseFacadeExtensions
|
||||
#else
|
||||
public static class DatabaseFacadeExtensions
|
||||
#endif
|
||||
{
|
||||
/// <summary>
|
||||
/// Applies migration files to the database.
|
||||
@@ -23,7 +27,7 @@ namespace Microsoft.EntityFrameworkCore
|
||||
/// <param name="database">The database connection.</param>
|
||||
/// <param name="optionsAction">An action to set additional options.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>true on success, otherwise false or an exception is thrown.</returns>
|
||||
/// <returns><see langword="true"/> on success, otherwise false or an exception is thrown.</returns>
|
||||
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208")]
|
||||
public static async Task<bool> ApplyMigrationsAsync(this DatabaseFacade database, Action<DatabaseMigrationOptions> optionsAction, CancellationToken cancellationToken = default)
|
||||
{
|
||||
@@ -211,20 +215,20 @@ END;"
|
||||
if (options.SourceAssembly == null)
|
||||
{
|
||||
availableMigrationFiles = Directory.GetFiles(options.Path)
|
||||
.Where(f => f.ToLower().StartsWith(options.Path.ToLower()))
|
||||
.Where(f => f.ToLower().EndsWith(".sql"))
|
||||
.Where(f => f.StartsWith(options.Path, StringComparison.OrdinalIgnoreCase))
|
||||
.Where(f => f.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
availableMigrationFiles = options.SourceAssembly
|
||||
.GetManifestResourceNames()
|
||||
.Where(f => f.ToLower().StartsWith(options.Path.ToLower()))
|
||||
.Where(f => f.ToLower().EndsWith(".sql"))
|
||||
.Where(f => f.StartsWith(options.Path, StringComparison.OrdinalIgnoreCase))
|
||||
.Where(f => f.EndsWith(".sql", StringComparison.OrdinalIgnoreCase))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
if (!availableMigrationFiles.Any())
|
||||
if (availableMigrationFiles.Count == 0)
|
||||
return true;
|
||||
|
||||
using var command = connection.CreateCommand();
|
||||
@@ -270,7 +274,11 @@ END;"
|
||||
{
|
||||
using var stream = options.SourceAssembly.GetManifestResourceStream(migrationFile);
|
||||
using var sr = new StreamReader(stream);
|
||||
#if NET8_0_OR_GREATER
|
||||
sqlScript = await sr.ReadToEndAsync(cancellationToken).ConfigureAwait(false);
|
||||
#else
|
||||
sqlScript = await sr.ReadToEndAsync().ConfigureAwait(false);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(sqlScript))
|
||||
@@ -316,7 +324,11 @@ END;"
|
||||
{
|
||||
int affectedRows = 0;
|
||||
// Split script by a single slash in a line
|
||||
#if NET8_0_OR_GREATER
|
||||
string[] parts = FindSingleSlashInLine().Split(text);
|
||||
#else
|
||||
string[] parts = Regex.Split(text, @"\r?\n[ \t]*/[ \t]*\r?\n");
|
||||
#endif
|
||||
foreach (string part in parts)
|
||||
{
|
||||
// Make writable copy
|
||||
@@ -325,7 +337,11 @@ END;"
|
||||
// Remove the trailing semicolon from commands where they're not supported
|
||||
// (Oracle doesn't like semicolons. To keep the semicolon, it must be directly
|
||||
// preceeded by "end".)
|
||||
pt = Regex.Replace(pt.TrimEnd(), @"(?<!end);$", "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
||||
#if NET8_0_OR_GREATER
|
||||
pt = FindEndCommand().Replace(pt.TrimEnd(), "");
|
||||
#else
|
||||
pt = Regex.Replace(pt, @"(?<!end);$", "", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
|
||||
#endif
|
||||
|
||||
// Execute all non-empty parts as individual commands
|
||||
if (!string.IsNullOrWhiteSpace(pt))
|
||||
@@ -352,5 +368,13 @@ END;"
|
||||
SQLServer = 5,
|
||||
InMemory = 6,
|
||||
}
|
||||
|
||||
#if NET8_0_OR_GREATER
|
||||
[GeneratedRegex(@"\r?\n[ \t]*/[ \t]*\r?\n")]
|
||||
private static partial Regex FindSingleSlashInLine();
|
||||
|
||||
[GeneratedRegex(@"(?<!end);$", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant)]
|
||||
private static partial Regex FindEndCommand();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user