Skip to content

Commit

Permalink
Scaffold actual server version of the underlying database. (PomeloFou…
Browse files Browse the repository at this point in the history
  • Loading branch information
lauxjpn authored Nov 24, 2019
1 parent f804faa commit bd64acc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/EFCore.MySql/Scaffolding/Internal/MySqlCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
using Pomelo.EntityFrameworkCore.MySql.Storage.Internal;

namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal
Expand All @@ -15,13 +17,14 @@ namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal
/// </summary>
public class MySqlCodeGenerator : ProviderCodeGenerator
{
/// <summary>
/// Initializes a new instance of the <see cref="MySqlCodeGenerator" /> class.
/// </summary>
/// <param name="dependencies"> The dependencies. </param>
public MySqlCodeGenerator([NotNull] ProviderCodeGeneratorDependencies dependencies)
private readonly IMySqlOptions _options;

public MySqlCodeGenerator(
[NotNull] ProviderCodeGeneratorDependencies dependencies,
IMySqlOptions options)
: base(dependencies)
{
_options = options;
}

/// <summary>
Expand All @@ -41,6 +44,18 @@ public override MethodCallCodeFragment GenerateUseProvider(
? new object[] {connectionString}
: new object[] {connectionString, new NestedClosureCodeFragment("x", providerOptions)});
}

public override MethodCallCodeFragment GenerateProviderOptions()
{
var serverVersionCall = new MethodCallCodeFragment(
nameof(MySqlDbContextOptionsBuilder.ServerVersion),
_options.ServerVersion.ToString());

var providerOptions = base.GenerateProviderOptions();

return providerOptions == null
? serverVersionCall
: serverVersionCall.Chain(providerOptions);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
using Pomelo.EntityFrameworkCore.MySql.Internal;
using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal;
using Pomelo.EntityFrameworkCore.MySql.Storage;
using Pomelo.EntityFrameworkCore.MySql.Storage.Internal;

namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal
Expand Down Expand Up @@ -97,7 +98,27 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto
private void SetupMySqlOptions(DbConnection connection)
{
var optionsBuilder = new DbContextOptionsBuilder();
optionsBuilder.UseMySql(connection);
optionsBuilder.UseMySql(connection, builder =>
{
// Set the actual server version from the open connection here, so we can
// access it from IMySqlOptions later when generating the code for the
// `UseMySql()` call.
if (_options.ServerVersion.IsDefault)
{
try
{
var mySqlConnection = (MySqlConnection)connection;
builder.ServerVersion(new ServerVersion(mySqlConnection.ServerVersion));
}
catch (InvalidOperationException)
{
// If we cannot determine the server version for some reason, just fall
// back on the latest one (the default).

// TODO: Output warning.
}
}
});

if (Equals(_options, new MySqlOptions()))
{
Expand Down

0 comments on commit bd64acc

Please sign in to comment.