Beispiel #1
0
        public void Migrate(PersistenceMigratorRequest request, PersistenceMigratorResult result)
        {
            string targetMigration = null;

            string connString = null;

            if (request.PreviousVersion != null)
            {
                result.AddInfoMessage("EF Plugin: Executing rollback");
                connString = request.GetConnectionString();
                using (var ctx = _migratorFactory.CreateDbContext(connString))
                {
                    var repo = _migratorFactory.CreateMigrationRepository(ctx);
                    targetMigration = repo.GetTargetMigration(request.PreviousVersion.Alias);

                    if (repo.EnsureDoesNotExist(request.Version.Alias))
                    {
                        ctx.SaveChanges();
                        result.AddInfoMessage($"EF Plugin: removed '{targetMigration}' from migration mapping history");
                    }
                }
            }

            if (null == targetMigration)
            {
                result.AddInfoMessage("EF Plugin: There were no previous model migrations so nothing needed to rollback.");
            }
            else
            {
                var migrator = _migratorFactory.CreateMigrator(connString);
                migrator.Update(targetMigration);

                result.AddInfoMessage($"EF Plugin: Rolled back schema to migration target '{targetMigration}'");
            }
        }
Beispiel #2
0
        public void Migrate(PersistenceMigratorRequest request, PersistenceMigratorResult result)
        {
            var connString    = request.GetConnectionString();
            var migrator      = _migratorFactory.CreateMigrator(connString);
            var lastMigration = migrator.GetLocalMigrations().OrderByDescending(s => s).FirstOrDefault();

            if (null == lastMigration)
            {
                result.AddInfoMessage("EF Plugin: There were no model changes found so no migrations applied");

                if (request.PreviousVersion != null)
                {
                    //We have to bring the migration forward to the current version, or we won't know what to rollback to.
                    using (var ctx = _migratorFactory.CreateDbContext(connString))
                    {
                        _migratorFactory.CreateMigrationRepository(ctx)
                        .Copy(request.PreviousVersion.Alias, request.Version.Alias);
                        ctx.SaveChanges();
                        result.AddInfoMessage("EF Plugin: Added record to migration mapping history");
                    }
                }
            }
            else
            {
                migrator.Update();

                result.AddInfoMessage($"EF Plugin: Successfully executed EF migration up to '{lastMigration}'");

                using (var ctx = _migratorFactory.CreateDbContext(connString))
                {
                    _migratorFactory.CreateMigrationRepository(ctx)
                    .AddOrUpdate(request.Version.Alias, lastMigration);
                    ctx.SaveChanges();
                }

                result.AddInfoMessage("EF Plugin: Added record to migration mapping history");
            }
        }