/// <summary>
        /// Roll back all collection migrations after specified version.
        /// Use -1 as a version parameter to downgrade all existing migrations.
        /// </summary>
        /// <param name="collection">The target collection name.</param>
        /// <param name="version">The desired migration version.</param>
        public void Down(string collection, long version = -1)
        {
            //Console.WriteLine($"Discovering migrations in {_locator.LocatedAssembly.FullName}");

            var appliedMigrations    = _dbMigrations.GetAppliedMigrations(collection);
            var downgradedMigrations =
                _locator.GetMigrations(new MigrationVersion(collection, version), MigrationVersion.Max(collection))
                .Where(m => appliedMigrations.Any(x => x.Version == m.Version.Version && string.Equals(x.Collection, m.Version.Collection)))
                .OrderByDescending(x => x.Version.Collection)
                .ThenByDescending(x => x.Version.Version)
                .ToList();

            _logger?.LogInformation($"Found ({downgradedMigrations.Count}) migrations in {_locator.LocatedAssembly.FullName}");

            foreach (var migration in downgradedMigrations)
            {
                try
                {
                    migration.Down();
                    _dbMigrations.CancelMigration(migration.Version);
                    _logger?.LogInformation($"Applied down migration: {migration.Version.Version}  [{migration.Version.Description}].");
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, $"Could not apply down migration {migration.Version.Version} [{migration.Version.Description}]!");
                    break;
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Rool back all migrations after specified version.
        /// Use -1 to downgrade all existing migrations
        /// </summary>
        /// <param name="version"></param>
        public void Down(long version = -1)
        {
            Console.WriteLine($"Discovering migrations in {_migrationAssembly}");

            var appliedMigrations    = _dbMigrations.GetAppliedMigrations();
            var downgradedMigrations =
                _locator.GetMigrations(new MigrationVersion(version), MigrationVersion.Max)
                .Where(m => appliedMigrations.Any(x => x.Version == m.Version.Version))
                .ToList();

            Console.WriteLine($"Found ({downgradedMigrations.Count}) migrations in {_migrationAssembly}");

            foreach (var migration in downgradedMigrations)
            {
                Console.WriteLine($"Applying: {migration.Version}");

                migration.Down();
                _dbMigrations.CancelMigration(migration.Version);

                Console.WriteLine($"Applied: {migration.Version}");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Roll back all collection migrations after specified version.
        /// Use -1 as a version parameter to downgrade all existing migrations.
        /// </summary>
        /// <param name="collection">The target collection name.</param>
        /// <param name="version">The desired migration version.</param>
        public void Down(string collection, long version = -1)
        {
            Console.WriteLine($"Discovering migrations in {_locator.LocatedAssembly.FullName}");

            var appliedMigrations    = _dbMigrations.GetAppliedMigrations(collection);
            var downgradedMigrations =
                _locator.GetMigrations(new MigrationVersion(collection, version), MigrationVersion.Max(collection))
                .Where(m => appliedMigrations.Any(x => x.Version == m.Version.Version && string.Equals(x.Collection, m.Version.Collection)))
                .OrderByDescending(x => x.Version.Collection)
                .ThenByDescending(x => x.Version.Version)
                .ToList();

            Console.WriteLine($"Found ({downgradedMigrations.Count}) migrations in {_locator.LocatedAssembly.FullName}");

            foreach (var migration in downgradedMigrations)
            {
                Console.WriteLine($"Applying: {migration.Version}");

                migration.Down();
                _dbMigrations.CancelMigration(migration.Version);

                Console.WriteLine($"Applied: {migration.Version}");
            }
        }