question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce a new MigrateAsync that works with asp.net DI

See original GitHub issue

MigrateAsync currently does 2 things

  1. Find classes that implement IMigration
  2. Execute the migration logic

We can create a new overload for MigrateAsync that takes an IEnumerable<IMigration> as a parameter, and only executes the migration logic e.g.

public static async Task MigrateAsync(IEnumerable<IMigration> migrationsEnumerable)
{
    var lastMigNum = (
        await DB.Find<Migration, int>()
              .Sort(m => m.Number, Order.Descending)
              .Limit(1)
              .Project(m => m.Number)
              .ExecuteAsync()
              .ConfigureAwait(false))
        .SingleOrDefault();

    var migrations = new SortedDictionary<int, IMigration>();

    foreach (var migration in migrationsEnumerable)
    {
        var t = migration.GetType();
        var success = int.TryParse(t.Name.Split('_')[1], out int migNum);

        if (!success)
            throw new InvalidOperationException("Failed to parse migration number from the class name. Make sure to name the migration classes like: _001_some_migration_name.cs");

        if (migNum > lastMigNum)
            migrations.Add(migNum, migration);
    }

    var sw = new Stopwatch();

    foreach (var migration in migrations)
    {
        sw.Start();
        await migration.Value.UpgradeAsync().ConfigureAwait(false);
        var mig = new Migration
        {
            Number = migration.Key,
            Name = migration.Value.GetType().Name,
            TimeTakenSeconds = sw.Elapsed.TotalSeconds
        };
        await DB.SaveAsync(mig).ConfigureAwait(false);
        sw.Stop();
        sw.Reset();
    }
}

This will allow more control over how to migrate the database

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dj-nitehawkcommented, Jul 8, 2021

oh and when doing the PR, please add test to the tests project pls.

1reaction
dj-nitehawkcommented, Jul 8, 2021

added to v20.18.0-beta1

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to safely apply an EF Core migrate on ASP.NET ...
There are many ways to migrate a database using EF Core, and one of the most automatic approaches is to call EF Core's...
Read more >
Add migration with different assembly
On the command line, it is the project in the current working directory. ... (ASP.NET Core 2+). Had the same issue. Here is...
Read more >
Running async tasks on app startup in ASP.NET Core 3.0
In this post I describe how a small change in the ASP.NET Core 3.0 WebHost makes it easier to run asynchronous tasks on...
Read more >
Dependency injection in ASP.NET Core
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) ...
Read more >
DI : add support to eager load a singleton service · Issue ...
In my example the data access layer cannot contain a IStartupFilter implementation because it is a classlib which has no relation to asp.net....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found