66 lines
2.5 KiB
C#
66 lines
2.5 KiB
C#
|
|
using System;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Hangfire.SqlServer;
|
|||
|
|
using Microsoft.Data.SqlClient;
|
|||
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using Sozsoft.Platform.Data;
|
|||
|
|
using Volo.Abp.DependencyInjection;
|
|||
|
|
using static Sozsoft.Settings.SettingsConsts;
|
|||
|
|
|
|||
|
|
namespace Sozsoft.Platform.DbMigrator;
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// DbMigrator çalışırken HangFire şemasını oluşturur.
|
|||
|
|
/// EF Core migration'larından sonra (EntityFrameworkCorePlatformDbSchemaMigrator),
|
|||
|
|
/// SqlDataSeeder'dan önce çalışarak HangFire tablolarının (HangFire.Job vb.) mevcut
|
|||
|
|
/// olmasını sağlar. Bu sayede SqlData klasöründeki HangFire view'ları ilk çalışmada oluşturulabilir.
|
|||
|
|
/// [ExposeServices] ABP'nin naming convention'ından bağımsız olarak IPlatformDbSchemaMigrator
|
|||
|
|
/// listesine dahil edilmesini sağlar.
|
|||
|
|
/// </summary>
|
|||
|
|
[ExposeServices(typeof(IPlatformDbSchemaMigrator))]
|
|||
|
|
public class HangfireDbSchemaMigrator : IPlatformDbSchemaMigrator, ITransientDependency
|
|||
|
|
{
|
|||
|
|
private readonly IConfiguration _configuration;
|
|||
|
|
private readonly ILogger<HangfireDbSchemaMigrator> _logger;
|
|||
|
|
|
|||
|
|
public HangfireDbSchemaMigrator(
|
|||
|
|
IConfiguration configuration,
|
|||
|
|
ILogger<HangfireDbSchemaMigrator> logger)
|
|||
|
|
{
|
|||
|
|
_configuration = configuration;
|
|||
|
|
_logger = logger;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public async Task MigrateAsync()
|
|||
|
|
{
|
|||
|
|
var connectionString = _configuration.GetConnectionString(DefaultDatabaseProvider);
|
|||
|
|
if (string.IsNullOrWhiteSpace(connectionString))
|
|||
|
|
{
|
|||
|
|
_logger.LogWarning("HangFire schema migration skipped: connection string '{Key}' not found.", DefaultDatabaseProvider);
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
await using var connection = new SqlConnection(connectionString);
|
|||
|
|
await connection.OpenAsync();
|
|||
|
|
|
|||
|
|
// HangFire tablolarını oluştur (idempotent — mevcutsa atlar).
|
|||
|
|
SqlServerObjectsInstaller.Install(connection, "HangFire", enableHeavyMigrations: true);
|
|||
|
|
|
|||
|
|
_logger.LogInformation("HangFire schema initialized successfully.");
|
|||
|
|
}
|
|||
|
|
catch (SqlException sqlEx) when (sqlEx.Number == 4060)
|
|||
|
|
{
|
|||
|
|
// Veritabanı henüz oluşturulmamış (ilk migration dışında tenant DB senaryosu)
|
|||
|
|
_logger.LogWarning("HangFire schema skipped: database does not exist yet (error 4060).");
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
_logger.LogError(ex, "HangFire schema initialization FAILED.");
|
|||
|
|
throw;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|