using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Skcks.Study.AbpProject.Data;
using Volo.Abp.DependencyInjection;

namespace Skcks.Study.AbpProject.EntityFrameworkCore;

public class EntityFrameworkCoreAbpProjectDbSchemaMigrator
    : IAbpProjectDbSchemaMigrator, ITransientDependency
{
    private readonly IServiceProvider _serviceProvider;

    public EntityFrameworkCoreAbpProjectDbSchemaMigrator(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public async Task MigrateAsync()
    {
        /* We intentionally resolving the AbpProjectDbContext
         * from IServiceProvider (instead of directly injecting it)
         * to properly get the connection string of the current tenant in the
         * current scope.
         */

        await _serviceProvider
            .GetRequiredService<AbpProjectDbContext>()
            .Database
            .MigrateAsync();
    }
}
