Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make migrator usable with postgres #558

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/Infrastructure/LeanCode.EFMigrator/BaseFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

Expand All @@ -15,31 +13,20 @@ public abstract class BaseFactory<TContext, TFactory> : IDesignTimeDbContextFact
typeof(TFactory).Assembly.GetName().Name
?? throw new InvalidOperationException("This type is not supported on Assembly-less runtimes.");

protected virtual void UseAdditionalSqlServerDbContextOptions(SqlServerDbContextOptionsBuilder builder) { }

protected virtual void UseAdditionalDbContextOptions(DbContextOptionsBuilder<TContext> builder) { }

public TContext CreateDbContext(string[] args)
{
var connectionString =
MigrationsConfig.GetConnectionString()
?? throw new InvalidOperationException("Failed to find connection string.");
var builder = new DbContextOptionsBuilder<TContext>().UseLoggerFactory(
new ServiceCollection()
.AddLogging(cfg => cfg.AddConsole())
.BuildServiceProvider()
.GetRequiredService<ILoggerFactory>()
);

var builder = new DbContextOptionsBuilder<TContext>()
.UseLoggerFactory(
new ServiceCollection()
.AddLogging(cfg => cfg.AddConsole())
.BuildServiceProvider()
.GetRequiredService<ILoggerFactory>()
)
.UseSqlServer(
connectionString,
cfg => UseAdditionalSqlServerDbContextOptions(cfg.MigrationsAssembly(AssemblyName))
);

UseAdditionalDbContextOptions(builder);
builder = UseDbProvider(builder);

return (TContext?)Activator.CreateInstance(typeof(TContext), builder.Options)
?? throw new InvalidOperationException("Failed to create DbContext instance.");
}

public abstract DbContextOptionsBuilder<TContext> UseDbProvider(DbContextOptionsBuilder<TContext> builder);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
<ProjectReference Include="../../Infrastructure/LeanCode.AzureIdentity/LeanCode.AzureIdentity.csproj" />

<PackageReference Include="Microsoft.EntityFrameworkCore" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" />

<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Logging" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" />

<PackageReference Include="Azure.Identity" />
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
<PackageReference Include="Microsoft.Data.SqlClient" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion src/Infrastructure/LeanCode.EFMigrator/MigrationsConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace LeanCode.EFMigrator;

public static class MigrationsConfig
{
public static string ConnectionStringKey { get; set; } = "SqlServer:ConnectionString";
public static string ConnectionStringKey { get; set; }

[System.Diagnostics.CodeAnalysis.SuppressMessage("?", "CA1056", Justification = "It should be `string`.")]
public static string KeyVaultUrlKey { get; set; } = "KeyVault:VaultUrl";
Expand Down
9 changes: 6 additions & 3 deletions src/Infrastructure/LeanCode.EFMigrator/Migrator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Data.SqlClient;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using static System.Console;
Expand Down Expand Up @@ -120,11 +120,12 @@ protected virtual int Seed()
{
int rowsAffected;

using (var connection = new SqlConnection(MigrationsConfig.GetConnectionString()))
using (var connection = GetDbConnection())
{
connection.Open();

using var command = new SqlCommand(File.ReadAllText(SeedPath), connection);
using var command = connection.CreateCommand();
command.CommandText = File.ReadAllText(SeedPath);

rowsAffected = command.ExecuteNonQuery();
}
Expand All @@ -133,4 +134,6 @@ protected virtual int Seed()

return rowsAffected;
}

protected abstract DbConnection GetDbConnection();
}