Exemplo n.º 1
0
        /// <summary>
        /// Apply all migrations before specified version for target collection.
        /// Use -1 as a version parameter to apply all existing migrations.
        /// </summary>
        /// <param name="collection">The target collection name.</param>
        /// <param name="version">The desired migration version.</param>
        public void Up(string collection, long version = -1)
        {
            version = version == -1 ? long.MaxValue : version;

            Console.WriteLine($"Discovering migrations in {_locator.LocatedAssembly.FullName}");

            var appliedMigrations      = _dbMigrations.GetAppliedMigrations(collection);
            var inapplicableMigrations =
                _locator.GetMigrations(MigrationVersion.Min(collection), new MigrationVersion(collection, version))
                .Where(m => appliedMigrations.All(x => x.Version != m.Version.Version || !string.Equals(x.Collection, m.Version.Collection)))
                .OrderBy(x => x.Version.Collection)
                .ThenBy(x => x.Version.Version)
                .ToList();

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

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

                migration.Up();
                _dbMigrations.ApplyMigration(migration.Version);

                Console.WriteLine($"Applied: {migration.Version}");
            }
        }
        /// <summary>
        /// Apply all migrations before specified version for target collection.
        /// Use -1 as a version parameter to apply all existing migrations.
        /// </summary>
        /// <param name="collection">The target collection name.</param>
        /// <param name="version">The desired migration version.</param>
        public void Up(string collection, long version = -1)
        {
            version = version == -1 ? long.MaxValue : version;

            //_logger.LogDebug($"Discovering migrations in {_locator.LocatedAssembly.FullName}");

            var appliedMigrations      = _dbMigrations.GetAppliedMigrations(collection);
            var inapplicableMigrations =
                _locator.GetMigrations(MigrationVersion.Min(collection), new MigrationVersion(collection, version))
                .Where(m => appliedMigrations.All(x => x.Version != m.Version.Version || !string.Equals(x.Collection, m.Version.Collection)))
                .OrderBy(x => x.Version.Collection)
                .ThenBy(x => x.Version.Version)
                .ToList();

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

            foreach (var migration in inapplicableMigrations)
            {
                try
                {
                    migration.Up();
                    _dbMigrations.ApplyMigration(migration.Version);
                    _logger?.LogInformation($"Applied migration {migration.Version.Version} [{migration.Version.Description}].");
                }
                catch (Exception ex)
                {
                    _logger?.LogError(ex, $"Could not apply migration {migration.Version.Version} [{migration.Version.Description}]!");
                    break;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Apply all migrations before specified version.
        /// Use -1 to apply all existing migrations
        /// </summary>
        /// <param name="version"></param>
        public void Up(long version = -1)
        {
            version = version == -1 ? long.MaxValue : version;

            Console.WriteLine($"Discovering migrations in {_migrationAssembly}");

            var appliedMigrations      = _dbMigrations.GetAppliedMigrations();
            var inapplicableMigrations =
                _locator.GetMigrations(MigrationVersion.Min, new MigrationVersion(version))
                .Where(m => appliedMigrations.All(x => x.Version != m.Version.Version))
                .ToList();

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

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

                migration.Up();
                _dbMigrations.ApplyMigration(migration.Version);

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