Beispiel #1
0
    public static async Task <MigrationSummary <MigrationInfo> > ApplyMigrationsFromTestClass <T>(
        this MigrationEngine <DatabaseContext> migrationEngine,
        MigrationApproach approach = MigrationApproach.MigrationsWithNewerVersions)
    {
        var testMigrations = new List <PendingMigration <long> >();

        foreach (var nestedType in typeof(T).GetNestedTypes())
        {
            var versionAttribute = nestedType.GetCustomAttribute <MigrationVersionAttribute>();
            if (nestedType.IsClass && versionAttribute is not null)
            {
                var migration = new PendingMigration <long>(versionAttribute.Version, nestedType);
                testMigrations.Add(migration);
            }
        }

        var migrationPlan = approach == MigrationApproach.MigrationsWithNewerVersions ?
                            await migrationEngine.GetPlanForNewMigrationsAsync() :
                            await migrationEngine.GetPlanForNonAppliedMigrationsAsync();

        var pendingMigrations = migrationPlan.PendingMigrations !
                                .Intersect(testMigrations)
                                .ToList();

        return(await migrationEngine.ApplyMigrationsAsync(pendingMigrations));
    }